1

I have this simple code here in my application:

public static boolean makeImage(String path){
     BufferedImage inputImage = ImageIO.read(path);
}

I also tried this answer here but it doesn't matter how I initialize the BufferedImage, when the image is loaded in the BifferedImage, the RAM usage of the app goes up and never go down.
After the method is executed the ram is still occupied and when i call the method inputImage.flush() nothing changes.
It does not seem to be a ImageIO.read(path) problem cause initializing the image the other way has the same result.
How can i get rid of this BufferedImage from the RAM?

Edit: This line of code i reported here is the result of various tests in my application after isolating the memory increase to this buffered image. I am looking at the memory from task manager in windows and top command on linux. The memory does not grow until it throws OutOfMemoryException but remains stuck there as if the image is never released. This causes me problems because in my full application, various images are created over time and ram keeps growing. I looked at a heap dump and i see a large amount of byte[] elements but i am not sure how to interpret it because the dump i made when ram was low looks very similar to when i encountered the problem ( with over 1gb of ram occupied, normally is like 200 mb )

  • Try with the serial garbage collector(-XX:+UseSerialGC) – Jawad El Fou May 17 '22 at 23:34
  • 1
    From the [JavaDocs](https://docs.oracle.com/en/java/javase/16/docs/api/java.desktop/java/awt/Image.html#flush()) says *"BufferedImage objects leave the primary Raster which stores their pixels untouched, but flush any information cached about those pixels such as copies uploaded to the display hardware for accelerated blits."* - so flushing a `BufferedImage` might not release as much memory as you might think – MadProgrammer May 17 '22 at 23:41
  • But is it normal for the memory to still be there after the method is done executing? I started using the flush() method cause i saw memory still there after the method was done – Alessandro Valentino May 17 '22 at 23:45
  • Edited question with more details – Alessandro Valentino May 18 '22 at 00:04
  • We can't see that this is the cause of any "memory increase." If you get out of memory errors, you have a problem. But just memory increasing is normal because the garbage collector doesn't run until it needs to. – markspace May 18 '22 at 00:09
  • The JVM does not release memory back to the operating system. GC'd objects just go back onto the heap free list. So viewing memory usage via the Task Manager isn't going to tell you anything about the actual heap memory usage of the program. If you want the `BufferedImage` to completely disappear, let it be GC'd. – user207421 May 18 '22 at 00:21
  • From what i understand, then it should not be a problem, but then when i reach high amounts of memory, the application starts lagging a lot. I was convinced it was related, probably i was wrong. – Alessandro Valentino May 18 '22 at 00:27
  • Probably you weren't releasing the `BufferedImage` objects to garbage collection. Merely flushing them doesn't accomplish enough, as @MadProgrammer points out above. – user207421 May 18 '22 at 00:32

0 Answers0