I found out that if my application has much free memory available it uses more memory than it needs. I heard that this behavior is common for all Java applications but it's not acceptable for me.
So the question is how can I force Java to use less memory?
I know that I can call System.gc()
manually every few seconds, but is it alright to do so and are there any other methods?
P.S. my application is updated regularly on production server and I need to monitor memory usage for some leaks and so on (generally there are no leaks but if one appears I should see it as fast as possible), and also I need to predict the time when I should upgrade my hardware. All these things are very hard to do if jvm unpredictably changes its memory hunger. So the only thing I want is to force jvm to be predictable in memory usage (if number of users doubles memory should doubles too)
You can tell it to use less memory by calling java with the -Xmx
parameter ie:
java -Xmx128M xxx
will run with a max of 128Mb (by default in Java 6 this is set to "Smaller of 1/4th of the physical memory or 1GB")
Don't call System.gc()
all the time...it's your time you'll be wasting ;-)
Also, why are you worrying about this? When java gets near the maximum memory it's allowed, it will do a GC sweep anyways
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With