Java中ArrayList的内存大小是多少

发布于 2024-10-12 20:27:35 字数 104 浏览 3 评论 0原文

我有一个 ArrayList,我想知道它使用了多少内存。

Obj 是变体,因此,它不像将数组中的元素数量乘以对象的大小那么容易。

I have an ArrayList<Obj> and I wish to know how much memory it is using.

The Obj is variant so, it is not as easy as multiply the number of elements in the array per the size of an object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

内心激荡 2024-10-19 20:27:35

它是 java.util.ArrayList 的容量乘以引用大小(32 位为 4 个字节,64 位为 8 个字节)+ [对象头 + 1 个 int 和 1 个引用]

容量与大小不同(总是 >=),但您想让它们相等,请调用 trimToSize()

从技术上讲,ArrayList 有一个 Object[] 用于存储数据。

It's the capacity of the java.util.ArrayList multiplied by the reference size (4 bytes on 32bit, 8bytes on 64bit) + [Object header + one int and one references].

The capacity is different (always >=) than the size but you want to make 'em equal, call trimToSize()

Technically speaking the ArrayList has an Object[] where it stores the data.

A君 2024-10-19 20:27:35

您可以使用类似 Runtime.getRuntime().totalMemory() 及其对应的 Runtime.getRuntime().freeMemory() 以获得有根据的猜测,但这并没有考虑到 GC 的对象在通话之间编辑。

You can use something like Runtime.getRuntime().totalMemory() and its counterpart Runtime.getRuntime().freeMemory() to get an educated guess, but that doesn't account for objects that are GC'ed between calls.

等风来 2024-10-19 20:27:35

这就是内存分析器的用途。它会告诉您适合您的平台。空 ArrayList 的最小大小为 64 字节。除非您有 100K 或更多元素,否则您很可能不需要知道这一点。

This is what a memory profiler is for. It will tell you for your platform. The minimum size for an empty ArrayList is 64-bytes. It is highly likely you don't need to know this unless you have 100K elements or more.

漫漫岁月 2024-10-19 20:27:35

来源:http://uttesh.blogspot。 com/2015/04/get-byte-or-memory-size-of.html

public static long getBytesFromList(List list) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(baos);
    out.writeObject(list);
    out.close();
    return baos.toByteArray().length;
}

Source: http://uttesh.blogspot.com/2015/04/get-byte-or-memory-size-of.html

public static long getBytesFromList(List list) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(baos);
    out.writeObject(list);
    out.close();
    return baos.toByteArray().length;
}
屋檐 2024-10-19 20:27:35

如果你知道对象的内容分布,你可以暴力计算它。

然而,我能想到的最精确的方法是在分析器中查看它。有一些免费工具,其中一些是 JDK 附带的。然而,我用过的最好的工具是 Yourkit。这并不意味着它是唯一的解决方案,只是我最喜欢的。

You can brute force calculate it if you know the content distribution of the objects.

However, the most precise method I can think of is to look at it in a profiler. There are free tools out there, some that come with the JDK. However, the best tool I've used is Yourkit. That doesn't mean it's the only solution, just my favorite.

药祭#氼 2024-10-19 20:27:35

您可能必须使用 分析器(例如 Netbeans 中提供的分析器),它会向您显示我们的程序的内存消耗情况以及可以为您提供有关每个对象的一些详细信息。

You may have to use a profiler like the one available in Netbeans that will show you the memory consumption of our program and can give you some details about each object.

感悟人生的甜 2024-10-19 20:27:35

答案是,这取决于你如何衡量。如果你问我 ArrayList 的大小,我会给你浅层大小。也就是说,ArrayList 由一个引用数组和一个指示所包含元素数量的整数组成。如果你想知道大小,很简单,就是 4 + *array.length。

如果您想知道 ArrayList 和所有包含的元素的大小,那么有一些堆分析器可以解决这个问题。我相信 YourKit 就是其中之一。

The answer is, it depends on how you measure. If you asked me the size of an ArrayList I would give you the shallow size. That is to say, an ArrayList consists of an array of references and an integer indicating the number of contained elements. If you wanted to know the size it is quite simply 4 + *array.length.

If you want to know the size of the ArrayList and all contained elements then there are some heap analyzers that can figure this out. I believe YourKit is one of them.

╄→承喏 2024-10-19 20:27:35

java的内存使用情况取决于JVM的实现。我所知道的确定实例内存使用情况的唯一真正方法是使用 Java 5 仪器代理。有一个教程可以做到这一点。

The memory usage of java depends of the JVM implementation. The only real method to determine the memory usage of an instance I know is to use an Java 5 instrumentation agent. There is a little tutorial to do so.

落花浅忆 2024-10-19 20:27:35

ArrayList 的工作方式是它只包含一个特定大小的数组(可以在构造函数中指定,我相信 10 是默认值?)。每当元素数量对于内部数组来说太大时,内部数组的大小就会加倍。因此,您需要将对象的大小乘以内部数组的大小。

The way an ArrayList works is that it simply contains an array of a certain size (which can be specified in the constructor, I believe 10 is the default?). Whenever the number of elements becomes too large for the internal array, the size of the internal array is doubled. So you need to multiply the size of the object by the size of the internal array.

毁梦 2024-10-19 20:27:35

在 ArrayList 中添加和删除元素后,进行堆转储。使用任何堆分析器。 (Eclipse MAT 我更喜欢)
您可能会看到为已删除的项目放置了空值。这使得 ArrayList 即使只有几个元素或没有元素也能将内存保存在堆中。您可以在“Retained Heap”列中看到确切消耗的内存大小。

Take heap dump, after adding and deleting elements from your ArrayList. Use any heap analyzer. (Eclipse MAT I prefer)
You might see null values placed for deleted items. this makes ArrayList to hold memory in a heap even when there are only a few elements or no element. You can see the exact consumed memory size in the "Retained Heap" column.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文