0

I was wondering if it is possible to prevent an object from being moved around by the GC by setting one of the bits in the mark word of the object header myself (trying to trick GC).

So basically I would like to know if this way it could be ensured that a marked object always has the same memory address.

Haasip Satang
  • 457
  • 3
  • 17
  • directbytebuffers sit outside the heap, so their memory is guranteed to not be moved – the8472 Dec 07 '16 at 16:01
  • True, but the question was not about storing something outside of heap. I really wanted to know if there is a way to (temporary) prevent objects from being moved. – Haasip Satang Dec 08 '16 at 01:18
  • well, a JVM with a non-moving GC would have that behavior automatically. and some JVMs might offer object pinning as explicit feature. hotspot does not. – the8472 Dec 08 '16 at 09:57

1 Answers1

1

No, HotSpot JVM does not support object pinning in any form, even internally inside JVM.

Do not try to modify object header. It is high chance JVM will crash otherwise. The contents of the header may vary depending on JVM version, arguments and selected GC algorithm. JVM has full control over the object header and is not ready for external changes.

apangin
  • 92,924
  • 10
  • 193
  • 247
  • Shame but thanks! Do you have any good read recommendations regarding these topics (other the OpenJDK source code). Something like an updated "Inside the Java Virtual Machine" that also covers details like how the object header is really used, etc? – Haasip Satang Dec 06 '16 at 12:05
  • @HaasipSatang I'm not aware of any, sorry. I believe the source code is the best. HotSpot JVM sources are well documented by the way. You may also look for conference presentations about JVM internals like [this one](https://www.infoq.com/presentations/hotspot-memory-data-structures). – apangin Dec 08 '16 at 01:25