Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Cecil Westerhof Newsgroups: comp.lang.java.programmer Subject: Re: Restricting the memory usage of a Java program Date: Sat, 13 Aug 2016 01:11:52 +0200 Organization: Decebal Computing Lines: 99 Message-ID: <87fuq9lgzr.fsf@Equus.decebal.nl> References: <8760r7m18z.fsf@Equus.decebal.nl> <87wpjml720.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: mx02.eternal-september.org; posting-host="12baf5125b19322b899c79c2e3878015"; logging-data="20383"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18/OXrDOC3FhDS6MFjs4Snwy3biD9aShyo=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:Dgk97Kz0VmaVmhL02XLEfcVrXi8= sha1:mC3zcFxy9Qo8N3SMgXN4Cb9gY1g= X-Homepage: http://www.decebal.nl/ Xref: csiph.com comp.lang.java.programmer:35280 On Friday 12 Aug 2016 15:23 CEST, Arne Vajhøj wrote: >> It is funny: I was always told that calling gc does not have an >> effect on memory and that it can have a negative impact on >> performance. But I saw a real difference. Maybe it is a good idea >> to call it once an hour? > > I doubt it. > > System.gc is not something that makes sense to call as > part of normal execution. > > But if one want to know how much heap space is in used > after GC has removed orphan objects, then it makes sense. > >> I added the following to my constructor: >> ses.scheduleAtFixedRate(new Runnable() { >> @Override >> public void run() { >> long divideValue = 1024 * 1024; >> long freeMemory = rt.freeMemory() / divideValue; >> long totalMemory = rt.totalMemory() / divideValue; >> >> giveMessage(String.format("Maximum to use %d", >> rt.maxMemory() / divideValue)); >> giveMessage(String.format("Before %d - %d = %d", >> totalMemory, freeMemory, >> totalMemory - freeMemory)); >> System.gc(); >> freeMemory = rt.freeMemory() / divideValue; >> totalMemory = rt.totalMemory() / divideValue; >> giveMessage(String.format("After %d - %d = %d", >> totalMemory, freeMemory, >> totalMemory - freeMemory)); >> } >> }, 15, 60, TimeUnit.MINUTES); >> >> The function giveMessage prints the input string with a time stamp >> to standard out. >> >> The first time I ran it with: >> }, 0, 15, TimeUnit.MINUTES); >> >> I ran it for little more as ten hours and with both I saw that the >> free memory was the same after the gc. So it is really useful to >> get an indication. > > Yes. It gives an indication of how much heap space is absolutely > required. > > If you set Xmx lower than that then you run out of memory. > > If you set Xmx just a little bit higher than that then GC run too > frequently and hurt throughput. > > If you set Xmx extremely higher than that then GC will run very > rarely and can cause pauses (because it is GC'ing too much). > > If you set Xmx sufficient higher then you should see good > throughput and minimal pausing. I am thinking about setting it to two to four times the max value of used after gc. It is of-course depending on the type of application and the way it is used. When the usage changes, the needed memory requirement can be changed also. >> Maybe I should rewrite it as an utility function, so I could use it >> in all my applications. > > I am just suggesting it for fine tuning what you want to set Xmx > to. Sort of a debugging tool. Not a production thing. That is where the whole exercise started for: getting a good value for Xmx. When setting this to 256 MB I see that the application when not really used uses about half of the memory as when Xmx not used. In that case it could be halved. But I should look for the case that the application is used of-course. ;-) By the way: there is now way to get the value for minMemory? >> Just to be sure: maxMemory cannot change during the run of a Java >> application? > > That is my understanding. See previous post. But it can, I just saw: freeAfter freeBefore maxMemory totalAfter totalBefore "185" "65" "239" "239" "242" "101" "96" "228" "155" "155" So first maxMemory was 228. An hour later total was 242 before gc. After gc total was 239 and maxMemory was changed from 228 to 239. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof