1

When I run my application of hyprgraph construction I get this exception

    java.lang.OutOfMemoryError: Java heap space

I use ubuntu 12.04 The output of the command free -m few minute before the exception is :

noura@noura-Inspiron-N5050:~$ free -m
         total       used       free     shared    buffers     cached
Mem:          3939       2672       1266          0         80        610
-/+ buffers/cache:       1981       1957
Swap:         7328         65       7263

I note that the swap is not use enough It is possible to utilize it to avoid this Exception

nawara
  • 1,157
  • 3
  • 24
  • 49

3 Answers3

3

The java virtual machine imposes some limits, you can increase the stack and heap limit when launching the java command.

Apparently you have to pass something like

java -Xmx6g myprogram

where 6g should limit your heap to 6 GiB

Increase heap size in Java

Community
  • 1
  • 1
LtWorf
  • 7,286
  • 6
  • 31
  • 45
1

The OutOfMemory error can occur due to multiple reason (e.g. Heap exhaustion due to memory leak in program, your program needs more memory to run, exhaustion of native memory etc.). The best way to find out the reason for OutOfMemory is to make JVM generate a heapdump when it goes OutOfMemory.You can use -XX:+HeapDumpOnOutOfMemoryError option to achieve that.

Once the heapdump is generated you can use eclipse memory analyzer to analyze the heap.

http://www.eclipse.org/mat/

The analysis with ascertain whether you have a memory leak or your program really needs additional memory. If additional memory is required you can configure the same using -Xmx option.

dkaustubh
  • 444
  • 2
  • 15
0

The default limit is 1/4 of main memory. If you increase your maximum you can use more of it.

You should note:

  • using swap space for a JVM application is a very bad idea as the GC will kill your process if not your machine.
  • buy more memory may be worth considering, you can buy 64 GB for $300.

BTW I bought a PC for my 8 year old son which he built himself. ;) It cost £240 and it has 8 GB of memory.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Why would swap be a bad idea? – LtWorf Mar 28 '14 at 10:51
  • @LtWorf The GC attempts to access it all at least twice. Once you start swapping the heap the GC can force the oldest pages to swap out to pull in a part it needs to access and eventually shuffle all the heap to a portion of disk a couple of times. On Linux you can kill the process if you are lucky and on Windows you often need to power cycle the box to get control back. – Peter Lawrey Mar 29 '14 at 06:58
  • So all this is due to the GC being implemented poorly I suppose. – LtWorf Apr 01 '14 at 11:44
  • @LtWorf It assumes you will not over commit the system, which may be a poor assumption. You can always make it smaller to suit your system. – Peter Lawrey Apr 01 '14 at 16:00