Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Nam Ha Minh
Ranch Hand
Posts: 502
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • 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?
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • 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: 502
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • 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: 42967
73
  • Mark post as helpful
  • send pies
  • 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: 502
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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: 502
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • 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: 502
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • 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: 42967
73
  • Mark post as helpful
  • send pies
  • 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: 502
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • 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: 42967
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic