JVM 将内存发送回操作系统 [英] JVM sending back memory to OS

查看:20
本文介绍了JVM 将内存发送回操作系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 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 个 1GB 内存的 java 进程(堆中只有 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 将内存发送回操作系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆