views:

926

answers:

3

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.

+1  A: 

For windows, you'll need to access WMI - this will get you going: http://j-integra.intrinsyc.com/support/com/doc/other_examples/WMI_Scripting_from_Java.htm

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.

Nate Bross
Martin McNulty
Yes different versions of windows support different WMI Classes. You'll need to adjust based on your target OS.
Nate Bross
A: 

Under linux you can use sysinfo(2). I don't think it's actually possible from pure java.

wds
Cheers - presumably I'd have to call that through JNI?
Martin McNulty
Yeah that's the C syscall. From a cmdline you can parse the results from free(1), I'm pretty sure there's an even better way but can't think of one right now. You could conceivably put all this in a shell script or package that users use to install your application too, instead of using jni.
wds
+3  A: 

If your intent is to tune memory settings for the JVM to use all the available physical memory, but not more, then you can take a look at the -XX:+AggressiveHeap parameter.

With it, you don't need to know the available memory. The JVM will scale it's parameters automatically.

Laurent Simon