What are the 'shadow$_klass_' and 'shadow$_monitor_' variables for in java.lang.Object?

Tspoon picture Tspoon · Nov 14, 2014 · Viewed 7.6k times · Source

In the latest Android update (SDK 21), it appears that two new variables have been added to java.lang.Object:

private transient Class<?> shadow$_klass_;
private transient int shadow$_monitor_;

I notice that shadow$_monitor_ is briefly used in hashCode():

public int hashCode() {
    int lockWord = shadow$_monitor_;
    final int lockWordMask = 0xC0000000;  // Top 2 bits.
    final int lockWordStateHash = 0x80000000;  // Top 2 bits are value 2 (kStateHash).
    if ((lockWord & lockWordMask) == lockWordStateHash) {
        return lockWord & ~lockWordMask;
    }
    return System.identityHashCode(this);
}

But otherwise there are no references to them. Are they somehow related to GC in ART? Or some sort of native stuff?

Answer

Petter picture Petter · Nov 21, 2014

They are indeed connected to GC. They seem to have been added in order to support Brooks pointers. I found some information on Brooks pointers here:

The idea is that each object on the heap has one additional reference field. This field either points to the object itself, or, as soon as the object gets copied to a new location, to that new location. This will enable us to evacuate objects concurrently with mutator threads

See especially these two commits:

libcore: a7c69f785f7d1b07b7da22cfb9150c584ee143f4

art: 9d04a20bde1b1855cefc64aebc1a44e253b1a13b