I have a piece of code that caclulates the total RAM in my system [not Java VM Heap Size].
It is as follows:
OperatingSystemMXBean operatingSystemMXBean =
ManagementFactory.getOperatingSystemMXBean();
Method m = operatingSystemMXBean.getClass().
getDeclaredMethod("getTotalPhysicalMemorySize");
m.setAccessible(true);
Object value = m.invoke(operatingSystemMXBean);
if (value != null)
{
System.out.println(value);
}
The problem with the above piece of code is that it gives me incorrect values sometimes. For example in a system with 8 GB of RAM, using a 32 bit JVM, it shows 4 GB. I guess it is because the JVM is 32 bit and using 32 bits 4 GB is the maximum that it can address.
Can anyone suggest ways/workarounds for this. Basically how do we get the total RAM in a system using a 32 bit JVM.
Full details of my System are as follows:
OS : Windows 7
CPU : Intel i7
JDK : Sun Java 1.6 Update 21, 32 bit
RAM : 8 GB