Native allocation accounting
Added two new functions: registerNativeAllocation and registerNativeFree.
These functions should be used to let the GC know about native allocations
which are held live by Java objects and released in finalizers. GC are performed
or requested from within registerNativeAllocation when the total number of native
bytes accounted for exceeds a certain threshold. After a GC occurs in
registerNativeAllocation, finalizers are run so that the native memory is freed.
Added a test which shows how to use these functions.
Change-Id: I40f3c79e1c02d5008dec7d58d61c5bb97ec2fc1b
diff --git a/src/native/dalvik_system_VMRuntime.cc b/src/native/dalvik_system_VMRuntime.cc
index ce3cc93..baae8a3 100644
--- a/src/native/dalvik_system_VMRuntime.cc
+++ b/src/native/dalvik_system_VMRuntime.cc
@@ -165,6 +165,24 @@
}
}
+static void VMRuntime_registerNativeAllocation(JNIEnv* env, jobject, jint bytes) {
+ ScopedObjectAccess soa(env);
+ if (bytes < 0) {
+ ThrowRuntimeException("allocation size negative %d", bytes);
+ return;
+ }
+ Runtime::Current()->GetHeap()->RegisterNativeAllocation(bytes);
+}
+
+static void VMRuntime_registerNativeFree(JNIEnv* env, jobject, jint bytes) {
+ ScopedObjectAccess soa(env);
+ if (bytes < 0) {
+ ThrowRuntimeException("allocation size negative %d", bytes);
+ return;
+ }
+ Runtime::Current()->GetHeap()->RegisterNativeFree(bytes);
+}
+
static void VMRuntime_trimHeap(JNIEnv*, jobject) {
uint64_t start_ns = NanoTime();
@@ -210,10 +228,13 @@
NATIVE_METHOD(VMRuntime, newNonMovableArray, "(Ljava/lang/Class;I)Ljava/lang/Object;"),
NATIVE_METHOD(VMRuntime, properties, "()[Ljava/lang/String;"),
NATIVE_METHOD(VMRuntime, setTargetSdkVersion, "(I)V"),
+ NATIVE_METHOD(VMRuntime, registerNativeAllocation, "(I)V"),
+ NATIVE_METHOD(VMRuntime, registerNativeFree, "(I)V"),
NATIVE_METHOD(VMRuntime, startJitCompilation, "()V"),
NATIVE_METHOD(VMRuntime, trimHeap, "()V"),
NATIVE_METHOD(VMRuntime, vmVersion, "()Ljava/lang/String;"),
NATIVE_METHOD(VMRuntime, vmLibrary, "()Ljava/lang/String;"),
+
};
void register_dalvik_system_VMRuntime(JNIEnv* env) {