Author Topic: GC frees too much  (Read 2633 times)

Offline vael

  • byte
  • *
  • Posts: 10
    • View Profile
GC frees too much
« on: November 20, 2010, 05:54:50 pm »
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:

Code: [Select]
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

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11968
    • View Profile
    • http://www.jpct.net
Re: GC frees too much
« Reply #1 on: November 20, 2010, 06:38:09 pm »
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?

Offline vael

  • byte
  • *
  • Posts: 10
    • View Profile
Re: GC frees too much
« Reply #2 on: November 20, 2010, 08:08:06 pm »
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

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11968
    • View Profile
    • http://www.jpct.net
Re: GC frees too much
« Reply #3 on: November 20, 2010, 09:16:50 pm »
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.

Offline vael

  • byte
  • *
  • Posts: 10
    • View Profile
Re: GC frees too much
« Reply #4 on: November 21, 2010, 04:06:47 pm »
I use an up-to-date version of jPCT.

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

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11968
    • View Profile
    • http://www.jpct.net
Re: GC frees too much
« Reply #5 on: November 21, 2010, 04:10:10 pm »
rotateAxis might create something...i'll take a look later.

Offline vael

  • byte
  • *
  • Posts: 10
    • View Profile
Re: GC frees too much
« Reply #6 on: November 21, 2010, 04:15:26 pm »
oh it seems like I've found the problem:

Code: [Select]

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
« Last Edit: November 21, 2010, 04:22:17 pm by vael »

Offline vael

  • byte
  • *
  • Posts: 10
    • View Profile
Re: GC frees too much
« Reply #7 on: November 21, 2010, 04:38:26 pm »
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?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11968
    • View Profile
    • http://www.jpct.net
Re: GC frees too much
« Reply #8 on: November 21, 2010, 09:13:12 pm »
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...

Offline vael

  • byte
  • *
  • Posts: 10
    • View Profile
Re: GC frees too much
« Reply #9 on: November 22, 2010, 06:44:17 pm »
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?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11968
    • View Profile
    • http://www.jpct.net
Re: GC frees too much
« Reply #10 on: November 22, 2010, 09:32:14 pm »
No, it just returns the internally used instance.