I'm writing an installer that will tune the configuration of the product for the particular hardware on which it will be run. In particular, I want to determine how much physical RAM is installed in the system so I can estimate how much memory to allocate to the product when it runs.
Ideally, I'd like to do this in a platform-independent, pure Java way, as the installer will need to run on several different platforms, but in case this isn't possible, solutions for Windows are preferred as that's the most common deployment platform.
In this case, it's safe to assume that the product will be the only/main application running on the box, so I don't have to worry about squeezing anyone else out. I don't want to over-allocate as this, in our experience, can hurt performance.
How to get total Memory in Java. You can use Runtime. getRuntime. totalMemory() to get total memory from JVM which represents the current heap size of JVM which is a combination of used memory currently occupied by objects and free memory available for new objects.
totalMemory() - Runtime. getRuntime(). freeMemory()) / 1024);
Java was initially designed as a safe, managed environment. Nevertheless, Java HotSpot VM contains a “backdoor” that provides a number of low-level operations to manipulate memory and threads directly. This backdoor – sun.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With