Android processing .md2 model java.lang.outOfMemoryError

Started by
9 comments, last by polyfrag 11 years ago

Why am I getting java.lang.outOfMemoryError on the line


    				va[f].normals[index] = new CVector3();

in



        numverts = header.num_tris * 3;
        
    	int index;

    	FloatBuffer va2 = FloatBuffer.allocate( numverts * (3 + 2 + 3) );
        float temp;
        int f;
		CVector3 normal = new CVector3();
		CVector3 tri[] = new CVector3[3];
		tri[0] = new CVector3();
		tri[1] = new CVector3();
		tri[2] = new CVector3();
		
        CVertexArray va[] = new CVertexArray[ header.num_frames ];
        
    	for(f=0; f<header.num_frames; f++)
    	{
    		va[f] = new CVertexArray();
    		
    		va[f].numverts = numverts;
    		va[f].vertices = new CVector3[ numverts ];
    		va[f].texcoords = new CVector2[ numverts ];
    		va[f].normals = new CVector3[ numverts ];
            
    		for (i = 0; i < header.num_tris; ++i)
    		{
    			for (j = 0; j < 3; ++j)
    			{
    				index = i*3 + j;
    				
    				va[f].vertices[index] = new CVector3();
    				va[f].texcoords[index] = new CVector2();
    				va[f].normals[index] = new CVector3();
    			}
    		}
    	}
        
        vertexArrays = va;

It doesn't happen if I comment out any of the other lines


    		va[f].vertices = new CVector3[ numverts ];
    		va[f].texcoords = new CVector2[ numverts ];
    		va[f].normals = new CVector3[ numverts ];

It's the 6th model loaded and

header.num_frames = 260

numverts = 702

Before the stack trace it says:

03-25 08:33:21.510: I/dalvikvm-heap(22950): Clamp target GC heap from 103.874MB to 96.000MB

03-25 08:33:22.158: I/dalvikvm-heap(22950): Clamp target GC heap from 103.987MB to 96.000MB
03-25 08:33:22.814: I/dalvikvm-heap(22950): Clamp target GC heap from 103.987MB to 96.000MB
03-25 08:33:22.814: I/dalvikvm-heap(22950): Forcing collection of SoftReferences for 20-byte allocation
03-25 08:33:23.447: I/dalvikvm-heap(22950): Clamp target GC heap from 103.987MB to 96.000MB
03-25 08:33:23.455: E/dalvikvm-heap(22950): Out of memory on a 20-byte allocation.

This works fine on the iphone in C++/ObjC

Advertisement

Agh, I have to port it to NDK because you can't get around the heap memory limit in Java.

va[f].vertices = new CVector3[ numverts ];

I'm not primarily a Java programmer, but is this really creating a full object's worth of overhead for each CVector3? It just seems like a lot of memory to me, especially for a mobile device. Your heap is capping out at 100mb, whereas quake 2 only required 24MB of ram. Ok, they used 16 bit integers for a lot of it, so double it to 50 MB using 32 bit floats for modern systems. And this was for multiple models and a full level map.

Does Java/Android have any reasonable way to pack it down? Quake in the old days was in C, so each vector took just 3 floats. In many ways programming for a mobile device is just like an old PC.

You could pack things down into larger allocations:

class CVector3Array {

int capacity;

int count;

float[] data; //constructor will size to 3 * number of vectors

}

You would have to implement a few things yourself, and this:

vertices.set( i, x,y,z);

won't be as elegant as

vertices = new CVector3d(x,y,z);

But it might end up cleaner than dropping to C.

I have to agree it looks like a lot of memory, and directly storing the floats rather than using a class is a great way to go.

Also, it looks like a lot of verts. For us it depends on the size of the object in the world, but between 150-200 is the norm and about 500 is the high-end limit. Of course we potentially have a lot of objects on the screen, but we still end up pushing the limits of much hardware.

There's 138 vertices but I use glDrawArrays instead of glDrawElements so triangle vertices aren't shared.

There's 138 vertices but I use glDrawArrays instead of glDrawElements so triangle vertices aren't shared.

Then your earlier post has a discrepancy:

header.num_frames = 260

numverts = 702

Your code allocates:


for(f=0; f<header.num_frames; f++)
    {
        va[f].vertices = new CVector3[ numverts ];
        va[f].texcoords = new CVector2[ numverts ];
        va[f].normals = new CVector3[ numverts ];
 

260 frames * 702 verts = 182'520 items. I don't know how big your individual classes are, but I'm guessing after allocation and padding and whatnot the classes are 64 bytes big, giving over 11.6MB for that one model. If they are only 32 bytes big after padding then they are only 5.8MB for that one model. And that's just the vertex data.

That is a huge amount of space for a single model.

Out of curiosity, why do you have a different copy of it for every frame? Are those supposed to be animation frames or something? Or is it really a completely separate copy of every object?

Well I need the vertices for hit detection. The rest is stored in VBO. Not every object is important for hit detection though. Not sure how I could further reduce it to get to a Quake 2 level.

And they're animation frames of course.

And they're animation frames of course.

Maybe I am missing something here.

Why do you need a copy of every vertex, every texture coordinate, and every normal for every animation frame?

Most engines use a skinned model. They only need to store the position and orientation for five or ten bones, and even then they only store it for a few key frames. Main character models might have in the range of 50 or so bones rather than ten. But even 50 for a highly articulated model is MUCH less than your 702 entries.

I know, but I read somewhere that skeletal animations aren't so good on phones. But I guess they were talking about older models. It's a trade-off between memory and CPU I guess. But the engine is all md2 now.

Agh, I have to port it to NDK because you can't get around the heap memory limit in Java.

Just load texture and model data to the GPU, destroy the objects loaded on the CPU side as soon as the resources is pushed over to OpenGL.

Most android devices use shared memory so there is no disadvantage to storing all model data in VBOs, even if you use keyframes, memory used by OpenGL does not count towards the size limit on the managed heap., The GC will kick in if you run out of memory(But if you still hold references to a bunch of data it can't do much and will throw an exception if you try to allocate more) but once everything is loaded you probably want to "force" garbage collection to reduce the risk of the gc kicking in during gameplay. (you can do this on android by creating a weak reference to an allocated object, set the objects reference to null and then repeatedly call system.gc followed by sleep(1) until the weak reference fails or a timer expires)

You should still aim to use less than 512MB total or you will run into serious issues on older devices.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

This topic is closed to new replies.

Advertisement