0

I was reading about generational garbage collection here. My understanding is that objects may be moved from one location to another in memory when GC runs. If objects are being moved, how are references to objects stored so that they are not invalidated?

Object ref = new Object();

What is saved in ref?

Jon Heller
  • 34,999
  • 6
  • 74
  • 132
letmutx
  • 1,386
  • 11
  • 22

1 Answers1

2

Keep in mind: Java code is executed within a virtual machine.

And in that sense it is a pure implementation detail how that JVM internally deals with those "things" that are perceived as objects by the programmer.

In other words: the JVM is (probably) "simply" using mapping functions to turn references into real pointers.

Then: you have to understand that java references are only an abstraction. They are not pointers to some memory!

GhostCat
  • 137,827
  • 25
  • 176
  • 248