Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get heap outOfMemory exception?

I'm trying to allocate a large matrix (around 10GB). I'm working on 64 bit machine with a 64 bit JVM. My process then should have 2^64 bytes available and I've set the JVM heap size to be 128G (I have 16GB of RAM in my machine if that matters). My understanding was that I should get the memory from the OS and that unneeded matrix cells would be swapped out by the OS. However I'm getting the above exception.

Edit:

This is how I'm defining the matrix:

Jama.Matrix A = new Matrix(num_words, num_documents);

Where num_words is approximately 100k and num_documents is approximately 35k. Also worth mentioning that the type is double

Edit2:

Relevant flags:

-Xms40m
-Xmx128g
-d64
like image 444
Shmoopy Avatar asked Jan 26 '14 07:01

Shmoopy


People also ask

How can Outofmemory exception be resolved?

1) An easy way to solve OutOfMemoryError in java is to increase the maximum heap size by using JVM options "-Xmx512M", this will immediately solve your OutOfMemoryError.

How do I fix a heap space error?

The java. lang. OutOfMemoryError: Java heap space error occurs when it attempts to add more data into the heap space area, but there is not enough room for it. The solution to fix this problem is to increase the heap space(Default value maybe 128 MB).

What is Outofmemory error?

OutOfMemoryError is a runtime error in Java which occurs when the Java Virtual Machine (JVM) is unable to allocate an object due to insufficient space in the Java heap. The Java Garbage Collector (GC) cannot free up the space required for a new object, which causes a java.

How can we avoid OutOfMemoryError in Java?

Prevention: If MaxMetaSpaceSize, has been set on the command line, increase its value. MetaSpace is allocated from the same address spaces as the Java heap. Reducing the size of the Java heap will make more space available for MetaSpace.


3 Answers

JVM works as native process, in this respect: JVM requests memory from OS that can allocate it either in RAM in swap.

The memory you can allocate in java does not depend on your RAM but on command line option -Xmx you specify when you are running your JVM. If it is not enough memory in RAM JVM receives it from swap and (I believe) even does not know about that.

However,

  • in some opearating system (Windows XP) the limit is 4G.
  • the heap in the JVM is not designed to run off disk.
  • swap is limited.

If you need to work with large data you need to work with BigMemory products (EhCache or Terracotta).

Finally, run jvisualvm or with the -verbose:gc prameter to see the heap allocation.

like image 54
venergiac Avatar answered Oct 08 '22 00:10

venergiac


Here some description:

Xms -> the init memory that should be allocated in the start up in MB.
Xmx -> the Max amount of memory that your application can get in MB e.g Xmx2048m.
-XX:MaxNewSize= -> the max S1 and S2 memory size
-XX:NewSize= -> init S1 and S2 size

so in your case if you want to allocate memory as much as you can, so you need to say for example 16 * 1024 = 16384 or 16g and the -XX:MaxNewSize= and -XX:NewSize= set it by 40% of your Xmx

like image 29
Salah Avatar answered Oct 08 '22 00:10

Salah


A few things you should know.

  • Managed memory doesn't swap very well. In fact it usually results in your application dying or your OS locking up (esp windows you have to power cycle if even 10% of your heap is swapped)
  • The heap is divided into multiple regions and it will not resize always as you might expect. Normally it will scale these regions together e.g. it will make the young and tenured spaces 1:8 i.e. you cannot easily grab almost all the heap at once. I would try a 192 GB maximum heap.
  • Most advanced processes support 48-bit address spaces or 256 TB of virtual memory and only 40-bit of actual memory (1 TB per processors)
  • the -d64 only works on Solaris.

In short you need about 256 GB of main memory and a large heap you really consider what you are doing. Accessing memory randomly is up to one million times faster than accessing disk space and this means not only is it one million times slower, but it is unlikely to work at all.

What you can do is use off heap memory, and if you really know what you are doing, you can make this work and be perhaps only 100x slower than having the memory you need. esp if you use a fast SSD for swap or your matrix is sparse is just the right ways.

like image 1
Peter Lawrey Avatar answered Oct 08 '22 01:10

Peter Lawrey