-2

How to free memory allocated by ImageIcon? I have an infinite loop with "new ImageIcon ..." and i need to free the memory allocated by each image after displaying it on the screen. The display interval between images is 200 ms. Does anyone have a simple and efficient solution to avoid the growing consumption of memory? Thanks a lot!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Nilton
  • 9
  • 6
  • 1
    Could you share your code snippet? – JHS Dec 15 '12 at 02:11
  • I call this function infinite times: public void setImagem(String Imagem) { if (Imagem != null) { imagem = new ImageIcon(Imagem).getImage(); } else { imagem = null; } repaint(); } – Nilton Dec 15 '12 at 02:29
  • 1
    *"Does anyone have a simple and efficient solution to avoid the growing consumption of memory?"* Ignore it. The JVM will GC when it needs to. – Andrew Thompson Dec 15 '12 at 06:14

2 Answers2

2

If you not reusing the images and want to discard the system cached data, take a look at ImageIcon#getImage#flush

Flushes all reconstructable resources being used by this Image object. This includes any pixel data that is being cached for rendering to the screen as well as any system resources that are being used to store data or pixels for the image if they can be recreated. The image is reset to a state similar to when it was first created so that if it is again rendered, the image data will have to be recreated or fetched again from its source.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I tried your suggestion without success. The memory continues increasing each "new ImageIcon". I think (i am ignorant, no offense) that "flush" only process pending actions but not free memory allocated by "ImageIcon" (maybe i´m wrong). Have you another "magic card" to show? Thank you very much! – Nilton Dec 15 '12 at 10:27
  • 1
    `flush` would basically allow the resources previously allocated by the `ImageIcon`, which they may become available for garbage allocation at some time in the future. – MadProgrammer Dec 15 '12 at 10:51
  • Thank you very much MadProgrammer! I will flush every time i use "new ImageIcon". Have a very nice day! – Nilton Dec 15 '12 at 11:18
1

Simply after the fnal painting of the image, say images.get(i) remove the held image: images.set(i, null). In this way the garbage collector can remove it.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • I will try each answer. Thanks a lot guys, i really appreciate your quick support. – Nilton Dec 15 '12 at 02:35
  • I tried using it and could not reach success because I'm ignorant. When I type "Images.get (i); Images.set (i, null)" the interpreter NetBeans expects something more, type "set what, or get what?" and, as I said I am ignorant and do not know what to say to the interpreter associate my code with the proper function. Can you help me? The code is: public void set picture (Picture String) { if (Image! = null) { image = new ImageIcon (Picture). getImage (); Else {} image = null; } repaint (); } – Nilton Dec 15 '12 at 10:32
  • Sorry, I was the ignorant here; after writing it, you expanded your question. I assumed you held the created images in a class field, and in the paint(Component) method drew the next image. With image the only memory leak is either a reference held, or `createGraphics` without `dispose`. Sorry, maybe you do a `add(new JLabel(imageicon))` too ininetely? That would cause a problem. – Joop Eggen Dec 15 '12 at 10:43
  • I'm trying to create a desktop application (java swing) and I find it very annoying form using the "look and feel", static image, degrade or something. So I created some animations, something like an animated gif, but in my case is a sequence of 20 images 1024x640 jpg (each form) and I'm creating forms like animated wallpapers. This is the problem - how to release the image above to allocate a new image sequence without using more and more memory? – Nilton Dec 15 '12 at 11:01