Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is java heap space dictated by RAM or hard drive?

Tags:

java

memory

I'm performing a huge calculation and I'm getting a "out of heap space" error. What I'm wondering is if Java does paging automatically, so that maybe my hard drive space can be used when performing this calculation? If it doesn't do it automatically, how do I turn this option on?

like image 215
BitAndBytes Avatar asked Mar 11 '11 17:03

BitAndBytes


People also ask

Is heap allocated in RAM?

Stored in computer RAM just like the stack. In C++, variables on the heap must be destroyed manually and never fall out of scope. The data is freed with delete , delete[] , or free .

How is Java heap memory calculated?

Use this code: // Get current size of heap in bytes long heapSize = Runtime. getRuntime(). totalMemory(); // Get maximum size of heap in bytes.

Is heap space same as RAM?

The RAM is the physical memory of your computer. Heap memory is the (logical) memory reserved for the heap.


3 Answers

The virtual memory manager of the operating system, not the Java Virtual Machine (JVM), decides when to eject memory pages and write them to the hard disk.

As earlier answers have mentioned, the JVM stores the heap space entirely in resident memory and most JVM implementations allow the user to specify the maximum heap space. For example, the OpenJDK allows you to set the maximum heap size using the -Xmx<size> option. Run java -X to see this and other non-standard OpenJDK command-line options.

like image 42
Derek Mahar Avatar answered Oct 26 '22 02:10

Derek Mahar


The Java heap lives in RAM (ignoring virtual memory :). You can change the default initial heap size and maximum heap size with the -Xms and -Xmx VM args, respectively.

The old generation, default heap size can be overridden by using the -Xms and -Xmx switches to specify the initial and maximum sizes respectively:

java -Xms <initial size> -Xmx <maximum size> program

For example:

java -Xms128m -Xmx512m application

How much RAM does your machine have? Just what sort of a calculation is this that you think you need more heap than that?

like image 84
Matt Ball Avatar answered Oct 26 '22 01:10

Matt Ball


The size of the heap can determined however the JVM wants to. For most, it's via the -Xmx command line argument, with varying defaults. Chances are you want something like:

java -Xmx2048M foo.bar.MyProgram
like image 38
Jon Skeet Avatar answered Oct 26 '22 00:10

Jon Skeet