-2
public class MemoryTest {

  final String fs = "final String"; //A
  final int fi = 1;                 //A

  String s = "Member String";       //A
  int i = 2;                        //A

  final static String fss = "final static String"; //B
  final static int fsi = 3;                        //B

  static String ss = "static String"; //C
  static int si = 4;                  //C

  public static void main(String args[]){
   MemoryTest m = new MemoryTest();
  }
}

[my answer]

//A: When an object is created from a class, it is copied to the object by reference to constant pool of method area, and the created object is assigned those value.

=> location : Heap

//B : Exists in constant pool of method area, Not copied to object

=> location : constant pool of method area

// C : When the execution engine runs "static {}" Class variable in the method area will be assigned by reference to a constant pool in the method area.

=> location : Class variable of method area

ByteCode Analysis

Book Of Zeus
  • 49,509
  • 18
  • 174
  • 171
andy.Mo
  • 11

1 Answers1

2

//A: When an object is created from a class, it is copied to the object by reference to constant pool of method area, and the created object is assigned those value.

Incorrect.

  1. The constant pool you are talking about is part of the classfile, and the values in the pool cannot be referenced by running programs1. The interpreter and JIT compiler use them under the hood, but that is invisible at the application level.

  2. The constant pool is shared by all methods in a classfile. It is not part of any method area.

In the case of string literals and string-valued constant expression, the value in the classfile constant pool is used to create a String object2. That objects is then interned into the runtime string pool, and added to a runtime descriptor.

Note that the string pool is different to the constant pool. In modern JVMs the string pool is a data structure in the regular heap.

=> location : Heap

Correct. The String object, and the MemoryTest object which holds the fs, fi, s and i variables are both in the heap; i.e. those variables are all in the heap.

The s and fs variables hold references. The i and fi variables hold simple 32 bit integer values. They don't refer to anything. They are ... self-contained.

//B : Exists in constant pool of method area, Not copied to object

Incorrect; see above.

The situation is a little complicated for ffi, uses of the value of that variable may be inlined into the code by the bytecode compiler. However, the field still exists at runtime, and may be accessed using reflection or a debug agent.

=> location : constant pool of method area

Incorrect. The location of the String object is in the string pool as above. The fss and fsi variable are in the classes static frame which is also in the heap.

// C : When the execution engine runs "static {}" Class variable in the method area will be assigned by reference to a constant pool in the method area.

Incorrect; see above. (Though si won't be inlined.)

=> location : Class variable of method area

Incorrect; see above.


1 - OK, you could write application code the parse the classfile yourself. Or you could (in theory) use native code to find where the JVM has cached the constant pool and dig the information out. Just don't.

2 - This used to be done eagerly, but recent JVMs create the String objects lazily the first time that the string literal is used. It is actually possible write a test to infer when the object is created. Jon Skeet showed me one time :-)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216