Hello Guest

Memory deallocation using BufferedImage

  • 12 Replies
  • 21732 Views
Memory deallocation using BufferedImage
« on: September 16, 2003, 07:53:29 »
I need to deallocate -at the moment- the memory used when I load the PNG files from java.

I use ByteBuffers to store the texture that must be used for openGL, but java maintains the BufferedImage that I used for load the image.

I wrote the following:

image = null;
System.gc();

but I'm surelly that the deallocation never happened.

Othertwise I think that LWJGL stores a copy of the texture loaded for this reason:

when the user changes application <Alt+Tab> the textures stored in video memory doesn't dissappear.

There are any variable or argument that I can change  to avoid this texture duplication?

Thanks: Elvis Enmanuel

*

Offline elias

  • *****
  • 899
    • http://oddlabs.com
Memory deallocation using BufferedImage
« Reply #1 on: September 16, 2003, 08:20:31 »
There isn't. OpenGL automatically stores all textures internally. But that just means that after upload, you can toss the BufferedImage and the ByteBuffer.

 - elias

Memory deallocation using BufferedImage
« Reply #2 on: September 16, 2003, 08:51:18 »

*

Offline elias

  • *****
  • 899
    • http://oddlabs.com
Memory deallocation using BufferedImage
« Reply #3 on: September 16, 2003, 09:02:51 »
Then you are using OpenGL in C++ wrong. OpenGL guarantees that your textures will survice mode switches etc. By tossing I mean to set the reference to null, like you do for the image in your original post.

 - elias

Memory deallocation using BufferedImage
« Reply #4 on: September 16, 2003, 10:29:59 »
Unless you mean that the textures disappear *from the screen*?

The screenbuffer is maintained by OpenGL, so if you draw an image that includes a texture that you then delete, unless you clear the framebuffer you'll still see the image on-screen.
ellomynameis Charlie Dobbie.

Memory deallocation using BufferedImage
« Reply #5 on: September 16, 2003, 12:58:09 »
I use glDeleteTextures() and glTexImage2D() functions to improve my own texture management, if texture doesn't appears in screen I delete it and when appears an again I upload it from my texture copy in system memory.

In this form openGL -should- have the texture in video memory, because the video memory aren't full.

But don't worry that ain't the problem I test this and works fine in C++.

I only want I know is: how can I release the BufferedImage in java at the moment?

image = null;
System.gc();

this doesn't work, and I have too many memory leaks.

Thanks in advance.

*

Offline elias

  • *****
  • 899
    • http://oddlabs.com
Memory deallocation using BufferedImage
« Reply #6 on: September 16, 2003, 13:04:13 »
It works allright. How can you tell that it doesn't? If you look a the memory taken by the java process you will be wrong - java doesn't shrink it's memory every time a gc runs. You should use Runtime.* methods to get used memory. ByteBuffers won't be counted though.

 - elias

*

Offline princec

  • *****
  • 1933
    • Puppygames
Memory deallocation using BufferedImage
« Reply #7 on: September 16, 2003, 14:57:03 »
chelvis,
you should not be deleting textures from OpenGL and keeping them in system memory. That's what OpenGL does for you, automatically. All you're doing is making your program use twice the system memory and slowing it down a whole load.

Cas :)

Memory deallocation using BufferedImage
« Reply #8 on: September 17, 2003, 01:01:39 »
I do exactly this in Java, but  I need to deallocate the memory used when I create a BufferedImage...

*

Offline princec

  • *****
  • 1933
    • Puppygames
Memory deallocation using BufferedImage
« Reply #9 on: September 17, 2003, 01:08:48 »
Ah, no you don't, just forget about it. It'll get freed automatically if you run out of memory.

The other problem with BufferedImage of course is that it's a bit inefficient loading it in and then transferring it to GL via JNI. If you were to load the image directly into a DirectByteBuffer you'd have much better memory usage.

Cas :)

*

Offline oNyx

  • ***
  • 177
  • 弾幕
Memory deallocation using BufferedImage
« Reply #10 on: September 18, 2003, 01:55:54 »
>Ah, no you don't, just forget about it. It'll get freed automatically if you run out of memory.

If you use a MediaTracker and dont remove em (from the mediatracker) you will run out of memory (OutOfMemoryException), because the MediaTracker caches em all (it looks like it is doing that forever).

-load an image
-add it to your MediaTracker
-wait for it
-copy it to whatsoever
-remove it from your MediaTracker

However the OutOfMemoryException occours rarly (you need a lot of images to fill up all ram).

*

Offline bedelf

  • ***
  • 196
Memory deallocation using BufferedImage
« Reply #11 on: September 18, 2003, 02:36:36 »
Is there a downside to using javax.imagio.ImageIO.read() instead of mediatracker for applications? I hardly remember any details about this stuff now.

*

Offline princec

  • *****
  • 1933
    • Puppygames
Memory deallocation using BufferedImage
« Reply #12 on: September 18, 2003, 10:57:39 »
You know, in all the years I've used Java I never actually used MediaTracker! Strange but true. I've always used ImageIO.

Cas :)