Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 按长数组、整数数组和ArrayList列出的内存使用情况_Java_Memory - Fatal编程技术网

Java 按长数组、整数数组和ArrayList列出的内存使用情况

Java 按长数组、整数数组和ArrayList列出的内存使用情况,java,memory,Java,Memory,无论我使用Long[]、Integer[]还是ArrayList,为什么它们都返回相同的内存使用率 System.out.println("Memory Usage : " + Runtime.getRuntime().totalMemory()/(1024*1024)); System.out.println("Memory Usage : " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())/

无论我使用
Long[]
Integer[]
还是
ArrayList
,为什么它们都返回相同的内存使用率

System.out.println("Memory Usage : " + Runtime.getRuntime().totalMemory()/(1024*1024));
System.out.println("Memory Usage : " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())/(1024*1024));

//Long[] aa = new Long[70000000];
Integer[] bb = new Integer[70000000];
//ArrayList<Integer> a = new ArrayList<Integer>(70000000);

System.gc();

System.out.println("Memory Usage : " + Runtime.getRuntime().totalMemory()/(1024*1024));
System.out.println("Memory Usage : " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())/(1024*1024));
System.out.println(“内存使用:+Runtime.getRuntime().totalMemory()/(1024*1024));
System.out.println(“内存使用:”+(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freemory())/(1024*1024));
//Long[]aa=新Long[7000000];
整数[]bb=新整数[7000000];
//ArrayList a=新的ArrayList(7000000);
gc();
System.out.println(“内存使用:”+Runtime.getRuntime().totalMemory()/(1024*1024));
System.out.println(“内存使用:”+(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freemory())/(1024*1024));
在上面的语句中,无论我使用Long[]、Integer[]还是ArrayList,为什么它们都使用相同的内存

这很有道理。在所有情况下,您基本上都在分配一个7000000*的数组(JVM上的引用大小)。
整数
引用的大小与
引用的大小相同,后者与
对象
引用的大小相同。在
ArrayList
的情况下,您得到了
ArrayList
对象本身的非常小的额外开销,但这非常小,而且根据Louis的评论很容易解释

如果您实际填充了这些数组,创建了
Long
Integer
的实例,那么您会看到不同之处。同样,如果您创建了一个
long[]
和一个
int[]
,您会看到不同之处

在上面的语句中,无论我使用Long[]、Integer[]还是ArrayList,为什么它们都使用相同的内存

这很有道理。在所有情况下,您基本上都在分配一个7000000*的数组(JVM上的引用大小)。
整数
引用的大小与
引用的大小相同,后者与
对象
引用的大小相同。在
ArrayList
的情况下,您得到了
ArrayList
对象本身的非常小的额外开销,但这非常小,而且根据Louis的评论很容易解释


如果您实际填充了这些数组,创建了
Long
Integer
的实例,那么您会看到不同之处。同样,如果您创建了一个
long[]
和一个
int[]
,您会看到不同之处。

,因为您的测量方法完全不准确
System.gc()
是“嘿,你能不能在不久的将来的某个时候用gc?”而
Runtime.getRuntime().totalMemory()
是一个非常粗略的近似值,可能非常离谱。这与Spring和Hibernate有什么关系?因为你的测量方法完全不准确
System.gc()
是“嘿,你能不能在不久的将来的某个时候用gc?”而
Runtime.getRuntime().totalMemory()
是一个非常粗略的近似值,可能会非常离谱。这与Spring和Hibernate有什么关系?