From f7093befbbc64d324b6d4c41e98ae69bbed4cd31 Mon Sep 17 00:00:00 2001 From: Ben Murdoch Date: Mon, 1 Nov 2010 10:27:33 +0000 Subject: Calculate memory thresholds that V8 can use to control GC. V8 can be smarter about the memory it uses and the frequency of garbage collection but we need to inform it of the values to use. As this varies device to device look it up and store it on the WebViewCore to be read by native code over JNI. See also https://android-git.corp.google.com/g/#change,78691 Bug: 3075565 Change-Id: Ib52f4df775b80b386fc98f1d71b5687f01c12b2e --- core/java/android/webkit/WebViewCore.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'core/java') diff --git a/core/java/android/webkit/WebViewCore.java b/core/java/android/webkit/WebViewCore.java index 1b7eb2a..6e927a6 100644 --- a/core/java/android/webkit/WebViewCore.java +++ b/core/java/android/webkit/WebViewCore.java @@ -16,6 +16,7 @@ package android.webkit; +import android.app.ActivityManager; import android.content.Context; import android.content.pm.PackageManager.NameNotFoundException; import android.database.Cursor; @@ -125,6 +126,9 @@ final class WebViewCore { private DeviceMotionService mDeviceMotionService; private DeviceOrientationService mDeviceOrientationService; + private int mLowMemoryUsageThresholdMb; + private int mHighMemoryUsageThresholdMb; + // The thread name used to identify the WebCore thread and for use in // debugging other classes that require operation within the WebCore thread. /* package */ static final String THREAD_NAME = "WebViewCoreThread"; @@ -170,6 +174,23 @@ final class WebViewCore { WebStorage.getInstance().createUIHandler(); // Create the UI handler for GeolocationPermissions GeolocationPermissions.getInstance().createUIHandler(); + + // Get the memory class of the current device. V8 will use these values + // to GC more effectively. + ActivityManager manager = (ActivityManager) mContext.getSystemService( + Context.ACTIVITY_SERVICE); + ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo(); + manager.getMemoryInfo(memInfo); + + // Allow us to use up to our memory class value before V8's GC kicks in. + // These values have been determined by experimentation. + mLowMemoryUsageThresholdMb = manager.getMemoryClass(); + // If things get crazy, allow V8 to use up to 3 times our memory class, or a third of the + // device's total available memory, whichever is smaller. At that point V8 will start + // attempting more aggressive garbage collection. + mHighMemoryUsageThresholdMb = Math.min(mLowMemoryUsageThresholdMb * 3, + (int) (memInfo.availMem / 3) >> 20); + // Send a message to initialize the WebViewCore. Message init = sWebCoreHandler.obtainMessage( WebCoreThread.INITIALIZE, this); -- cgit v1.1