• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

help me to fix memory leak in ImageIO.write() and ImageIO.read()

 
Ranch Hand
Posts: 530
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am running into problem regarding memory leak when writing and reading images using the following two methods from ImageIO class:



I am using these two methods in a while loop and observe that memory is continuously rising significantly.

Did anyone have the same problem and was able to fix it?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you think there's a leak? It's understandable that these methods would use a significant amount of memory, but what makes you think it's not getting released during a subsequent GC?
 
Nam Ha Minh
Ranch Hand
Posts: 530
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know why memory does not get released. Here is the code I use:



Even when I try to use a FileOutputStream and close it after writing, the memory is still increasing:



The buffImg reference is not used somewhere else.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the JVM actually run out of memory? Just because the memory isn't released immediately doesn't mean it won't eventually if it is needed.
 
Nam Ha Minh
Ranch Hand
Posts: 530
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:Does the JVM actually run out of memory? Just because the memory isn't released immediately doesn't mean it won't eventually if it is needed.



No, I don't wait till the JVM runs out of memory, but I think it will be out of memory soon because I see the memory increases significantly from 100MB to 500MB within a minute. So I have to stop the JVM process first. And there seems to be no clue that the memory is reclaimed.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can you say that if you didn't even get an OutOfMemoryError? The JVM will not throw that error unless it really has to. It will always first try to garbage collect objects to prevent it. Perhaps it would have done so but you just didn't give it a chance.
 
Nam Ha Minh
Ranch Hand
Posts: 530
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh my god!!!
I don't know why, that memory leak problem only happens when I run the program inside NetBeans. Somehow the IDE does not release memory immediately.
But when I run the program outside NetBeans, there seems to be no problem at all.
What a strange behavior!
 
Nam Ha Minh
Ranch Hand
Posts: 530
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Today I noticed that when running the program with Java 7 64bit the memory leak problem happens again.
That doesn't happen with Java 32bit.
So there may be some memory issues with Java 64bit?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll ask again: has the program ever caused an actual memory problem (and not just an imagined one like you're describing)?
 
Nam Ha Minh
Ranch Hand
Posts: 530
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:I'll ask again: has the program ever caused an actual memory problem (and not just an imagined one like you're describing)?


It doesn't cause OutOfMemory error, but I can see the memory allocated for the program rises significantly just in seconds.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's expected behavior is something memory-intensive is done. Keep running the program for longer to see if it actually runs out memory. Normally, the garbage collector (GC) should reclaim the memory before that (but not right after the instructions causing the memory to be allocated, as you seem to think).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic