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
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.
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).
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.
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.
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,
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.
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
A few things you should know.
-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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With