I knew the general answer but fumbled the sequencing.
Start by clarifying the language and runtime (e.g., Java HotSpot) and then walk through the memory changes in chronological order: class loading, object allocation, initialization, and reference assignment. Emphasize the distinction between stack and heap, and mention relevant JVM internals like TLAB and escape analysis.
Pro tip: Mention that the JIT compiler may optimize away the allocation entirely via escape analysis, and that the actual memory layout includes an object header—this shows depth beyond textbook answers.
Explain that before any object can be created, the JVM must load, link, and initialize the Object class. This involves loading the class file, verifying bytecode, preparing static fields, and resolving symbolic references.
Describe how the JVM allocates memory on the heap for the new Object instance. Mention techniques like bump-the-pointer and TLAB (Thread Local Allocation Buffer) for efficient allocation, and note that the object header (mark word and klass pointer) is included.
Explain that the allocated memory is zeroed out (ensuring all fields have default values) and then the constructor is called. For Object, the constructor does nothing, but the JVM still performs initialization steps.
Describe how the reference variable 'o' is stored on the stack (or in a register) and is assigned the address of the newly created object on the heap. Mention that the reference itself is a pointer-sized value.
Discuss how the JIT compiler might eliminate the allocation entirely if the object does not escape (escape analysis), or how it might use scalar replacement to store fields on the stack instead.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.