本文介绍了Java在对象和垃圾收集取消分配后仍然使用系统内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行JVM 1.5.0(Mac OS X Default),并且正在监视活动监视器中的Java程序。我有以下内容:

$ $ p $ import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;

public class MemoryTest {

public static void memoryUsage(){
System.out.println(
Runtime.getRuntime()。totalMemory() -
Runtime.getRuntime()。freeMemory()
);

$ b $ public static void main(String [] args)throws IOException {

/ *创建列表* /
ArrayList< Date> list = new ArrayList< Date>();

填充大量数据* /
(int i = 0; i list.add(new Date()) ;
} //系统显示〜164 MB实际使用中

/ *清除它* /
memoryUsage(); //大约154 MB
list.clear();
list = null;
System.gc();
memoryUsage(); //约151 KB,垃圾收集器工作

//系统仍显示正在使用的物理内存为164 MB。
System.out.println(按回车结束...);
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in)
);
br.readLine();
}

}

那么,即使垃圾收集器似乎工作得很好,为什么物理内存不会被释放?

解决方案

许多JVM永远不会返回内存到操作系统。无论它是否是特定于实现的。对于那些没有启动的内存限制,通常通过-Xmx标志指定的内存限制是为其他应用程序保留内存的主要方式。



我有很难找到关于这个主题的文档,但是关于Sun的Java的确实解决了这个问题,这表明在正确的条件下,如果使用了正确的收集器,堆将收缩 - 默认情况下,如果70%以上的堆是空闲的,它将收缩,因此只有40%自由。控制它们的命令行选项是 -XX:MinHeapFreeRatio -XX:MaxHeapFreeRatio


I am running JVM 1.5.0 (Mac OS X Default), and I am monitoring my Java program in the Activity Monitor. I have the following:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;

public class MemoryTest {

public static void memoryUsage() {
 System.out.println(
     Runtime.getRuntime().totalMemory() - 
     Runtime.getRuntime().freeMemory()
 );
}

public static void main( String[] args ) throws IOException {

    /* create a list */
    ArrayList<Date> list = new ArrayList<Date>();

    /* fill it with lots of data */
    for ( int i = 0; i < 5000000; i++ ) {
        list.add( new Date() );
    } // systems shows ~164 MB of physical being used

    /* clear it */
    memoryUsage();      //  about 154 MB
    list.clear();
    list = null;
    System.gc();
    memoryUsage();      //  about 151 KB, garbage collector worked

    // system still shows 164 MB of physical being used.
    System.out.println("Press enter to end...");
    BufferedReader br = new BufferedReader( 
            new InputStreamReader( System.in )
            );
    br.readLine();
}

}

So why doesn't the physical memory get freed even though the garbage collector seems to work just fine?

解决方案

Many JVMs never return memory to the operating system. Whether it does so or not is implementation-specific. For those that don't, the memory limits specified at startup, usually through the -Xmx flag, are the primary means to reserve memory for other applications.

I am having a hard time finding documentation on this subject, but the garbage collector documentation for Sun's Java 5 does address this, suggesting that under the right conditions, the heap will shrink if the correct collector is used—by default, if more that 70% of the heap is free, it will shrink so that only 40% is free. The command line options to control these are -XX:MinHeapFreeRatio and -XX:MaxHeapFreeRatio.

这篇关于Java在对象和垃圾收集取消分配后仍然使用系统内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 06:55