ThelmaJay
ThelmaJay

Reputation: 163

Exception in thread "Thread-8" java.lang.OutOfMemoryError: Java heap space

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.

Upvotes: 1

Views: 15650

Answers (2)

Ghazanfar
Ghazanfar

Reputation: 1469

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.

Upvotes: 0

GhostCat
GhostCat

Reputation: 140553

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.

Upvotes: 1

Related Questions