I think I know the answer to this but I'm going to ask it anyway.
Is there any possible way to find out how much RAM is installed on a machine from java? I realize this is fairly system/OS specific and is most likely impossible to get using pure java but if there is a way (or an easy way to cheat for the major OS's Windows, linux, bsd). I would greatly appreciate hearing how.
I think you can't do this using purely Java code bcz the reasons you said. You could try to call a specific-system library through JNI (a .so in Linux or a .dll in Windows) to get this kind of information.
There is no method in the standard library to find out how much memory the machine has that your program is running on. However, class java.lang.Runtime does contain some useful methods: for example maxMemory(), which will return the maximum amount of memory that's available to the JVM.
Normally, the JVM will not allocate more memory than that maximum. You can set the maximum amount of memory on the command line with the -Xmx... option, for example:
What you put in there is the amount of memory the application actually needs. You should determine that number before you distribute the application.
And it is possible to put in -Xmx... a number that is larger than the machine's physical memory (I have done it). That just causes the JVM to use virtual memory, which is not a bad thing. Okay, it's not a good thing, but at least not a very bad thing.
Well how much the application needs changes based on the size of input.
Sure, there's nothing unusual about that. But I don't see how it helps to know how much physical memory is available. No matter what the answer is, you don't have infinite memory, so if you have an application that can potentially use infinite memory (again, nothing unusual about that) then you need to write it so that it deals with that problem somehow. And then pick a non-infinite number that you can make it work within.
It's pretty standard for packaged software to say "Requires X MB memory to run". Seems to me that Java programs should be entitled to say the same thing.
And the last time I put a number there that was equivalent to the physical ram on my machine it crashed either the application or the machine
I have 256 MB of physical memory on my home system and I run a Java application with -Xmx400M in the command line, with no problems.
Well - the amount of needed memory depends on input, not on the available memory, and users might change the available memory by buying RAM or resizing swap, so I would go for a reasonable default value, and a well documentation of how and when to change that value, especially in an catch(OutOfMemoryException oome)-block.