views:

126

answers:

4

Free RAM: my target metric.
Java: my tool of choice.
???: a good way to get the former using the latter.

+1  A: 

This question has some good info you could find useful.

Sri Kumar
A: 

Probably like this, using Java Native Interface (JNI) :

    Kernel32 lib = (Kernel32) Native.loadLibrary ("kernel32",Kernel32.class);           
    Kernel32.MEMORYSTATUS mem = new Kernel32.MEMORYSTATUS ();
    lib.GetMem(mem);
    System.out.println ("Available physical memory " + mem.dwAvailPhys);
Adrian Faciu
What platform is this? `Kernel32` is certainly not a J2SE class.
Stephen C
@Stephen C, you are right, i've updated the post, it's for JNI
Adrian Faciu
A: 

Difficult to do without resorting to non-portable or native libraries.

Something like

Runtime.getRuntime().freeMemory()

will only return the memory available to the JVM, which may not be the same as the system-wide available memory.

This page provides a good rundown.

http://blog.codebeach.com/2008/02/determine-available-memory-in-java.html

Tim Kane
A: 

I know of two projects that have purchased JNIWrapper and have been happy with the result. Both Windows - based usage. When I embedded it on our current project, we wanted to know how much free ram was available when users launched our app (WebStart) since there were lots of performance complaints which were hard to investigate (we suspected RAM issues). JNIWrapper helps us to collect stats at startup about free ram, total and CPU etc so if a user group is complaining, we can check our stats to see if they have been given dodgy machines. Life saving.

jowierun
Since the question was on retrieving the size of available memory and not "How can I use JNI more easily", you might want to post an example of how the former could be accomplished...
Shog9
@shog9. The question is how can I get the free RAM available in Java. I have suggested the easiest way I have seen to achieve this.
jowierun
@jowierun: can you perhaps provide an example of what JNIWrapper would offer, by way of contrast with what Adrian wrote (which seems to be a bit light on context)? I know the question is vague ("free RAM" can mean an awful lot of things...) but if you have something specific in mind you might as well share it...
Shog9