1
Exception in thread "Thread-8" java.lang.OutOfMemoryError: Java heap space
Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "Thread-8"
Exception in thread "Thread-6" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-1" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-3" java.lang.OutOfMemoryError: Java heap space

I'm having this exception. Could anyone please help me understand why this exception happens?

The exception doesn't provide where in my code this happens.

EDIT: From all the answers I received, I realized I needed some kind of Java Profiler, to help me detect where the memory leakage is. Because I am using NetBeans to develop my application, I decided to use its embedded profiler.

@MuhammadGhazanfar gave me this very helpfull link: Best way to profile memory usage in a Java application?

Thank you for your time and all your help.

trincot
  • 317,000
  • 35
  • 244
  • 286
ThelmaJay
  • 165
  • 1
  • 3
  • 9

2 Answers2

1

It doesn't matter where it happens: it just happens at a place where some memory is required; and none is found to be left.

Basically you want to learn how to use a Java profiler the profile the memory usage of your application; in order to either figure the memory leak your are dealing with; or to understand how much memory is actually required; could simply be that your application needs more memory than the default settings when running "java" account for.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I did some research but none of the post I've found helped me. From one of those post I added -Xmx1024m -Xms512m but did not helped me. I know the problem is leakage I just wanted to understand where. My intention when I posted this question it was to see if anyone had already seen it, and maybe knew what type of code could create such exception. I did not mean to throw a question just for the sake of it. I needed directions because I don't know where to look. Your advice is to use Java Profiler. I will look on the internet for some more info about it. Thank you. – ThelmaJay Oct 23 '15 at 15:27
  • Well, "debugging java memory leaks" gave me some good links out of the box. But OK, sometimes one really needs SO to get on the right track ... – GhostCat Oct 23 '15 at 15:29
0

Going by the Java docs, the OutOfMemoryError is,

Thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector.


Why does this happen ? Your application is simply using too much memory.

What can you do to fix it ? Well, you can try some of the thing listed below and look here and here for more

  • Try to use the Singleton pattern wherever you can.
  • Avoid using large arrays. It is easier to fit a linked list in memory.
  • Don't make an object when you don't need one.
  • Use the -Xmx option with java to set a higher heap size.
Community
  • 1
  • 1
Ghazanfar
  • 1,419
  • 13
  • 21