Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting OS memory size from java

Are there any way to get the size of the total memory on the operating system from java? Using

Runtime.getRuntime().maxMemory()

returns the allowed memory for the JVM, not of the operating system. Does anyone have a way to obtain this (from java code)?

like image 592
soren.enemaerke Avatar asked Dec 02 '10 13:12

soren.enemaerke


1 Answers

com.sun.management.OperatingSystemMXBean bean =
  (com.sun.management.OperatingSystemMXBean)
    java.lang.management.ManagementFactory.getOperatingSystemMXBean();
long max = bean.getTotalPhysicalMemorySize();

returns available RAM size for JVM (limited by 32bit), not the heap size.

like image 149
khachik Avatar answered Nov 08 '22 13:11

khachik