JVM Monitor char array memory usage

有些话、适合烂在心里 提交于 2019-12-11 01:58:49

问题


I'm creating a 4096 x 4096 array of my class. When I'm profiling, it's showing that the most memory is used by char arrays. I don't have any char arrays or strings in my class.

Here is my Java class:

public class MyClass {
    private int id;
    private boolean flag;


    public MyClass() {
        id = 0;
        flag = false;
    }
}

And my Main class:

public class Main {

    public static void main(String[] args) {
        MyClass[][] objects = new MyClass[4096][4096];

        for (int i = 0;i<4096;i++) {
            for(int j = 0;j<4096;j++) {
                objects[i][j] = new MyClass();
            }
        }

        while(true);
    }
}

I'm using JVM Monitor for profiling. Why is it showing this?

Here's a screenshot of it running in Eclipse:


回答1:


From your image, it looks like the instances of MyClass you created aren't even live when you're doing the profiling. The char[] usage is expected since char arrays back Strings, which are used extensively in class definitions, etc.

It could be that your array is getting garbage collected since it's not used. Try adding a line after the sleep that uses the array, and then try to profile again. Example:

    MyClass[][] objects = new MyClass[4096][4096];
    for(int i = 0;i<4096;i++)
    {
        for(int j = 0;j<4096;j++)
        {
            objects[i][j] = new MyClass();

        }
    }

    Thread.sleep(1000000);
    System.out.println(objects[10][40]);

This ensures that the array is not eligible for garbage collection until you've had a chance to profile it.




回答2:


make sure you run System.gc() or similar before taking your heap dump. Else you will see a lot of objects that are no longer referenced in your sample.



来源:https://stackoverflow.com/questions/21908614/jvm-monitor-char-array-memory-usage

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!