Memory Management - (Un-)Managed BufferObjects!

  ... information about changes and new features of AndEngine,

Memory Management - (Un-)Managed BufferObjects!

Postby Nicolas Gramlich » Mon Jun 20, 2011 9:49 pm

Hello Community,

yesterday I had introduced that BufferObjects can be either Managed or Unmanaged.
The changes originate from the patches supplied by the user zergling_zzh made during the development of the Z-Studio game "ZDefense", which btw is super awesome and addictive :( :? :lol:


This works as follows:

Background-Information:
When you create a Sprite, there are two BufferObjects involved:
  1. a VertexBuffer, holding the Geometry. (Created by the Sprite itself.)
  2. a TextureRegionBuffer, holding some Texture data. (Created when creating a TextureRegion)

These BufferObjects can be created as Managed (this is when AndEngine takes the work of loading and unloading from you) and Unmanaged (this is the do-it-yourself mode). Following are the technical details:

When the buffers are created as:
  • Managed:
    • When instanciated, they load themselves to the currently active BufferObjectManager.
    • The object that is using the BufferObject will make sure, that once itself is garbage collected, the BufferObject is released.
  • Unmanaged:
    • You have to load the BufferObject to the BufferObjectManager yourself.
    • The object that is using the BufferObject will leave it alive, once itself is garbage collected!

For the VertexBuffers 'Managed' works perfectly fine and totally transparent to you, as every Sprite uses its own VertexBuffer (unless you don't pass one to its constructor).
For the TextureRegions is gets tricky, as quite often one TextureRegion is used by multiple Sprites. So when one Sprite gets garbage-collected, that has a Managed TextureRegionBuffer it unloads that TextureRegionBuffer and "destroys" all other Sprites using the same TextureRegion(Buffer), what is most likely not desired!

So what I decided to do is to make the VertexBuffers Managed by default. Also the TextureRegionBuffers are created as managed if you instanciate a TextureRegion yourself (you usually don't do that). BUT when using the TextureRegionFactory to create your TextureRegions (which is btw the desired way and 99.9% of the developers do it like this) they are created as managed, but after creation they are set to be unmanaged(!), what means that they have already loaded themselves, but they are not automatically unloaded to prevent the above "shared-destroying" issue!

I could have solved this by automatically by making the TextureRegion being cloned, when passed to the constructor of a Sprite, but I though it would be better this way. Which unfortunately means that there is a "little memory leak", when one is not adapting his code. This "little memory leak" is actually not a problem, as it is very small (a few bytes per TextureRegion, of which you usually don't have more than a dozen and more importantly it does not increase over time :!:)

Changeset: http://code.google.com/p/andengine/sour ... c7789eae3f

Best Regards,
Nicolas
Nicolas Gramlich
Site Admin
 
Posts: 1734
Joined: Mon Jun 07, 2010 6:20 pm
Location: Schriesheim, Germany

Re: Memory Management - (Un-)Managed BufferObjects!

Postby shailaja » Mon Jun 27, 2011 11:48 am

Hi Nicolas
Thanks for developing Andengine. Am an android game developer. and this is what exactly my problem. While playing my game after few levels, whenever i am moving from one level to next level am getting Nullpointerexception at bufferobjectmanager class. As you told i got that i have to clone my app. and i want to know the exact way to avoid this problem. could you please give suggestion to getting updated with this new changes.
Thanks
shailaja
 
Posts: 8
Joined: Wed May 04, 2011 8:11 am

Re: Memory Management - (Un-)Managed BufferObjects!

Postby James » Fri Jul 08, 2011 1:11 am

Before you add anymore new features, PLEASE document your entire framework. I'm very interested in using Andengine, but digging through contradicting forum posts to get a straight answer is impractically consuming my time, and our dev cycle is VERY tight.
James
 
Posts: 1
Joined: Fri Jul 01, 2011 8:35 am

Re: Memory Management - (Un-)Managed BufferObjects!

Postby panzerfahrer » Mon Jul 11, 2011 11:38 pm

James wrote:Before you add anymore new features, PLEASE document your entire framework. I'm very interested in using Andengine, but digging through contradicting forum posts to get a straight answer is impractically consuming my time, and our dev cycle is VERY tight.


At the risk of getting annoying > http://code.google.com/p/andengine/issues/detail?id=40
panzerfahrer
 
Posts: 14
Joined: Mon Jul 11, 2011 10:53 am

Re: Memory Management - (Un-)Managed BufferObjects!

Postby akashguptacs » Tue Oct 23, 2012 11:40 am

This is update in andengine or we (Developers) have to code like that?
if we developers have to code then which vertex buffer to put in constructor as its need one?
when i am using sprite.getvertextbufferobjectmanager() it is giving error.
can any one please explain a bit what's going on here?
akashguptacs
 
Posts: 1
Joined: Mon Oct 15, 2012 10:43 am

Re: Memory Management - (Un-)Managed BufferObjects!

Postby MoxorTheOne » Thu Dec 13, 2012 10:44 pm

Can anyone provide an loading/unloading example on both modes, managed and unmanaged?

Thanks!
MoxorTheOne
 
Posts: 1
Joined: Thu Dec 13, 2012 10:40 pm


Return to Updates

Who is online

Users browsing this forum: Google Feedfetcher and 2 guests