Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #35280

Re: Restricting the memory usage of a Java program

From Cecil Westerhof <Cecil@decebal.nl>
Newsgroups comp.lang.java.programmer
Subject Re: Restricting the memory usage of a Java program
Date 2016-08-13 01:11 +0200
Organization Decebal Computing
Message-ID <87fuq9lgzr.fsf@Equus.decebal.nl> (permalink)
References <8760r7m18z.fsf@Equus.decebal.nl> <noisp2$12e7$1@gioia.aioe.org> <87wpjml720.fsf@Equus.decebal.nl> <nokil0$1ce1$1@gioia.aioe.org>

Show all headers | View raw


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

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Restricting the memory usage of a Java program Cecil Westerhof <Cecil@decebal.nl> - 2016-08-11 23:42 +0200
  Re: Restricting the memory usage of a Java program Arne Vajhøj <arne@vajhoej.dk> - 2016-08-11 18:04 -0400
    Re: Restricting the memory usage of a Java program Cecil Westerhof <Cecil@decebal.nl> - 2016-08-12 10:34 +0200
      Re: Restricting the memory usage of a Java program Martin Gregorie <martin@address-in-sig.invalid> - 2016-08-12 10:08 +0000
      Re: Restricting the memory usage of a Java program Arne Vajhøj <arne@vajhoej.dk> - 2016-08-12 09:23 -0400
        Re: Restricting the memory usage of a Java program Cecil Westerhof <Cecil@decebal.nl> - 2016-08-13 01:11 +0200
          Re: Restricting the memory usage of a Java program Cecil Westerhof <Cecil@decebal.nl> - 2016-08-13 02:52 +0200
          Re: Restricting the memory usage of a Java program Arne Vajhøj <arne@vajhoej.dk> - 2016-08-12 21:05 -0400
  Re: Restricting the memory usage of a Java program Arne Vajhøj <arne@vajhoej.dk> - 2016-08-11 18:24 -0400

csiph-web