JVM将内存发送回OS

编程入门 行业动态 更新时间:2024-10-27 10:21:54
本文介绍了JVM将内存发送回OS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个关于JVM内存管理的问题(至少对于SUN的内存管理问题)。

I have a question regarding the JVM memory management (at least for the SUN's one).

我想知道如何控制JVM发送的事实未使用的内存回到操作系统(在我的情况下是Windows)。

I would like to know how to control the fact that the JVM send the unused memory back to the OS (windows in my case).

我写了一个简单的java程序来说明我的期望。 使用-Dcom.sun.management.jmxremote选项运行它,以便您也可以使用jconsole监视堆。

I wrote a simple java program to illustrate what I expect. Run it with -Dcom.sun.management.jmxremote option so that you can also monitor the heap with jconsole for example.

使用以下程序:

package fr.brouillard.jvm; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.LinkedList; import java.util.List; public class MemoryFree { private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private List<byte[]> usedMemory = new LinkedList<byte[]>(); private int totalMB = 0; private int gcTimes = 0; public void allocate(int howManyMB) { usedMemory.add(new byte[howManyMB * 1024 * 1024]); totalMB += howManyMB; System.out.println(howManyMB + "MB allocated, total allocated: " + totalMB + "MB"); } public void free() { usedMemory.clear(); } public void gc() { System.gc(); System.out.println("GC " + (++gcTimes) + " times" ); } public void waitAnswer(String msg) { System.out.println("Press [enter]" + ((msg==null)?"":msg)); try { reader.readLine(); } catch (IOException e) { } } public static void main(String[] args) { MemoryFree mf = new MemoryFree(); mf.waitAnswer(" to allocate memory"); mf.allocate(20); mf.allocate(10); mf.allocate(15); mf.waitAnswer(" to free memory"); mf.free(); mf.waitAnswer(" to GC"); mf.gc(); mf.waitAnswer(" to GC"); mf.gc(); mf.waitAnswer(" to GC"); mf.gc(); mf.waitAnswer(" to GC"); mf.gc(); mf.waitAnswer(" to exit the program"); try { mf.reader.close(); } catch (IOException e) {} } }

一旦完成第一个GC(预期的内容),内部堆就会自由,但内存只会从第三个GC开始发送回操作系统。在第四个之后,将完整分配的内存发送回操作系统。

The internal heap is free once the first GC is done (what is expected) but the memory is only sent back to the OS starting from the third GC. After the fourth, the full allocated memory is sent back to the OS.

如何设置JVM来控制此行为? 实际上我的问题是我需要在服务器上运行几个CITRIX客户端会话,但我希望服务器上运行的JVM尽快释放内存(我只有很少的高消耗内存函数)我的应用程序)。

How to setup the JVM to control this behaviour? In fact my problem is that I need to run several CITRIX clients sessions on a server, but I would like the running JVMs on the server to free the memory as soon as possible (I have only few high consuming memory functions in my application).

如果无法控制此行为,我是否可以像这样让它增加操作系统虚拟内存并让操作系统按需使用它而不需要大性能问题。 例如,在4GB服务器上有10个java进程的1GB内存(堆中只有100MB实际分配的对象)当然会有足够的虚拟内存。

If this behaviour cannot be controlled, can I let it like this and increase instead the OS virtual memory and let the OS using it as it wants without big performance issues. For example, would there be issues to have 10 java process of 1GB memory (with only 100MB real allocated objects in the heap) on a 4GB server with enough virtual memory of course.

我猜其他人已经遇到过这样的问题。

I guess that other people already faced such questions/problems.

感谢您的帮助。

推荐答案

要控制堆返回到操作系统,从Java 5开始,使用 -XX:MaxHeapFreeRatio 选项,如上所述在调整中指南。

To control return of heap to the OS, from Java 5 onward, use the -XX:MaxHeapFreeRatio option, as described in the tuning guide.

如果您认为您的问题与这个,请指出如何。

If you feel your question is meaningfully different from this one, please point out how.

更多推荐

JVM将内存发送回OS

本文发布于:2023-11-11 11:09:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1578259.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内存   JVM   OS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!