• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to free memory allocated by "new ImageIcon".

 
Nilton Castro
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to free memory allocated by "new ImageIcon"? I have a form with background image and each 200 ms i change the background image in infinite loop. When i execute the program i can see a crescent memory usage (duuhh). Then i ask you Big Moose Guys and Gals, how to free memory allocated after draw each image on background and avoid expensive allocation? My humble code is:

[color=blue]... INFINITE LOOP (each 200 ms i call this: ===>
public void setImagem(String Imagem)
{
if (Imagem != null)
{
imagem = new ImageIcon(Imagem).getImage();
}
else
{
imagem = null;
}
repaint();
}
<=============
[/color]
P.S. I have the same problem with C++ e never can solved it. In C i used "memory pointer = malloc (qtd bytes); free (memory pointer) and always worked fine. Now i use [b]Java Swing[/b] (i´m afraid about)
 
Darryl Burke
Bartender
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off, if you need an Image why are you constructing an ImageIcon? You should load any non-animated image via ImageIO#read(...) or, for an animated GIF, Toolkit#createImage(...). If you use Toolkit, you may need to free up reconstructable resources by calling the Image method that is documented to do so -- as Toolkit caches images for re-use.

And welcome to the Ranch!

edit Corrected a typo
 
Nilton Castro
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke, thank you very much for quick answer. I´ll give a try and as soon as possible i will feedback.
 
Rob Spoor
Sheriff
Pie
Posts: 20368
43
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:First off, if you need an Image why are you constructing an ImageIcon? You should load any non-animated image via ImageIO#read(...) or, for an animated GIF, Toolkint#createImage(...).

Well, if you're lazy you can use ImageIcon to take care of the MediaTracker code for you.

As for the infinite loop, that's not the right way to go. Either you're doing it on the EDT and you're blocking that, or you're doing it from another thread and probably are repainting from a non-EDT thread. You should check out Concurrency in Swing. In this case a javax.swing.Timer would be a better solution.
 
Darryl Burke
Bartender
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:
Darryl Burke wrote:First off, if you need an Image why are you constructing an ImageIcon? You should load any non-animated image via ImageIO#read(...) or, for an animated GIF, Toolkit#createImage(...).

Well, if you're lazy you can use ImageIcon to take care of the MediaTracker code for you.

Correct, if you need to load an animated GIF using Toolkit. For any other image type, using ImageIO#read(...) is the laziest solution
 
Nilton Castro
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Good Big Moose Guys!
No offense guys, I'm not lazy, I'm ignorant, a beginner in java. What I can not understand is that in java (and C++ too) you can easily open any door using "new", but it is not easy to close. Therefore, and again with all due respect, I ask again: if the java allocates memory for each "new ImageIcon" or every "ImageIO.read" I wish deallocate this memory to use it again without having to see my program find an error like "Out of Memory" after opening "n" images. I tried "image.flush (); image = null", which helped a little, but brought unexpected results, like a trail on the screen when I drag the form and just slow the memory consumption. I just want to build a desktop application with animated backgrounds. There are 20 images per form and 20 forms (400 jpg images 1024x640). The dumb solution is allocate only once time all images ... I'm not able to think in another solution and unless someone helps me I will commit this programmatic suicide. For me the Java seems wonderful on the surface but disappoint me in the depth, like windows and unix. All have primitive errors and inconsistences that were never corrected (again, i´m so ignorant). Thanks a lot!
 
Darryl Burke
Bartender
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No offense guys, I'm not lazy, I'm ignorant, a beginner in java.

Nobody called you lazy. We were discussing the laziest solution -- the solution that involves writing the least code.

unexpected results, like a trail on the screen when I drag the form

Read the second paragraph of Rob Spoor's response. Carefully. Ask about anything that's over your head.

Since you're loading jpegs, I would again advise the use of ImageIO#read(...) and re-use of the same variable so that discarded images are no longer reachable and are eligible for GC.

There are 20 images per form and 20 forms (400 jpg images 1024x640).

What's a 'form'? how many of these 20 'forms' are displayed concurrently?
 
Nilton Castro
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Darryl Burke!
Thanks again! I've read every word carefully and I'm trying to digest them. This takes some time because I'm really very new to Java. But I appreciate every second that you are spending with me. When I mention "form" I mean JFrame with components such as tables, input fields, etc. - meaning a traditional desktop application with various "forms" linked among themselves. I wanted to innovate the background of these "forms" creating animations (20 images each) that are displayed in sequence, an interval of 20ms between them (i used timer). Upon reaching the end of the sequence the first image appears again and so on. Each form have a proper sequence of images. I want to have an application where the user does not see only data information, but also can see relaxing images of animated mountains, rivers, lakes, rain, etc., behind the data. But to do that I need to handle a lot of images with the best possible memory consumption. I do not want to use animated gif as the quality is poor. I opened 6 "forms" simultaneously and i saw 4 animations...
 
Nilton Castro
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Big Moose guys that help me!
I want to thank everyone for their support and especially the respect with which you treated me (I'm very ugly but my English is even uglier). I follow the wonderful suggestion to try ImageIO.read (image) and BINGO!!! it worked exactly as I imagined it should be. There is no waste of memory, execution speed is good and I have a sketch of a commercial application with beautiful animated background. I used:
BufferedImage imagem = null;
... beginning of loop
imagem.flush();
imagem = null;
imagem = ImageIO.read(new File (path_of_imagem));
[Display and timing]
[points to new image]
go back to loop ...
Thank you, brave guys!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic