Archived

This topic is now archived and is closed to further replies.

[java] GF4J - Rotation Effect eventually causes out of memory error

This topic is 5620 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey everyone - I''m using Gameframe for java version 0.9.6 and after rotating a bitmap a few times I get an "out of memory" exception... Here is the jist of the code: /////////////////////////////////////////////////////////////// //Declare variables CloneableBitmap originalPic = null; RotationEffect myRotationEffect = RotationEffect.SHARED_INSTANCE; BitMap todraw = null; originalPic = myGraphicsEngine.loadKeyColorBitmap("plane.bmp",true); todraw = myGraphicsEngine.loadKeyColorBitmap("plane.bmp",true); //... game loop { myRotationEffect.setRotationDegrees(degrees); todraw = originalPic.getClone(myRotationEffect,false); todraw.drawTo(x,y); myGraphicsEngine.flip(); } ///////////////////////////////////////////////////////////// If I rotate the picture too many times then I will get an out of memory error... Is there any way I can ensure that I get rid of previously used rotations? Or any idea what is causing the error? Thanks in advance for any help, -Alex

Share this post


Link to post
Share on other sites
I''d email javanerd directly on this one. Or just make a big image with all the rotations all ready in it then use an imagefilter to break it down into an image[] or whatever.

Share this post


Link to post
Share on other sites
The "out of memory" exception occurs because you are creating bitmaps and you don''t free the resources used by these bitmaps.

As a quick fix, you should add "todraw.finalize()" after the "todraw.drawTo(x,y);" line. This will free all resources that are used by the created bitmap.

As a long term solution I suggest you should change your code so that in the beginning you create all the needed rotations of the bitmap into an bitmap array and then use these bitmaps when drawing. This is because you''re current method is very unoptimal as it creates one Bitmap object every gameloop cycle (even if the rotation stays in the same value over several gameloop cycles).

Hopefully this clears things up a bit

-
JavaNerd

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
and I thought in java you don''t need to worry about
freeing memory, how disappointing...

Share this post


Link to post
Share on other sites
Mr. Javanerd -

Thanks for the advice, it works like a charm! I added the todraw.finalize() and only have it create a rotated bitmap if it needs to (ie it will use the same todraw if the object hasn''t rotated)...

I''d also like to say how much fun I''m having working with GF4J - I had no idea you could do 2D graphics like that in Java... I''ve been out of touch with C/C++ for a while, and finding a 2D graphics engine in GF4J may have single handedly resurrected my game programming hobby... Thanks again!

-Alex

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
and I thought in java you don''t need to worry about
freeing memory, how disappointing...



You don''t, but if the object doesn''t go out of scope, the memory never gets released.

---
Make it work.
Make it fast.

"Commmmpuuuuterrrr.." --Scotty Star Trek IV:The Voyage Home

Share this post


Link to post
Share on other sites
I have one more question... Is it possible to grab a SubBitmap of what is currently on the display or the backbuffer?

I would use this so that instead of redrawing the whole screen each iteration I could grab a copy of a section of the background image before I draw an object on top of that, and store what was there with the object so the object can erase itself by only writing over that small section again...

I looked through the documentation but didn''t see anyway to get a Bitmap from the GraphicsEngine besides loading it from a file...

Thanks,

-Alex

Share this post


Link to post
Share on other sites
quote:
Original post by javanerd
The "out of memory" exception occurs because you are creating bitmaps and you don''t free the resources used by these bitmaps.

As a quick fix, you should add "todraw.finalize()" after the "todraw.drawTo(x,y);" line. This will free all resources that are used by the created bitmap.



Why isn''t the finalize method simply called by the GC ?

Share this post


Link to post
Share on other sites
//... game loop
{

myRotationEffect.setRotationDegrees(degrees);
todraw = originalPic.getClone(myRotationEffect,false);

todraw.drawTo(x,y);
myGraphicsEngine.flip();
}

=> ''todraw'' gets a new pic during each loop, that means: ''todraw'' referenced to a pic created by ''getClone(...)'' and replace this reference to a new pic generated by ''getClone''. So, the "old" pic is existing but has no more variable referencing it. Isn''t it ?
That''s why he had an ''out of memory error''. So, why didn''t the GC came in action and destroyed these "old" pics ?

Share this post


Link to post
Share on other sites