2

My understanding is that during garbage collection the hotspot JVM will:

  1. Walk through GC root, and mark all objects on the PATH as reachable. Not consider static object, JVM will find GC root on statck by OopMap.
  2. JVM will "sweep" the unreachable object. So the JVM must also find out the unreachable object reference. I guesss it can also leverage OopMap. I think the OopMap is on the stack, so if the stack frame is popped, JVM lost references...

My questions are:

  1. How does the JVM find an unreachable object if the stack frame is not popped?
  2. How does the JVM find an unreachable object if the stack frame is popped?
Kaan
  • 5,434
  • 3
  • 19
  • 41
CodeBoy
  • 591
  • 6
  • 12
  • 1
    First off, compacting GC algorithms typically don't need to "sweep" at all. They just copy (evacuate) reachable objects to a new destination, and reclaim the source memory area (region/space) all at once. – apangin Jan 27 '20 at 23:39
  • 2
    Sweeping algorithms (e.g. CMS) find reclaimable memory blocks (i.e. unreachable objects) by iterating through the entire heap generation and processing all unmarked objects. An object size is determined from the object header. Given an object address and its size, JVM can easily find the address of the next object (either marked or unmarked). – apangin Jan 27 '20 at 23:41
  • 1
    You do know `CMS` ( which you seem to ask here about) is deprecated, right? – Eugene Jan 28 '20 at 11:04
  • 1
    I suggest reading [this answer](https://stackoverflow.com/a/57853489/2711488) and perhaps [this as an addendum](https://stackoverflow.com/a/54619104/2711488) – Holger Jan 28 '20 at 13:24
  • did you found the answer that you wanted? I am trying to figure out this too, I have read the jdk source code but did not found how the jvm found the unreachable object. @CodeBoy – Dolphin Oct 21 '22 at 13:48

0 Answers0