简体   繁体   English

Java中ArrayList的内存大小是多少

[英]What is the memory size of an ArrayList in Java

I have an ArrayList<Obj> and I wish to know how much memory it is using. 我有一个ArrayList<Obj> ,我想知道它正在使用多少内存。

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. Obj是变体,因此,它不像将一个对象的大小乘以数组中的元素数那么容易。

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] . 它是java.util.ArrayList的容量乘以引用大小(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() 容量不同于大小(always >=) ,但是要使它们相等,请调用trimToSize()

Technically speaking the ArrayList has an Object[] where it stores the data. 从技术上讲, ArrayList具有一个Object[] ,用于存储数据。

您可以使用类似Runtime.getRuntime().totalMemory()及其对应的Runtime.getRuntime().freeMemory()来进行有根据的猜测,但这并不考虑两次调用之间使用GC的对象。

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. 空ArrayList的最小大小为64字节。 It is highly likely you don't need to know this unless you have 100K elements or more. 除非您拥有100K或更多的元素,否则您很有可能不需要知道这一点。

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. 有一些免费工具,有些是JDK附带的。 However, the best tool I've used is Yourkit. 但是,我使用的最好的工具是Yourkit。 That doesn't mean it's the only solution, just my favorite. 这并不意味着它是唯一的解决方案,只是我的最爱。

您可能必须使用类似于Netbeans中可用的探查器 ,该探查器将向您显示我们程序的内存消耗,并可以为您提供有关每个对象的一些详细信息。

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. 如果您问我ArrayList的大小,我会给您一些浅的大小。 That is to say, an ArrayList consists of an array of references and an integer indicating the number of contained elements. 也就是说,ArrayList由引用数组和指示所包含元素数的整数组成。 If you wanted to know the size it is quite simply 4 + *array.length. 如果您想知道大小,则只需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. 如果您想知道ArrayList的大小以及所有包含的元素,那么可以使用一些堆分析器来解决。 I believe YourKit is one of them. 我相信YourKit是其中之一。

The memory usage of java depends of the JVM implementation. java的内存使用情况取决于JVM的实现。 The only real method to determine the memory usage of an instance I know is to use an Java 5 instrumentation agent. 确定我知道的实例的内存使用情况的唯一真正方法是使用Java 5 工具代理。 There is a little tutorial to do so. 有一些教程可以做到这一点。

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?). ArrayList的工作方式是只包含一个特定大小的数组(可以在构造函数中指定,我相信默认值为10)。 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. 因此,您需要将对象的大小乘以内部数组的大小。

Source: http://uttesh.blogspot.com/2015/04/get-byte-or-memory-size-of.html 资料来源: 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM