GC frees too much

Started by vael, November 20, 2010, 05:54:50 PM

Previous topic - Next topic

vael

Hello,

I'm experiencing lags because of GC and I am rendering only like 30-40 box primitives at a time. I reuse all the objects so I actually I stop allocating memory after 2-3 seconds. But here is what I see in log:


D/FREEROLL(  650): CURRENT FPS: 30
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
D/FREEROLL(  650): CURRENT FPS: 40
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
D/FREEROLL(  650): CURRENT FPS: 50
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
D/FREEROLL(  650): CURRENT FPS: 44
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
D/FREEROLL(  650): CURRENT FPS: 52
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
D/FREEROLL(  650): CURRENT FPS: 56
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
D/dalvikvm(  650): GC_FOR_MALLOC freed 39703 objects / 1584536 bytes in 77ms
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
D/FREEROLL(  650): CURRENT FPS: 45
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
D/FREEROLL(  650): CURRENT FPS: 60
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
D/FREEROLL(  650): CURRENT FPS: 54
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
D/FREEROLL(  650): CURRENT FPS: 50
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
D/dalvikvm(  173): GC_EXPLICIT freed 487 objects / 48936 bytes in 837ms
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
D/FREEROLL(  650): CURRENT FPS: 45
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
D/dalvikvm(  650): GC_FOR_MALLOC freed 47179 objects / 1782160 bytes in 78ms
D/FREEROLL(  650): CURRENT FPS: 41
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
D/FREEROLL(  650): CURRENT FPS: 56
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
D/FREEROLL(  650): CURRENT FPS: 57
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
D/FREEROLL(  650): CURRENT FPS: 49
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
E/Tethering(   85): active iface (usb0) reported as added, ignoring
I/jPCT-AE (  650): Static references cleared...
D/LocationMasfClient(   85): getNetworkLocation(): Returning cache location with
accuracy 3875.0
D/FREEROLL(  650): CURRENT FPS: 47
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
D/FREEROLL(  650): CURRENT FPS: 47
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
D/dalvikvm(  650): GC_FOR_MALLOC freed 48783 objects / 1835704 bytes in 77ms
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
D/FREEROLL(  650): CURRENT FPS: 43
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
D/FREEROLL(  650): CURRENT FPS: 52
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
D/FREEROLL(  650): CURRENT FPS: 47
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
D/FREEROLL(  650): CURRENT FPS: 54
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
D/FREEROLL(  650): CURRENT FPS: 50
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
D/dalvikvm(  650): GC_FOR_MALLOC freed 48624 objects / 1832888 bytes in 76ms
D/FREEROLL(  650): CURRENT FPS: 45
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
D/FREEROLL(  650): CURRENT FPS: 53
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
D/FREEROLL(  650): CURRENT FPS: 61
I/jPCT-AE (  650): Static references cleared...
I/jPCT-AE (  650): Static references cleared...
D/FREEROLL(  650): CURRENT FPS: 58


as you see it frees about 1.5mb every few seconds. I have no any dynamic light in the world. When I had it, I had like 1.5mb free per second.

So my question is: why does jPCT needs so much memory to just render? or do I do something wrong?

Thank you

EgonOlsen

Actually, it doesn't allocate any additional memory just for the rendering. However, some other methods do/have to and not all of them have been tweaked to minimize object creation. It's impossible to tell the source of this from your description alone. Do you have a test case?

vael

The source is quite big already so lets try to find it out this way:

I use these methods in the main loop:

Camera's:
rotateX(float);
setPosition(float, float, float);
setOrientation(SimpleVector, SimpleVector);

Object3D's:
translate(float, float, float);
rotateAxis(SimpleVector, float);

can they cause this? as I said I am having about 40 boxes at a time

EgonOlsen

Are you creating new SimpleVectors in each iteration? If so, don't do this and recycle old ones.

And which version of jPCT-AE are you using? Older versions weren't as gc friendly as newer ones.

vael

I use an up-to-date version of jPCT.

And no, I don't create new SimpleVector's but maybe those methods do?

EgonOlsen

rotateAxis might create something...i'll take a look later.

vael

#6
oh it seems like I've found the problem:



public class JPCTBulletMotionState extends MotionState
{
private Object3D obj3d;
 
public JPCTBulletMotionState(Object3D obj)
{
this.obj3d = obj;
}
 
public JPCTBulletMotionState(Object3D obj, Transform startTrans)
{
this.obj3d = obj;
this.setGraphicFromTransform(startTrans);
}  


public Transform getWorldTransform(Transform worldTrans)
  {
this.setTransformFromGraphic(worldTrans);
return worldTrans;
  }
 
public void setWorldTransform(Transform worldTrans)
  {
this.setGraphicFromTransform(worldTrans);
  }
 
 
private void setTransformFromGraphic(Transform tran)
{
SimpleVector p = this.obj3d.getTransformedCenter();
tran.origin.set(p.x, p.y, p.z); // not sure if translation or position

Matrix matrixGfx = this.obj3d.getRotationMatrix();
//matrixGfx.rotateX((float)Math.PI);
MatrixUtil.getOpenGLSubMatrix(tran.basis, matrixGfx.getDump());
}
 
private void setGraphicFromTransform(Transform tran)
{
SimpleVector pos = obj3d.getTransformedCenter();
this.obj3d.translate(tran.origin.x - pos.x,
 tran.origin.y - pos.y,
 tran.origin.z - pos.z);

float[] dump = this.obj3d.getRotationMatrix().getDump(); //new float[16];
Matrix matrixGfx = new Matrix();
    MatrixUtil.getOpenGLSubMatrix(tran.basis, dump);
   
    matrixGfx.setDump(dump);
    //matrixGfx.rotateX((float)Math.PI);
   
    this.obj3d.setRotationMatrix(matrixGfx);
}
}


Is there anything I can save here?

when I create DefaultMotionState instead of this one, I don't have GC working so hard
well at least it is freeing way less

vael

and by the way seems like you've pointed the right method. because when I comment using it GC goes way more quite. By the way are you sure translate() doesn't create objects and doesn't cause a renderer to create some because an object is translated?

EgonOlsen

No, translate() is fine. However, apart from rotateAxis(), which created one SimpleVector and one Matrix for each call, the methods you are using in your own code are partially a problem. Whenever i encounter such a bottleneck, i add a variant that takes the object to return as a parameter, so that you can reuse objects that you create in your own code.
I've uploaded a new jar (as well as new javadocs), that fixes the rotateAxis() flaw and adds two new methods, getTransformedCenter(<SimpleVector>) and fillDump(<float[]>) (instead of getDump()). Both should help you to optimize your code by creating some "pool"-objects that you can reuse in each iteration. And try to get rid of the call to new Matrix().
It's a shame, that Dalvik's gc sucks so much that this stuff is actually needed...

vael

wow you're fast! with the new version and my own code rewritten to match the optimized getters it is WAY better.

Thank you for a that fast fix.

By the way doesn't Object3D's method getRotationMatrix() create objects?

EgonOlsen

No, it just returns the internally used instance.