How do I find the physical memory size in Java?
Someone asked on Stack Overflow:
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.
I posted the following answer, which was chosen as the accepted answer and received 2 upvotes:
For windows, you’ll need to access WMI - this will get you going: Accessing Windows Management Instrumentation (WMI) from Java.
You’ll want to use this section of WMI: Win32_LogicalMemoryConfiguration.
There may be a pure java way of doing this, but I am unaware of it.
Notable comments
Nate (0 upvotes): Yes different versions of windows support different WMI Classes. You’ll need to adjust based on your target OS.
Originally posted on Stack Overflow — 2 upvotes (accepted answer). Licensed under CC BY-SA.