简体   繁体   中英

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

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.

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.

  • 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 eg it will make the young and tenured spaces 1:8 ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM