JVM Monitor char数组内存使用情况 [英] JVM Monitor char array memory usage

查看:68
本文介绍了JVM Monitor char数组内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建班级的4096 x 4096数组.当我进行性能分析时,它表明char数组使用了最多的内存.我的课堂上没有任何char数组或字符串.

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.

这是我的Java类:

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


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

我的主要班级:

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);
    }
}

我正在使用JVM Monitor进行性能分析.为什么显示这个?

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

以下是它在Eclipse中运行的屏幕截图:

Here's a screenshot of it running in Eclipse:

推荐答案

从图像中看,您创建的MyClass实例在进行性能分析时甚至都没有运行.预期使用char[]是因为char数组返回String,而在类定义等中广泛使用了该数组.

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.

这篇关于JVM Monitor char数组内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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