Slick Forums

Discuss the Slick 2D Library
It is currently Sun Jul 15, 2018 11:17 pm

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Mon Mar 05, 2012 7:07 pm 
Offline

Joined: Mon Feb 13, 2012 6:31 pm
Posts: 40
I was wondering, do you have to call destroy method of Image class, once the image will no longer be used? or will Java garbage collection automatically release the resource after a while?


Top
 Profile  
 
PostPosted: Tue Mar 06, 2012 1:09 am 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1482
You should destroy them if they are no longer needed, otherwise they may not be collected.

Keep in mind that sub-images and shallow copies (e.g. flipped copy) may use the same texture, so destroying one will effectively destroy all of them.


Top
 Profile  
 
PostPosted: Tue Mar 06, 2012 6:34 am 
Offline

Joined: Mon Feb 13, 2012 6:31 pm
Posts: 40
Yes, I should destroy every image if I can. but will garbage collection never release the image resources if I forgot to destroy them or if I left them undestroyed(but no refer to them) ? Some of the images are loaded dynamically( I dont't want to pre-load them) in my game but it seems image will be corrupt when destroying it in the same frame it was loaded, and making method to destroy them after next frame sometimes make my script less readable. I guess I should do it, but just wondering if I can just leave them with no refer.


Top
 Profile  
 
PostPosted: Tue Mar 06, 2012 7:32 am 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1482
The texture data is stored in video ram by OpenGL -- this won't be cleaned up by the garbage collector. If you are using getGraphics, the Graphics context (e.g. the OpenGL frame buffer object or Pbuffer) also won't be destroyed properly. The Java Texture object might hang around in InternalTextureLoader's cache a while longer; or it may hang around indefinitely if you are using Android / setHoldTextureData.

In short: if you plan to create many images over time, you need to destroy them otherwise your game will run out of memory.

Some things to note:
  • If you are creating images and then calling getGraphics on them, two internal textures will be created. If you then destroy the image, only one texture will be released. This is a bug/limitation with the current offscreen rendering technique. You can read about it here. I've added createOffscreenImage to the development branch to deal with the issue -- details here. I've also included a method in InternalTextureLoader called getTextureCount, which might be useful for debugging your program.
  • If you're concerned about memory management, think about things like using power of two textures (slick will convert a 513x513 image into a 1024x1024 texture.. that's a lot of wasted memory!) and internal formats (a grayscale texture is four times smaller than RGBA). Nitram added a patch to the dev branch that supports grayscale PNGs.
  • If you are really concerned with memory, you could decode your image to a byte buffer and copy the data to a single large texture (i.e. sprite sheet). Instead of creating a new OpenGL texture per image (and potentially a new framebuffer per image, if you are using getGraphics), you are just decoding bytes and uploading them to a single texture. This also means your images will render much more quickly since they don't each require a texture bind, plus you can use startUse/endUse to further speed things up.


Top
 Profile  
 
PostPosted: Tue Mar 06, 2012 8:21 am 
Offline

Joined: Mon Feb 13, 2012 6:31 pm
Posts: 40
Thank you for quick and detailed reply. That was really all I wanted to know, and getTextureCount method will be useful when debugging :)


Top
 Profile  
 
PostPosted: Tue Dec 12, 2017 5:31 am 
Offline

Joined: Fri Mar 31, 2017 5:26 am
Posts: 2
destroy() removes the texture data from the gpu, has nothing to do with the garbage collection.
image = null would case that (you have to kill all references of course).


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group