Local references and field references memory allocation
An object can contain references to other objects. If you declare these references as class/field variables then as the object itself is created on the heap, the values represented by the field references are stored on the heap.
So, if i have
public class A {
int size;
}
2
then that is stored as part of the object on the heap, but where is the reference ie the name size stored ? size == 2
on the heap ? Where is the name "size" stored?
The name of the field is stored in the Class object A.class
. You can inspect class field names by using the java.lang.reflect
library.
For example, to inspect all the fields of a class, do this:
for (Field field : A.class.getFields()) {
String fieldName = field.getName();
Class<?> fieldClass = field.getType();
// etc
}
Is the name "size" also stored inside of the object on the heap?
No. It is stored in permgen
memory
How does JVM cross-reference size == 2 on the heap?
It looks up the field at compile time and the rest happens in bytecode
Field references are not created on the main stack?
No. There are more memory areas than just heap and stack, There is also permgen, where the class definitions and class fields are stored. There still more memory areas, for example for the garbage collector.
链接地址: http://www.djcxy.com/p/82594.html上一篇: 存储引用类型对象的实例字段的位置
下一篇: 本地引用和字段引用内存分配