2012-09-19 12:21:19 -07:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
package org.mozilla.gecko;
|
|
|
|
|
2014-07-30 08:19:35 -07:00
|
|
|
import org.mozilla.gecko.AppConstants.Versions;
|
2012-10-19 17:36:21 -07:00
|
|
|
import org.mozilla.gecko.db.BrowserContract;
|
2013-04-08 00:51:56 -07:00
|
|
|
import org.mozilla.gecko.db.BrowserDB;
|
2013-09-12 07:48:59 -07:00
|
|
|
import org.mozilla.gecko.favicons.Favicons;
|
2013-03-15 03:52:53 -07:00
|
|
|
import org.mozilla.gecko.util.ThreadUtils;
|
2012-10-16 07:07:06 -07:00
|
|
|
|
2012-09-19 12:21:19 -07:00
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.ComponentCallbacks2;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a utility class to keep track of how much memory and disk-space pressure
|
|
|
|
* the system is under. It receives input from GeckoActivity via the onLowMemory() and
|
|
|
|
* onTrimMemory() functions, and also listens for some system intents related to
|
|
|
|
* disk-space notifications. Internally it will track how much memory and disk pressure
|
|
|
|
* the system is under, and perform various actions to help alleviate the pressure.
|
|
|
|
*
|
|
|
|
* Note that since there is no notification for when the system has lots of free memory
|
|
|
|
* again, this class also assumes that, over time, the system will free up memory. This
|
|
|
|
* assumption is implemented using a timer that slowly lowers the internal memory
|
|
|
|
* pressure state if no new low-memory notifications are received.
|
|
|
|
*
|
|
|
|
* Synchronization note: MemoryMonitor contains an inner class PressureDecrementer. Both
|
|
|
|
* of these classes may be accessed from various threads, and have both been designed to
|
|
|
|
* be thread-safe. In terms of lock ordering, code holding the PressureDecrementer lock
|
|
|
|
* is allowed to pick up the MemoryMonitor lock, but not vice-versa.
|
|
|
|
*/
|
|
|
|
class MemoryMonitor extends BroadcastReceiver {
|
|
|
|
private static final String LOGTAG = "GeckoMemoryMonitor";
|
2012-09-20 09:38:35 -07:00
|
|
|
private static final String ACTION_MEMORY_DUMP = "org.mozilla.gecko.MEMORY_DUMP";
|
2012-09-27 09:29:47 -07:00
|
|
|
private static final String ACTION_FORCE_PRESSURE = "org.mozilla.gecko.FORCE_MEMORY_PRESSURE";
|
2012-09-19 12:21:19 -07:00
|
|
|
|
2014-07-21 10:22:45 -07:00
|
|
|
// Memory pressure levels. Keep these in sync with those in AndroidJavaWrappers.h
|
2012-09-19 12:21:19 -07:00
|
|
|
private static final int MEMORY_PRESSURE_NONE = 0;
|
|
|
|
private static final int MEMORY_PRESSURE_CLEANUP = 1;
|
|
|
|
private static final int MEMORY_PRESSURE_LOW = 2;
|
|
|
|
private static final int MEMORY_PRESSURE_MEDIUM = 3;
|
|
|
|
private static final int MEMORY_PRESSURE_HIGH = 4;
|
|
|
|
|
|
|
|
private static MemoryMonitor sInstance = new MemoryMonitor();
|
|
|
|
|
|
|
|
static MemoryMonitor getInstance() {
|
|
|
|
return sInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
private final PressureDecrementer mPressureDecrementer;
|
2014-07-21 10:22:45 -07:00
|
|
|
private int mMemoryPressure; // Synchronized access only.
|
|
|
|
private volatile boolean mStoragePressure; // Accessed via UI thread intent, background runnables.
|
2014-02-27 12:32:34 -08:00
|
|
|
private boolean mInited;
|
2012-09-19 12:21:19 -07:00
|
|
|
|
|
|
|
private MemoryMonitor() {
|
|
|
|
mPressureDecrementer = new PressureDecrementer();
|
|
|
|
mMemoryPressure = MEMORY_PRESSURE_NONE;
|
|
|
|
}
|
|
|
|
|
2014-02-27 12:32:34 -08:00
|
|
|
public void init(final Context context) {
|
|
|
|
if (mInited) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:21:19 -07:00
|
|
|
IntentFilter filter = new IntentFilter();
|
|
|
|
filter.addAction(Intent.ACTION_DEVICE_STORAGE_LOW);
|
|
|
|
filter.addAction(Intent.ACTION_DEVICE_STORAGE_OK);
|
2012-09-20 09:38:35 -07:00
|
|
|
filter.addAction(ACTION_MEMORY_DUMP);
|
2012-09-27 09:29:47 -07:00
|
|
|
filter.addAction(ACTION_FORCE_PRESSURE);
|
2012-09-19 12:21:19 -07:00
|
|
|
context.getApplicationContext().registerReceiver(this, filter);
|
2014-02-27 12:32:34 -08:00
|
|
|
mInited = true;
|
2012-09-19 12:21:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onLowMemory() {
|
|
|
|
Log.d(LOGTAG, "onLowMemory() notification received");
|
2013-01-30 10:53:02 -08:00
|
|
|
if (increaseMemoryPressure(MEMORY_PRESSURE_HIGH)) {
|
|
|
|
// We need to wait on Gecko here, because if we haven't reduced
|
|
|
|
// memory usage enough when we return from this, Android will kill us.
|
2013-03-10 15:43:30 -07:00
|
|
|
GeckoAppShell.sendEventToGeckoSync(GeckoEvent.createNoOpEvent());
|
2013-01-30 10:53:02 -08:00
|
|
|
}
|
2012-09-19 12:21:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void onTrimMemory(int level) {
|
|
|
|
Log.d(LOGTAG, "onTrimMemory() notification received with level " + level);
|
2014-07-30 08:19:35 -07:00
|
|
|
if (Versions.preICS) {
|
|
|
|
// This won't even get called pre-ICS.
|
2012-09-19 12:21:19 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (level >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE) {
|
|
|
|
increaseMemoryPressure(MEMORY_PRESSURE_HIGH);
|
|
|
|
} else if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
|
|
|
|
increaseMemoryPressure(MEMORY_PRESSURE_MEDIUM);
|
|
|
|
} else if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
|
|
|
|
// includes TRIM_MEMORY_BACKGROUND
|
|
|
|
increaseMemoryPressure(MEMORY_PRESSURE_CLEANUP);
|
|
|
|
} else {
|
2013-01-30 10:52:49 -08:00
|
|
|
// levels down here mean gecko is the foreground process so we
|
|
|
|
// should be less aggressive with wiping memory as it may impact
|
|
|
|
// user experience.
|
|
|
|
increaseMemoryPressure(MEMORY_PRESSURE_LOW);
|
2012-09-19 12:21:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
if (Intent.ACTION_DEVICE_STORAGE_LOW.equals(intent.getAction())) {
|
|
|
|
Log.d(LOGTAG, "Device storage is low");
|
|
|
|
mStoragePressure = true;
|
2013-03-15 03:52:53 -07:00
|
|
|
ThreadUtils.postToBackgroundThread(new StorageReducer(context));
|
2012-09-19 12:21:19 -07:00
|
|
|
} else if (Intent.ACTION_DEVICE_STORAGE_OK.equals(intent.getAction())) {
|
|
|
|
Log.d(LOGTAG, "Device storage is ok");
|
|
|
|
mStoragePressure = false;
|
2012-09-20 09:38:35 -07:00
|
|
|
} else if (ACTION_MEMORY_DUMP.equals(intent.getAction())) {
|
|
|
|
String label = intent.getStringExtra("label");
|
|
|
|
if (label == null) {
|
|
|
|
label = "default";
|
|
|
|
}
|
|
|
|
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Memory:Dump", label));
|
2012-09-27 09:29:47 -07:00
|
|
|
} else if (ACTION_FORCE_PRESSURE.equals(intent.getAction())) {
|
|
|
|
increaseMemoryPressure(MEMORY_PRESSURE_HIGH);
|
2012-09-19 12:21:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 10:53:02 -08:00
|
|
|
private boolean increaseMemoryPressure(int level) {
|
2012-09-19 12:21:19 -07:00
|
|
|
int oldLevel;
|
|
|
|
synchronized (this) {
|
|
|
|
// bump up our level if we're not already higher
|
|
|
|
if (mMemoryPressure > level) {
|
2013-01-30 10:53:02 -08:00
|
|
|
return false;
|
2012-09-19 12:21:19 -07:00
|
|
|
}
|
|
|
|
oldLevel = mMemoryPressure;
|
|
|
|
mMemoryPressure = level;
|
|
|
|
}
|
|
|
|
|
|
|
|
// since we don't get notifications for when memory pressure is off,
|
|
|
|
// we schedule our own timer to slowly back off the memory pressure level.
|
|
|
|
// note that this will reset the time to next decrement if the decrementer
|
|
|
|
// is already running, which is the desired behaviour because we just got
|
|
|
|
// a new low-mem notification.
|
|
|
|
mPressureDecrementer.start();
|
|
|
|
|
|
|
|
if (oldLevel == level) {
|
|
|
|
// if we're not going to a higher level we probably don't
|
|
|
|
// need to run another round of the same memory reductions
|
|
|
|
// we did on the last memory pressure increase.
|
2013-01-30 10:53:02 -08:00
|
|
|
return false;
|
2012-09-19 12:21:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO hook in memory-reduction stuff for different levels here
|
|
|
|
if (level >= MEMORY_PRESSURE_MEDIUM) {
|
2013-06-17 14:09:09 -07:00
|
|
|
//Only send medium or higher events because that's all that is used right now
|
2013-01-23 06:38:57 -08:00
|
|
|
if (GeckoThread.checkLaunchState(GeckoThread.LaunchState.GeckoRunning)) {
|
2013-11-27 03:52:05 -08:00
|
|
|
GeckoAppShell.dispatchMemoryPressure();
|
2012-09-19 12:21:19 -07:00
|
|
|
}
|
2013-01-29 10:24:43 -08:00
|
|
|
|
2013-09-12 07:49:20 -07:00
|
|
|
Favicons.clearMemCache();
|
2012-09-19 12:21:19 -07:00
|
|
|
}
|
2013-01-30 10:53:02 -08:00
|
|
|
return true;
|
2012-09-19 12:21:19 -07:00
|
|
|
}
|
|
|
|
|
2014-07-21 10:22:45 -07:00
|
|
|
/**
|
|
|
|
* Thread-safe due to mStoragePressure's volatility.
|
|
|
|
*/
|
|
|
|
boolean isUnderStoragePressure() {
|
|
|
|
return mStoragePressure;
|
|
|
|
}
|
|
|
|
|
2012-09-25 12:46:17 -07:00
|
|
|
private boolean decreaseMemoryPressure() {
|
|
|
|
int newLevel;
|
|
|
|
synchronized (this) {
|
|
|
|
if (mMemoryPressure <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
newLevel = --mMemoryPressure;
|
2012-09-19 12:21:19 -07:00
|
|
|
}
|
2012-09-25 12:46:17 -07:00
|
|
|
Log.d(LOGTAG, "Decreased memory pressure to " + newLevel);
|
|
|
|
|
|
|
|
return true;
|
2012-09-19 12:21:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
class PressureDecrementer implements Runnable {
|
|
|
|
private static final int DECREMENT_DELAY = 5 * 60 * 1000; // 5 minutes
|
|
|
|
|
|
|
|
private boolean mPosted;
|
|
|
|
|
|
|
|
synchronized void start() {
|
|
|
|
if (mPosted) {
|
|
|
|
// cancel the old one before scheduling a new one
|
2013-03-15 03:52:53 -07:00
|
|
|
ThreadUtils.getBackgroundHandler().removeCallbacks(this);
|
2012-09-19 12:21:19 -07:00
|
|
|
}
|
2013-03-15 03:52:53 -07:00
|
|
|
ThreadUtils.getBackgroundHandler().postDelayed(this, DECREMENT_DELAY);
|
2012-09-19 12:21:19 -07:00
|
|
|
mPosted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public synchronized void run() {
|
|
|
|
if (!decreaseMemoryPressure()) {
|
|
|
|
// done decrementing, bail out
|
|
|
|
mPosted = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// need to keep decrementing
|
2013-03-15 03:52:53 -07:00
|
|
|
ThreadUtils.getBackgroundHandler().postDelayed(this, DECREMENT_DELAY);
|
2012-09-19 12:21:19 -07:00
|
|
|
}
|
|
|
|
}
|
2012-10-16 07:07:06 -07:00
|
|
|
|
2014-07-21 10:22:45 -07:00
|
|
|
private static class StorageReducer implements Runnable {
|
2012-10-21 09:32:39 -07:00
|
|
|
private final Context mContext;
|
|
|
|
public StorageReducer(final Context context) {
|
|
|
|
this.mContext = context;
|
|
|
|
}
|
|
|
|
|
2012-10-16 07:07:06 -07:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
// this might get run right on startup, if so wait 10 seconds and try again
|
2013-01-23 06:38:57 -08:00
|
|
|
if (!GeckoThread.checkLaunchState(GeckoThread.LaunchState.GeckoRunning)) {
|
2013-03-15 03:52:53 -07:00
|
|
|
ThreadUtils.getBackgroundHandler().postDelayed(this, 10000);
|
2012-10-16 07:07:06 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-21 10:22:45 -07:00
|
|
|
if (!MemoryMonitor.getInstance().isUnderStoragePressure()) {
|
|
|
|
// Pressure is off, so we can abort.
|
2012-10-16 07:07:06 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-19 17:36:21 -07:00
|
|
|
BrowserDB.expireHistory(mContext.getContentResolver(),
|
|
|
|
BrowserContract.ExpirePriority.AGGRESSIVE);
|
2013-02-18 13:17:54 -08:00
|
|
|
BrowserDB.removeThumbnails(mContext.getContentResolver());
|
2012-10-16 07:07:06 -07:00
|
|
|
// TODO: drop or shrink disk caches
|
|
|
|
}
|
|
|
|
}
|
2012-09-19 12:21:19 -07:00
|
|
|
}
|