We package a software with -Xmx512M, but then when people call and say "my program freezes up" and we investigate further and find they are collecting a ton of data we know the reason is that they have running too much stuff on the software and need more memory, so we direct them to increase the -Xmx to 1G or 2G or something more reasonable.
We set it to -Xmx512M because we dont know if the PC has 2GB or 8GB, we cant just set it to 2G because then there is a risk we are consuming too much of the system's memory. 512M works for 99% of the users, but this seems so kludgy.......
How do people set memory usage dynamically in java? It would be great if we could tell java "try to limit use 512M but if you absolutely must, go up to 2GB" and if the PC has only 1 GB at least it will run, and then throw some out of memory error later. But the flags dont work like that, I think this is effectively what -Xmx means but if you set -Xmx to 2G and the user only has 1G then the application wont even start. It would be great if java would at least start the JVM and crash later if it gets to 1.5G and the user only has 1.499G
This has to be a common problem? What are people doing to workaround this?
Contrary to your theory that it's a common problem, I doubt that there are many people distributing Java programs with large memory requirements to a heterogeneous collection of machines with different memory sizes. I think the answer is that people don't set memory usage dynamically in Java.
...or design it so it doesn't need that much memory in the first place. Especially for an app that 'users' can routinely cause it to use more memory than available I would wonder how you keep it from running out even with 2G.