java 创建大量的类放到集合中,清空集合,内存未被回收 | java | java 技术论坛-380玩彩网官网入口

背景:我在做一个数据的解析,会创建大量的类,里面会有长字符串和数组。我会把这些类存到数组,集合和hashmap中。

问题:解析完成后我会清空这些数组集合,但是内存并没有下降。导致多个数据进来,内存持续上涨。我试着在清空数组集合后,调用system.gc或者runtime的gc,但是没有用。

期望目标:清空数组后内存回落

以下是模拟程序

class mtest{
    private double[] m_dmatarray = new double[]{
            1.0, 0.0, 0.0, 0.0,
            0.0, 1.0, 0.0, 0.0,
            0.0, 0.0, 1.0, 0.0,
            0.0, 0.0, 0.0, 1.0};
    private string t0 = "f:\\test\\test_trunk\\objects_java\\com.test.modelconvert.threeddesigner.citygmlconvertor\\src\\main\\java\\com\\test\\modelconvert\\threeddesigner\\citygmlconvertor";
    private string t1 = "f:\\test\\test_trunk\\objects_java\\com.test.modelconvert.threeddesigner.citygmlconvertor\\src\\main\\java\\com\\test\\modelconvert\\threeddesigner\\citygmlconvertor";
    public void clear(){
        m_dmatarray = null;
        t0 = null;
        t1 = null;
    }
}
// 内存测试
public class memorytest {
    public static void main(string[] args) {
        system.out.println("1");
//        testmat();
        testhashmap();
        system.out.println("2");
//        runtime.getruntime().gc();
        try {
            thread.sleep(100000000);
        } catch (exception e) {}
    }
    public static void testarraylist(){
        arraylist<mtest> mat = new arraylist<>();
        for (int i = 0; i < 1000000; i) {
            mtest m = new mtest();
            mat.add(m);
        }
        mat.clear();
    }
    public static void testarray(){
        mtest[] mat = new mtest[1000000];
        for (int i = 0; i < 1000000; i) {
            mtest m = new mtest();
            mat[i] = m;
        }
        for (int i = 0; i < mat.length; i) {
            mat[i].clear();
            mat[i] = null;
        }
        mat = null;
    }
    public static void testhashmap(){
        hashmap<string, mtest> map = new hashmap<>();
        for (int i = 0; i < 100000; i) {
            mtest matrix = new mtest();
            map.put("i"  i, matrix);
        }
        map.clear();
    }
}

内存的测试结论:清空数组或者集合后,内存占用:
数组:216m
集合:209m
hashmap:36m
如果不添加到数组集合,只是在循环中创建类实例,最终内存只涨了4m
请问有没有什么方法使内存占用降下来

讨论数量: 1

file
对象s从线程栈打断指针后应该可以被回收。
尝试手动触发gc。

1个月前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
网站地图