Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #35271
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Restricting the memory usage of a Java program |
| Date | 2016-08-12 09:23 -0400 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <nokil0$1ce1$1@gioia.aioe.org> (permalink) |
| References | <8760r7m18z.fsf@Equus.decebal.nl> <noisp2$12e7$1@gioia.aioe.org> <87wpjml720.fsf@Equus.decebal.nl> |
On 8/12/2016 4:34 AM, Cecil Westerhof wrote:
> On Friday 12 Aug 2016 00:04 CEST, Arne Vajhøj wrote:
>> On 8/11/2016 5:42 PM, Cecil Westerhof wrote:
>>> The values I use (128 MB and 256 MB) are just values I kind of
>>> randomly choose. Is there a way to find ‘optimal’ values?
>>
>> System.gc();
>> Runtime rt = Runtime.getRuntime();
>> long actual_value = rt.totalMemory() - rt.freeMemory();
>> long good_value = SOME_MAGIC_FACTOR * actual_value;
>
> 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.
> 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.
> Just to be sure: maxMemory cannot change during the run of a Java
> application?
That is my understanding. See previous post.
> There is 28 MB difference between -Xmx and what maxMemory returns.
> Where did it go?
I don't know.
Arne
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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