From 05c4ce52dc83a27914447b881b3104e2a051b466 Mon Sep 17 00:00:00 2001 From: Jason Lu Date: Thu, 6 Mar 2025 21:56:50 +0800 Subject: [PATCH] add demo for how memory is allocated in java --- src/main/java/jvm/gc/PrintMemoryAddress.java | 21 ++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/main/java/jvm/gc/PrintMemoryAddress.java diff --git a/src/main/java/jvm/gc/PrintMemoryAddress.java b/src/main/java/jvm/gc/PrintMemoryAddress.java new file mode 100644 index 0000000..840215b --- /dev/null +++ b/src/main/java/jvm/gc/PrintMemoryAddress.java @@ -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(); + } +}