Pinning an object by modifying bits in mask word of object header

131 Views Asked by At

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.

1

There are 1 best solutions below

2
On BEST ANSWER

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.