如何计算java数组内存使用量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2972578/
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-08-13 15:03:44  来源:igfitidea点击:

how calculate java array memory usage

javaarrays

提问by xdevel2000

If I have:

如果我有:

int c[] = new int[10];

and

int a[][] = new int[2][3];

and in generally

并且在一般情况下

an n*m*..*jarray

一个n*m*..*j数组

how can I calculate the real memory usage considering also the references variables?

考虑到引用变量,我如何计算实际内存使用量?

采纳答案by J?rgen Fogh

If you want an accurate answer, you can't. At least not in any easy way. This threadexplains more.

如果你想要一个准确的答案,你不能。至少不是以任何简单的方式。这个线程解释了更多。

The trouble with Bragaadeesh's and Bakkal's answers are that they ignore overhead. Each array also stores things like the number of dimensions it has, how long it is and some stuff the garbage collector uses.

Bragaadeesh 和 Bakkal 的答案的问题在于他们忽略了开销。每个数组还存储诸如它的维数、长度以及垃圾收集器使用的一些东西。

For a simple estimate, you should be fine by using the calculations from the other answers and adding 100-200 bytes.

对于简单的估计,使用其他答案中的计算并添加 100-200 字节应该没问题。

回答by bragboy

The int[]or int[][]is not a primitive data type. It is an Object in Java. And with an Object, the size cannot be calculated straight away.

int[]int[][]不是原始数据类型。它是 Java 中的一个对象。并且对于对象,无法立即计算大小。

回答by Ryan Fernandes

you'll probably get the best approximation from this: http://java.sun.com/javase/6/docs/api/java/lang/instrument/Instrumentation.html#getObjectSize(java.lang.Object)

你可能会得到最好的近似值:http: //java.sun.com/javase/6/docs/api/java/lang/instrument/Instrumentation.html#getObjectSize(java.lang.Object)

This article, and this comprehensive onedemonstrates/details this approach

这篇文章和这篇综合文章演示/详细介绍了这种方法

回答by Ryan Fernandes

I know I'm kinda late to the party, but it's really not extremely hard to compute the memory footprint.

我知道我参加聚会有点晚了,但计算内存占用量真的不是很难。

Lets take your first example: int c[] = new int[N];

让我们举你的第一个例子: int c[] = new int[N];

According to the 64-bit memory model, an int is 4 bytes, so all the elements will be 4*N bytes in size. In addition to that, Java has a 24 bytes array overhead and there's also 8 bytes for the actual array object. So that's a total of 32 + 4 * N bytes.

根据 64 位内存模型,一个 int 是 4 个字节,所以所有元素的大小都是 4*N 个字节。除此之外,Java 有 24 个字节的数组开销,实际数组对象还有 8 个字节。所以总共有 32 + 4 * N 个字节。

For a 2 dimensional array: int a[][] = new int[N][M];

对于二维数组: int a[][] = new int[N][M];

It's basically the same just that each element in the first array is another array of size M, so instead of 4 we have 32 + 4 * M, so the total size is 32 + (32 + 4 * M) * N.

基本相同,只是第一个数组中的每个元素都是另一个大小为 M 的数组,因此我们有 32 + 4 * M,而不是 4,因此总大小为 32 + (32 + 4 * M) * N。

It's true that a generalization for D dimensions is pretty complicated, but you get the idea.

确实,D 维的泛化非常复杂,但您明白了。

回答by Chuanzhou Tang

for original type:base type and size of Byte

对于原始类型:基本类型和字节大小

  • boolean 1
  • byte 1
  • char 1
  • int 4
  • float 4
  • long 8
  • double 8
  • Interger 24 (16 for Class instance + 4 for int + 4 for memory alignment)
  • 布尔值 1
  • 字节 1
  • 字符 1
  • 整数 4
  • 浮动 4
  • 长8
  • 双8
  • 整数 24(16 用于类实例 + 4 用于 int + 4 用于内存对齐)

int a[M]: 24+4M

int a[M]: 24+4M

(16 for Class + 4 for save array size + 4 for memory alignment) + (for M size of double, we need 4 * M)

(Class 16 + 4 保存数组大小 + 4 内存对齐) + (M size of double, 我们需要 4 * M)

int a[M][N]: (24+4M) + M*(24+4N) = 24+28M+4MN ~~~4MN

int a[M][N]: (24+4M) + M*(24+4N) = 24+28M+4MN ~~~4MN

treat a[M][N] as M size of double array a[N] plus one extra array to hold the reference of all M size array start point.

将 a[M][N] 视为 M 大小的双数组 a[N] 加上一个额外的数组来保存所有 M 大小数组起点的引用。