Many books/articles I've read on this topic, as well as a small program I wrote using 'Unsafe', indicate that 1-d arrays in Java are always contiguous in memory. So is it dictated by JLS or is it an implementation convention? The question is asked to confirm this indication.
我读过很多关于这个主题的书籍和文章,以及我用'Unsafe'编写的一个小程序,表明Java中的1-d数组在内存中总是连续的。它是由JLS决定的还是实施惯例?要求提出这个问题。
4 个解决方案
#1
16
No, the JVM specification does not have any such guarantees: http://docs.oracle.com/javase/specs/jvms/se5.0/html/Concepts.doc.html#16446
不,JVM规范没有任何此类保证:http://docs.oracle.com/javase/specs/jvms/se5.0/html/Concepts.doc.html#16446
In practice it is probably the case but you also have no guarantee about the word size.
在实践中可能是这种情况,但您也无法保证字数。
Unsafe is not a standard Java class, so if your program uses this, then it is not portable anyway...
不安全不是标准的Java类,所以如果你的程序使用它,那么它无论如何都不可移植......
#2
10
Since there is no real way to interact with memory addresses in Java, it is also not defined in the spec how the layout of object in memory looks.
由于没有真正的方法与Java中的内存地址进行交互,因此在规范中也没有定义内存中对象的布局如何。
Note that using Unsafe
pretty much automatically means that you're strolling outside the realm of the spec.
请注意,几乎自动使用不安全意味着您在规范范围之外漫步。
That being said, I'd venture that most JVM implementations do in fact use a linear layout for (one-dimensional) arrays.
话虽如此,我敢说大多数JVM实现确实使用(一维)数组的线性布局。
#3
8
I want to refresh this question with what The Java Language Specification, Java SE 8 Edition (JLS) and The Java Virtual Machine Specification, Java SE 8 Edition (JVMS) are saying about it.
我想用Java语言规范,Java SE 8 Edition(JLS)和Java虚拟机规范,Java SE 8 Edition(JVMS)来解释这个问题。
We have to choices to answer to this question:
我们必须选择回答这个问题:
- What constraints are imposed on JVM implementations. This is the most reliable approach because implementation of any specification inherently presume of "Everything which is not forbidden is allowed" principle.
- 对JVM实现施加了哪些约束。这是最可靠的方法,因为任何规范的实现都固有地假定“允许一切不被禁止”的原则。
- What most JVM implementations suggest reasonable
- 大多数JVM实现建议合理
I will point out to specification constraints.
我将指出规范约束。
If we look at Chapter 10. Arrays of JLS (and any other chapters of JLS and JVMS related to arrays) we couldn't find any mention of memory layout constraints imposed to arrays. Thus it definitely means that array might be not continuous.
如果我们看一下第10章JLS的数组(以及与数组相关的JLS和JVMS的任何其他章节),我们找不到任何关于对数组施加的内存布局约束的提及。因此,它肯定意味着数组可能不连续。
Moreover, JLS says that arrays are Objects:
而且,JLS说数组是对象:
Chapter 10. Arrays.
第10章数组。
In the Java programming language, arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2). All methods of class Object may be invoked on an array.
...在Java编程语言中,数组是对象(§4.3.1),是动态创建的,可以分配给Object类型的变量(§4.3.2)。可以在数组上调用Object类的所有方法。 ...
4.3.1. Objects.
4.3.1。对象。
An object is a class instance or an array. (and Array is Object)
对象是类实例或数组。 (和Array是Object)
And at the same time JVMS says that objects and arrays are stored on the heap:
同时JVMS说对象和数组存储在堆上:
2.5.3. Heap
2.5.3。堆
The Java Virtual Machine has a heap that is shared among all Java Virtual Machine threads. The heap is the run-time data area from which memory for all class instances and arrays is allocated.
Java虚拟机具有在所有Java虚拟机线程之间共享的堆。堆是运行时数据区,从中分配所有类实例和数组的内存。
But JVMS doesn't compel heap memory to be continuous:
但是JVMS并不强制堆内存是连续的:
2.5.3. Heap
2.5.3。堆
... The memory for the heap does not need to be continuous.
...堆的内存不需要是连续的。
As all arrays are stored in heap and heap might be not continuous, it follows that arrays also might be not continuous.
由于所有数组都存储在堆中,并且堆可能不连续,因此数组也可能不连续。
#4
3
Given that many JVM's have a requirement that the heap is continous in memory I think its unlikely they will place a 1d array of primitives in different places in memory.
鉴于许多JVM都要求堆在内存中连续存在,我认为它们不太可能将1d基元数组放在内存中的不同位置。
The object referenced by an Object[] are unlikely to be continous in memory and even if they are, can be re-arranged without warning.
Object []引用的对象不太可能在内存中连续,即使它们是,也可以在没有警告的情况下重新安排。
Note: Using Unsafe you can read references in an array as int
values to see what they are before and after a GC. Some JVMs use 64-bit references which require a long, but most use 32-bti references (even for 64-bit JVMs)
注意:使用Unsafe,您可以将数组中的引用读取为int值,以查看GC之前和之后的内容。一些JVM使用64位引用,这些引用需要很长但大多数使用32-bti引用(即使对于64位JVM)
#1
16
No, the JVM specification does not have any such guarantees: http://docs.oracle.com/javase/specs/jvms/se5.0/html/Concepts.doc.html#16446
不,JVM规范没有任何此类保证:http://docs.oracle.com/javase/specs/jvms/se5.0/html/Concepts.doc.html#16446
In practice it is probably the case but you also have no guarantee about the word size.
在实践中可能是这种情况,但您也无法保证字数。
Unsafe is not a standard Java class, so if your program uses this, then it is not portable anyway...
不安全不是标准的Java类,所以如果你的程序使用它,那么它无论如何都不可移植......
#2
10
Since there is no real way to interact with memory addresses in Java, it is also not defined in the spec how the layout of object in memory looks.
由于没有真正的方法与Java中的内存地址进行交互,因此在规范中也没有定义内存中对象的布局如何。
Note that using Unsafe
pretty much automatically means that you're strolling outside the realm of the spec.
请注意,几乎自动使用不安全意味着您在规范范围之外漫步。
That being said, I'd venture that most JVM implementations do in fact use a linear layout for (one-dimensional) arrays.
话虽如此,我敢说大多数JVM实现确实使用(一维)数组的线性布局。
#3
8
I want to refresh this question with what The Java Language Specification, Java SE 8 Edition (JLS) and The Java Virtual Machine Specification, Java SE 8 Edition (JVMS) are saying about it.
我想用Java语言规范,Java SE 8 Edition(JLS)和Java虚拟机规范,Java SE 8 Edition(JVMS)来解释这个问题。
We have to choices to answer to this question:
我们必须选择回答这个问题:
- What constraints are imposed on JVM implementations. This is the most reliable approach because implementation of any specification inherently presume of "Everything which is not forbidden is allowed" principle.
- 对JVM实现施加了哪些约束。这是最可靠的方法,因为任何规范的实现都固有地假定“允许一切不被禁止”的原则。
- What most JVM implementations suggest reasonable
- 大多数JVM实现建议合理
I will point out to specification constraints.
我将指出规范约束。
If we look at Chapter 10. Arrays of JLS (and any other chapters of JLS and JVMS related to arrays) we couldn't find any mention of memory layout constraints imposed to arrays. Thus it definitely means that array might be not continuous.
如果我们看一下第10章JLS的数组(以及与数组相关的JLS和JVMS的任何其他章节),我们找不到任何关于对数组施加的内存布局约束的提及。因此,它肯定意味着数组可能不连续。
Moreover, JLS says that arrays are Objects:
而且,JLS说数组是对象:
Chapter 10. Arrays.
第10章数组。
In the Java programming language, arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2). All methods of class Object may be invoked on an array.
...在Java编程语言中,数组是对象(§4.3.1),是动态创建的,可以分配给Object类型的变量(§4.3.2)。可以在数组上调用Object类的所有方法。 ...
4.3.1. Objects.
4.3.1。对象。
An object is a class instance or an array. (and Array is Object)
对象是类实例或数组。 (和Array是Object)
And at the same time JVMS says that objects and arrays are stored on the heap:
同时JVMS说对象和数组存储在堆上:
2.5.3. Heap
2.5.3。堆
The Java Virtual Machine has a heap that is shared among all Java Virtual Machine threads. The heap is the run-time data area from which memory for all class instances and arrays is allocated.
Java虚拟机具有在所有Java虚拟机线程之间共享的堆。堆是运行时数据区,从中分配所有类实例和数组的内存。
But JVMS doesn't compel heap memory to be continuous:
但是JVMS并不强制堆内存是连续的:
2.5.3. Heap
2.5.3。堆
... The memory for the heap does not need to be continuous.
...堆的内存不需要是连续的。
As all arrays are stored in heap and heap might be not continuous, it follows that arrays also might be not continuous.
由于所有数组都存储在堆中,并且堆可能不连续,因此数组也可能不连续。
#4
3
Given that many JVM's have a requirement that the heap is continous in memory I think its unlikely they will place a 1d array of primitives in different places in memory.
鉴于许多JVM都要求堆在内存中连续存在,我认为它们不太可能将1d基元数组放在内存中的不同位置。
The object referenced by an Object[] are unlikely to be continous in memory and even if they are, can be re-arranged without warning.
Object []引用的对象不太可能在内存中连续,即使它们是,也可以在没有警告的情况下重新安排。
Note: Using Unsafe you can read references in an array as int
values to see what they are before and after a GC. Some JVMs use 64-bit references which require a long, but most use 32-bti references (even for 64-bit JVMs)
注意:使用Unsafe,您可以将数组中的引用读取为int值,以查看GC之前和之后的内容。一些JVM使用64位引用,这些引用需要很长但大多数使用32-bti引用(即使对于64位JVM)