Get system information using Java

July 19th, 2009 in Java/J2EE. Add comment
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

If you are using jdk 1.5 or greater, it is possible to get OS level system information like number of processors and memory etc using the OperatingSystemMXBean interface.

import java.lang.management.ManagementFactory;
import com.sun.management.OperatingSystemMXBean;
  ....
  ....
OperatingSystemMXBean mxbean = (OperatingSystemMXBean)  ManagementFactory.getOperatingSystemMXBean();
System.out.println("Physical memory "+mxbean.getTotalPhysicalMemorySize()/(1024*1024));
System.out.println("swapsize  "+ mxbean.getTotalSwapSpaceSize()/(1024*1024));
System.out.println("no of processors "+mxbean.getAvailableProcessors());
....
....

See the api documentation for more.
A caveat here: getTotalPhysicalMemory() for RAM size did not return the correct value in some cases.

Tags:

Leave a Reply

Sponsors