Memory size of Java 32-bit system int [] array

In Java, the memory used to occupy an int [] array of size n is equal to (4 N) * 4 bytes

In fact, it can be proved by the following code:

public class test {

    public static void main(String[] args) {

        long size = memoryUsed();
        int[] array = new int[2000];
        size = memoryUsed() - size;
        if (size == 0)
            throw new AssertionError("You need to run this with -XX:-UseTLAB for accurate accounting");
        System.out.printf("int[2000] used %,d bytes%n",size);

    }

    public static long memoryUsed() {
        return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
    }

}

Interestingly, the fourth digit in parentheses The first part of 4 bytes needs array reference, and the second array length needs 8 bytes?

Solution

General object overhead – typically a few bytes indicating the type of object and a few bytes associated with the object's monitor This is not array specific - you will see all the objects

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>