Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java decrease memory usage

Tags:

java

memory

jvm

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)

like image 338
tsds Avatar asked Dec 05 '22 18:12

tsds


1 Answers

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

like image 101
tim_yates Avatar answered Dec 07 '22 07:12

tim_yates