2

I was reading the official JDK 12 specs and had to verify something, which led me to an article that said:

"G1 collects regions with the least amount of live data (Garbage First!) and compacts/evacuates live data into new regions. Secondly, it uses a series of incremental, parallel and multi-phased cycles to achieve its soft pause time target. This allows G1 to do what’s necessary, in the time defined, irrespective of the overall heap size."

Does this imply that a variable in a low-active-data-density region could be moved to another location, in which case a variable's address will change?

Although one can dig for the exact piece of code that implements this feature of G1, I'd like to understand its consequences for memory management without going through an entire JVM implementation.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Calicoder
  • 1,322
  • 1
  • 19
  • 37
  • 1
    When do you ever have access to a variable's address? (Are you worried about things being misplaced?) – Sotirios Delimanolis Jul 22 '19 at 19:09
  • 2
    Yes, a objects address will change, and any of the garbage collectors can do that, not just G1, but that doesn't matter to you, because you don't get to know the address of objects (unless you use JNI). – Andreas Jul 22 '19 at 19:10
  • @Andreas I asked because it could matter in debugging, because sometimes one wants to check if the correct instance is being compared to another instance. However, I think i see what you mean: the object instance id (or "identity hashcode") is different from (and not based on) the actual address, right? – Calicoder Jul 22 '19 at 20:25
  • 2
    @Calicoder Correct. Hash code has nothing to do with address of object. There is not way, *in Java*, to get the address of an object. – Andreas Jul 22 '19 at 20:30
  • [Here's a good explanation](https://stackoverflow.com/a/3796963/5623232) on how the `identityHashCode()` interacts with memory (re)allocations. – NPras Jul 23 '19 at 07:31
  • @Andreas hashCode _could_ be memory based, [for example](https://stackoverflow.com/questions/49172698/default-hashcode-implementation-for-java-objects/49175508#49175508) and address _can_ be retrieved, but requires use of `Unsafe` - not the usual java, though. – Eugene May 21 '20 at 23:52

2 Answers2

0

Yes, a variable's location in memory can change due to garbage collection (not specifically to G1 either).

Separately, it isn't clear why you're concerned about this. And without a lot more detail, background, and justification, you probably shouldn't be.

Kaan
  • 5,434
  • 3
  • 19
  • 41
  • The only purpose fo this question was understanding which I think is a sufficient purpose for this question. Thanks for answering – Calicoder May 22 '20 at 00:27
0

The Object can change were it resides - yes, while it is being moved to a different region. But the reference to that Object is changed atomically - your application will not even notice that. Here is a very long explanation of how some GCs do it.

Eugene
  • 117,005
  • 15
  • 201
  • 306