How do I find the physical memory size in Java?

You can use the following Java code to query the physical memory:

com.sun.management.OperatingSystemMXBean os = (com.sun.management.OperatingSystemMXBean)
     java.lang.management.ManagementFactory.getOperatingSystemMXBean();
long physicalMemorySize = os.getTotalPhysicalMemorySize();

But the package com.sun.management is optional and need not be available on every platform.


What about local JMX and MBeans? Try this:

MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
Object attribute = mBeanServer.getAttribute(new ObjectName("java.lang","type","OperatingSystem"), "TotalPhysicalMemorySize");
System.out.println("Total memory: "+ attribute.toString() +" B");

You can access many usefull info this way.