add demo for how memory is allocated in java

This commit is contained in:
Jason Lu 2025-03-06 21:56:50 +08:00
parent ac6fe30cc1
commit 05c4ce52dc

View File

@ -0,0 +1,21 @@
package jvm.gc;
public class PrintMemoryAddress {
public Object getObj() {
Object o = new Object();
System.out.println(STR."The addree is \{System.identityHashCode(o)}");
return o;
}
public void showObj() {
Object o = getObj();
Object o2 = new Object();
System.out.println(STR."Now the addree is \{System.identityHashCode(o)}");
System.out.println(STR."The addree of o2 is \{System.identityHashCode(o2)}");
}
public static void main(String[] args) {
PrintMemoryAddress printMemoryAddress = new PrintMemoryAddress();
printMemoryAddress.showObj();
}
}