0
Exception in thread "Image Animator 3" java.lang.OutOfMemoryError: Java heap space
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at sun.awt.image.GifImageDecoder.readBytes(Unknown Source)
at sun.awt.image.GifImageDecoder.parseImage(Native Method)
at sun.awt.image.GifImageDecoder.readImage(Unknown Source)
at sun.awt.image.GifImageDecoder.produceImage(Unknown Source)
at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)
at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)
at sun.awt.image.ImageFetcher.run(Unknown Source)

what's wrong? In java, the gif file normally runs in the java program, and after a while, this error message appears.

g_know
  • 35
  • 1
  • 5

2 Answers2

1

As your error message suggests, you have run out of memory on the heap. You can allow the JVM to use more of your system RAM for heap space using the JVM argument: -Xmx, for example: java -Xmx1g MyProgram to run it with 1GB of heap space. However, the following limitations apply:

  • You cannot provide more heap space than you have physical RAM on your machine (somewhat intuitively)
  • There is a limit of 4gb on 32-bit systems
  • You might want to consider if it is actually a programming error. Certain situations most certainly do require large amounts of heap, but it is definitely worth checking for memory leaks first, before expanding the heap. In fact, reducing the heap space can be a very useful way to diagnose memory leaks. Read this article for a guide on how to find and remove memory leaks: https://www.baeldung.com/java-memory-leaks
cameron1024
  • 9,083
  • 2
  • 16
  • 36
0

This has to do with the amount of RAM Java has access to by default. Usually for projects that involve image processing it's a resource you'd want to have available. Add this to your run configurations. -Xmx2048m The number in the middle there represents the amount of memory you want to allow java to use, and the m represents mega-bytes. You can replace it with g for giga-bytes if need be. Also remember to close any streams that are no longer in use.

  • Where should I add -Xmx2048m in the Run configuration? I'm using the Eclipse neon version. Which should I put in the main, arguments, jre, classpath, source, environment, or common? Not long after I started learning Java, so I am not good at it. – g_know Jan 27 '20 at 03:54
  • @g_know Add it to the arguments. – Nathaniel Blanquel Jan 27 '20 at 04:02
  • I added -Xmx8g to Arguments-Program aggregates. The output of the error has been delayed a lot, but the error still occurs. Should I add it to VM arguments? Or do I have to go into Variables and add it?It's too hard. – g_know Jan 27 '20 at 05:01
  • Can you maybe add some code to your question. There might be a bug that is causing an accumulation of memory. – Nathaniel Blanquel Jan 27 '20 at 05:02
  • Luckily, the error has been corrected! Thank you. – g_know Jan 27 '20 at 05:11