java 获取内存(RAM)上的地图大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11329941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:41:11  来源:igfitidea点击:

Getting Map size on memory(RAM)

javamemorymap

提问by NIVESH SENGAR

I am using a HashMap to cache some important data when my application starts.
This cache sometime grows and then reduced. I want to know how much memory it is taking on RAM.
Is it possible to get memory taken by a map in KB? Is there any API?

当我的应用程序启动时,我正在使用 HashMap 来缓存一些重要数据。
这个缓存有时会增长然后减少。我想知道它占用了多少内存。
是否可以以 KB 为单位获取地图占用的内存?有没有API?

回答by marcolopes

i'm using this method to estimate the MAP size:

我正在使用这种方法来估计 MAP 大小:

public static void size(Map map) {
    try{
        System.out.println("Index Size: " + map.size());
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        ObjectOutputStream oos=new ObjectOutputStream(baos);
        oos.writeObject(map);
        oos.close();
        System.out.println("Data Size: " + baos.size());
    }catch(IOException e){
        e.printStackTrace();
    }
}

回答by AValchev

There is a good tool for this purpose: ClassMexer

为此有一个很好的工具:ClassMexer

You can estimate the memory of the HashMap, including referenced objects by:

您可以通过以下方式估算 HashMap 的内存,包括引用的对象:

long noBytes = MemoryUtil.deepMemoryUsageOf(myCacheMap);

long noBytes = MemoryUtil.deepMemoryUsageOf(myCacheMap);

回答by duffymo

You realize, of course, that your Mapholds references to objects that live out on the heap, not the objects themselves. So it's meaningless to ask how much memory the Mapis consuming without chasing down the entire reference tree to add up the bytes for the keys and the values.

当然,您会意识到,您Map持有的是对存在于堆中的对象的引用,而不是对象本身。因此,在Map不追踪整个参考树以将键和值的字节相加的情况下询问消耗了多少内存是没有意义的。

The object doesn't do it; there's no way to accomplish it.

对象不这样做;没有办法实现它。

Like the wise comment above said, get Visual VM with all the plugins and profile your app.

就像上面所说的明智评论一样,使用所有插件获取 Visual VM 并配置您的应用程序。

回答by Denys Séguret

It's virtually impossible to compute the real size of an object without instrumentation.

在没有仪器的情况下,几乎不可能计算出物体的真实尺寸。

Sometimes you can compute it given the size of pointers, primitive types, the overhead of objects, etc. but doing it for something as complex as a HashMap with content (the real size size is dependent of its history and hash factor and the hascode of all its elements modulo the hash factor) is near impossible.

有时您可以根据指针的大小、原始类型、对象的开销等来计算它。它的所有元素都以哈希因子为模)几乎是不可能的。

The only real solution is to use a heap analyzer, such as Eclipse Mat.

唯一真正的解决方案是使用堆分析器,例如Eclipse Mat

回答by Micha? Kosmulski

It depends very much on your JVM vendor and version. You can have a look at this presentationfor a rough estimation.

这在很大程度上取决于您的 JVM 供应商和版本。您可以查看此演示文稿以进行粗略估计。