aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes help me to fix memory leak in ImageIO.write() and ImageIO.read() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "help me to fix memory leak in ImageIO.write() and ImageIO.read()" Watch "help me to fix memory leak in ImageIO.write() and ImageIO.read()" New topic
Author

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

Nam Ha Minh
Ranch Hand

Joined: Oct 31, 2011
Posts: 498

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?


PM Me If You Want to get Urgent Help on Java Programming
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41880
    
  63
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?


Ping & DNS - my free Android networking tools app
Nam Ha Minh
Ranch Hand

Joined: Oct 31, 2011
Posts: 498

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
Marshal

Joined: Mar 22, 2005
Posts: 41880
    
  63
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

Joined: Oct 31, 2011
Posts: 498

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

Joined: Oct 27, 2005
Posts: 19696
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Nam Ha Minh
Ranch Hand

Joined: Oct 31, 2011
Posts: 498

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

Joined: Oct 31, 2011
Posts: 498

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
Marshal

Joined: Mar 22, 2005
Posts: 41880
    
  63
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

Joined: Oct 31, 2011
Posts: 498

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
Marshal

Joined: Mar 22, 2005
Posts: 41880
    
  63
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).
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: help me to fix memory leak in ImageIO.write() and ImageIO.read()