11

I am using Eclipse. The problem is my application crashes if the allocated memory is less then 512MB. Now is there anyway to check the available memory for a program before starting heavy memory exhaustive processing? For example, I know we can check available JVM heap size as:

long heapSize = Runtime.getRuntime().totalMemory();
System.out.println("Heap Size = " + heapSize);

Problem is, this gives the JVM heap size. Even increasing it does not work using Eclipse. In Eclipse, if I change the VM arguments then it works. However the printout from above statements is always the same. Is there any command through which I can exactly know how much memory I am allocated for a particular application?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Johnydep
  • 6,027
  • 20
  • 57
  • 74

3 Answers3

22

You could use JMX to collect the usage of heap memory at runtime.


Code Example:

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryPoolMXBean;
import java.lang.management.MemoryType;
import java.lang.management.MemoryUsage;

for (MemoryPoolMXBean mpBean: ManagementFactory.getMemoryPoolMXBeans()) {
    if (mpBean.getType() == MemoryType.HEAP) {
        System.out.printf(
            "Name: %s: %s\n",
            mpBean.getName(), mpBean.getUsage()
        );
    }
}

Output Example:

Name: Eden Space: init = 6619136(6464K) used = 3754304(3666K) committed = 6619136(6464K) max = 186253312(181888K)
Name: Survivor Space: init = 786432(768K) used = 0(0K) committed = 786432(768K) max = 23265280(22720K)
Name: Tenured Gen: init = 16449536(16064K) used = 0(0K) committed = 16449536(16064K) max = 465567744(454656K)

If your have question about "Eden Space" or "Survivor Space", check out How is the java memory pool divided

Community
  • 1
  • 1
Mike Lue
  • 839
  • 4
  • 8
  • Yess but it dosent help really. I just want to know total allocated memory to my application by JVM or something like this. From configuration i know if it is less then 512MB, it dosent' work. But how can i check this pragmatically because once this app is packaged, if it is crashing at run time, there should be a mecahnism to prompt user about the nature of error. In worse case i will catch exception and prompt the user to increase memory size, but that would be after the crash, but not before the start. Unfortuanatelly it takes about 30 mins before it crashes and it would be a waste of time. – Johnydep Jun 28 '11 at 14:22
  • Did you set your JAVA_OPTS or CATALINA_OPTS? What about `-Xmx` (max heap size) parameter? Maybe it crashes because it is out of memory at some point? What do the log files say, any outOfMemory errors? – Danijel Jul 24 '13 at 14:26
2

maybe an useful update using Java 17 to 19: After several trials with getRuntime() and old/Eden/Survivor Space I came back to use getRuntime() which seem to be 'faithful' now:

With Java 17-19 therefore I propose to use the heap size functions of getRuntime():

Runtime env = Runtime.getRuntime();

System.out.println("Max Heap Size = maxMemory() = " + env.maxMemory()); //max heap size from -Xmx, i.e. is constant during runtime
System.out.println("Current Heap Size = totalMemory() = " +  env.totalMemory()); //currently assigned  heap
System.out.println("Available in Current Heap = freeMemory() = " + env.freeMemory()); //current heap will extend if no more freeMemory to a maximum of maxMemory
System.out.println("Currently Used Heap = " + (env.totalMemory()-env.freeMemory()) );
System.out.println("Unassigned Heap = " + (env.maxMemory()-env.totalMemory()));
System.out.println("Currently Totally Available Heap Space = "+ ((env.maxMemory()-env.totalMemory()) + env.freeMemory()) ); //available=unassigned + free
Ingo
  • 605
  • 6
  • 10
0

totalMemory() returns the current total memory used, and not the total available memory. To query the maximum available memory then use maxMemory() (aka java -Xmx option):

long heapSize = Runtime.getRuntime().maxMemory();
System.out.println("Heap Size = " + heapSize);
Rob Oxspring
  • 2,835
  • 1
  • 22
  • 28