C malloc
implementations typically don't allocate the precise amount of memory requested but instead consume fixed-size runs of memory, e.g. with power-of-two sizes, so that an allocation of 1025 bytes actually takes a 2048-byte segment with 1023 bytes lost as slop.
C malloc实现通常不分配所请求的精确内存量,而是使用固定大小的内存运行,例如,具有两个幂的大小,因此1025字节的分配实际上需要一个2048字节的段,其中1023字节丢失为slop。
Does HotSpot use a similar allocation mechanism for Java arrays? If so, what's the right way to allocate a Java array such that there's no slop? (E.g. should the array length be a power of two or maybe power of two minus some fixed amount of overhead?)
HotSpot是否对Java数组使用类似的分配机制?如果是这样,分配Java数组的正确方法是什么,这样就没有什么了不起? (例如,阵列长度应该是2的幂,还是2的幂,减去一些固定的开销量?)
1 个解决方案
#1
5
If you're asking about the language, the answer is: Its not specified (same as for C)
如果您询问语言,答案是:未指定(与C相同)
If you're asking about a specific implementation, check out that implementation. I believe for Hotspot its 8 bytes granularity; that is object sizes are rounded up to the next granularity boundary. If the question is about the heap size increase when there isn't enough free heap, then it depends on implementation, GC settings, heap size params and so on; making it impractical to answer precisely.
如果您询问具体实现,请查看该实现。我相信Hotspot的8字节粒度;即对象大小向上舍入到下一个粒度边界。如果问题是当没有足够的可用堆时堆大小增加,那么它取决于实现,GC设置,堆大小参数等等;准确回答是不切实际的。
EDIT: Using a small reflection hack, accessing the sun.misc.Unsafe class (Oracle JRE only), object references can be converted to memory addresses; output the addresses of two consecutively allocated arrays to check for yourself.
编辑:使用一个小的反射黑客,访问sun.misc.Unsafe类(仅限Oracle JRE),对象引用可以转换为内存地址;输出两个连续分配的数组的地址以自行检查。
And I basically asked the same question here: Determine the optimal size for array with respect to the JVM's memory granularity (Answers include an example of using the Unsafe class to check for object size)
我在这里基本上问了同样的问题:根据JVM的内存粒度确定数组的最佳大小(Answers包括使用Unsafe类检查对象大小的示例)
#1
5
If you're asking about the language, the answer is: Its not specified (same as for C)
如果您询问语言,答案是:未指定(与C相同)
If you're asking about a specific implementation, check out that implementation. I believe for Hotspot its 8 bytes granularity; that is object sizes are rounded up to the next granularity boundary. If the question is about the heap size increase when there isn't enough free heap, then it depends on implementation, GC settings, heap size params and so on; making it impractical to answer precisely.
如果您询问具体实现,请查看该实现。我相信Hotspot的8字节粒度;即对象大小向上舍入到下一个粒度边界。如果问题是当没有足够的可用堆时堆大小增加,那么它取决于实现,GC设置,堆大小参数等等;准确回答是不切实际的。
EDIT: Using a small reflection hack, accessing the sun.misc.Unsafe class (Oracle JRE only), object references can be converted to memory addresses; output the addresses of two consecutively allocated arrays to check for yourself.
编辑:使用一个小的反射黑客,访问sun.misc.Unsafe类(仅限Oracle JRE),对象引用可以转换为内存地址;输出两个连续分配的数组的地址以自行检查。
And I basically asked the same question here: Determine the optimal size for array with respect to the JVM's memory granularity (Answers include an example of using the Unsafe class to check for object size)
我在这里基本上问了同样的问题:根据JVM的内存粒度确定数组的最佳大小(Answers包括使用Unsafe类检查对象大小的示例)