Skip to content

ICTPLSys/Android-Art-WeakReference

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Eliminating BlockingOnWeakGlobalAccess during DisableWeakRefAccess phase for the ConcurrentCopying GC(CC GC).

This repo introduces a new mechanism to handle WeakGlobalRef access during CC GC's DisableWeakRefAccess phase without blocking mutator threads for tens of miliseconds. We form and correct this solution through the discussion with Google’s generous expert, Hans Boehm. Our experiment shows that this solution could control most WeakGlobalAccesses from JNI to tens of microseconds and reduce total BlockingOnWeakGlobalAccess time by more than 80%.

Why ART(Android runtime) need BlockingOnWeakGlobalAccess

According to recent versions of the JNI spec, "Weak global references are related to Java phan tom references (java.lang.ref.PhantomReference). A weak global reference to a specific object is treated as a phantom reference referring to that object when determining whether the object is phantom reachable (see java.lang.ref). Such a weak global reference will become functionally equivalent to NULL at the same time as a PhantomReference referring to that same object would be cleared by the garbage collector.”

This implies that during the DisableWeakRefAccess phase—specifically after processing a FinalizerReference—the state of a WeakGlobalReference may be updated. Crucially, it can only transition from invalid to valid. If the referent is already valid before processing the Finalizer Reference, then returning the referent is safe.

How BlockingOnWeakGlobalAccess performs on mobile devices.

Informed by a team of Xiaomi, we observed that the CC GC's DisableWeakRefAccess phase could block mutators' WeakGlobalRef access for tens of milliseconds within long-running applications. Applications like DouYin are long-time used by many people and bring large memory pressure to the ART, which results in more GCs. Therefore, the prolonged blocking of WeakGlobalRef access frequently causes frame drops, negatively impacting the user's experience.

According to our experiment, processing FinalizerReference seldom changes(less than 1%) the state of WeakGlobalReference and most of WeakGlobalReferences accessed by jni(more than 99%) are valid during DisableWeakRefAccess phase. Return the referent which is valid before processing FinalizerReference can eliminate most BlockingOnWeakGlobalAccess.

Implementation and usage

Our solution is implemented on CC GC, based on branch android15-qpr2-release of ART. Branch mark-state-snapshot in this repo contains the implementation.

Compile com.android.art.capex

Touch Build scripts build.sh

#!/bin/bash
export SKIP_DOWNLOAD_OPERATOR_APPS=true
export XMS_BULDER_DISABLED=true
export SKIP_DOWNLOAD_DECOUPLED_APPS=true
export SKIP_DOWNLOAD_CUST_APPS=true
export SKIP_DOWNLOAD_VENDOR_GOOGLE_APPS=true
export BUILD_TARGET_IS=system

source build/envsetup.sh
lunch aosp_arm64-trunk_staging-userdebug

make com.android.art

Preparation and Compilation

$ git clone [email protected]:ICTPLSys/Android-Art-WeakReference.git
$ cd Android-Art-WeakReference
$ git checkout mark-state-snapshot
$ cd ..

$ mkdir AOSP
$ cd AOSP

$ repo init --partial-clone -b main -u https://android.googlesource.com/platform/manifest
## at least sync build environment of ART
$ repo sync -c -j8

## swith to branch android15-qpr2-release
$ cd art
$ git checkout android15-qpr2-release
$ cp -r ../../Android-Art-WeakReference/runtime ./

$ cd ..
$ ./build.sh

##target is AOSP/out/target/product/generic_arm64/system/apex/com.android.art.capex

Install ART module

$ art_capex="$HOME/AOSP/out/target/product/generic_arm64/system/apex/com.android.art.capex"

$ adb root
$ adb remount
$ adb push $art_capex /system/apex/

$ adb reboot

Then test with Perfetto or other tools.

License

This project is derived from the Android Open Source Project (AOSP) ART, which is licensed under the Apache License 2.0. In accordance with AOSP, this repository and all of its modifications, extensions, and contributions are also distributed under the Apache License 2.0.

Any party that uses, copies, modifies, or distributes this project, in whole or in part, is required to comply with the terms and conditions of the Apache License 2.0.

About

A patch eliminating "Blocking on Weak Global Access"

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •