Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Memory Leak

 
Manoj Amrutkar
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting this error in my swing application

"java.lang.OutOfMemoryError"
My Application has different form, which are opened one after another in page down key stroke, If i goes to fast(page down key press) above errors comes.

Frame is contaning internal frames. which displays my forms (one at a time)
Internal frames contains layered pane on which bottom layer i have used .gif images (which are the once creating the problem as there size is bit large say around 300 KB) to show forms.

Code is something like this on every page down




My problem is these images loaded in memory are the once creating the problem.

Please suggest something which will help in this regard

Some thing which will clear up (garbage collection), before next page down.

Thanks

Manoj
 
Joseph Kampf
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well the problem is your holding onto these images. You get too many of them and you are storing copies of them. Even if it is the same image.

Can your heap size handle 1 copy of each of the 4 images? Instead of loading them each time, you could cache references to each of the 4 and just point your myImage value at the correct reference.

Another option is to synchronize your method. This will at least ensure that you only have 1 valid reference to myImage at a time.


Originally posted by Manoj Amrutkar:
I am getting this error in my swing application

"java.lang.OutOfMemoryError"
My Application has different form, which are opened one after another in page down key stroke, If i goes to fast(page down key press) above errors comes.

Frame is contaning internal frames. which displays my forms (one at a time)
Internal frames contains layered pane on which bottom layer i have used .gif images (which are the once creating the problem as there size is bit large say around 300 KB) to show forms.

Code is something like this on every page down




My problem is these images loaded in memory are the once creating the problem.

Please suggest something which will help in this regard

Some thing which will clear up (garbage collection), before next page down.

Thanks

Manoj
 
Ernesto Elias-Nieland
Ranch Hand
Posts: 53
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code is missing where you store your images. After the method you show completed all images will be gc'ed. So there should be no out of memory error. Somehow you keep a reference of all images.
As a solution I would suggest (if there are just some different pictures): 1. create all of them on application startup and save them in a HashMap.
2. Give every new frame a reference to the HashMap.
3. Pull the image out of the HashMap in your method and show the picture.

By this you have just one image object for every picture.

Cheers,
Ernie
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic