diff options
author | rmcilroy@chromium.org <rmcilroy@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-07-18 18:49:14 +0000 |
---|---|---|
committer | rmcilroy@chromium.org <rmcilroy@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-07-18 18:49:14 +0000 |
commit | be7995f117df5ca5d4b60d918cd419aa08724e53 (patch) | |
tree | 1e1448f36dd539797e51d079f3e42e7ad2bb56fb /sql | |
parent | 3b0403687b604e396d57fc4eea6029b02de030ee (diff) | |
download | chromium_src-be7995f117df5ca5d4b60d918cd419aa08724e53.zip chromium_src-be7995f117df5ca5d4b60d918cd419aa08724e53.tar.gz chromium_src-be7995f117df5ca5d4b60d918cd419aa08724e53.tar.bz2 |
Adds a MemoryPressureListener to sql::Connection.
This will allow SQLite database to reduce their page cache memory on systems
which provide a MemoryPressureListener signal.
BUG=243769
Review URL: https://chromiumcodereview.appspot.com/17071007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@212374 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'sql')
-rw-r--r-- | sql/connection.cc | 29 | ||||
-rw-r--r-- | sql/connection.h | 6 |
2 files changed, 35 insertions, 0 deletions
diff --git a/sql/connection.cc b/sql/connection.cc index d11b40c..e7b6fe1 100644 --- a/sql/connection.cc +++ b/sql/connection.cc @@ -279,6 +279,35 @@ void Connection::Preload() { #endif } +void Connection::TrimMemory(bool aggressively) { + if (!db_) + return; + + // TODO(shess): investigate using sqlite3_db_release_memory() when possible. + int original_cache_size; + { + Statement sql_get_original(GetUniqueStatement("PRAGMA cache_size")); + if (!sql_get_original.Step()) { + DLOG(WARNING) << "Could not get cache size " << GetErrorMessage(); + return; + } + original_cache_size = sql_get_original.ColumnInt(0); + } + int shrink_cache_size = aggressively ? 1 : (original_cache_size / 2); + + // Force sqlite to try to reduce page cache usage. + const std::string sql_shrink = + base::StringPrintf("PRAGMA cache_size=%d", shrink_cache_size); + if (!Execute(sql_shrink.c_str())) + DLOG(WARNING) << "Could not shrink cache size: " << GetErrorMessage(); + + // Restore cache size. + const std::string sql_restore = + base::StringPrintf("PRAGMA cache_size=%d", original_cache_size); + if (!Execute(sql_restore.c_str())) + DLOG(WARNING) << "Could not restore cache size: " << GetErrorMessage(); +} + // Create an in-memory database with the existing database's page // size, then backup that database over the existing database. bool Connection::Raze() { diff --git a/sql/connection.h b/sql/connection.h index ab80a6c..3ee91ef 100644 --- a/sql/connection.h +++ b/sql/connection.h @@ -190,6 +190,12 @@ class SQL_EXPORT Connection { // generally exist either. void Preload(); + // Try to trim the cache memory used by the database. If |aggressively| is + // true, this function will try to free all of the cache memory it can. If + // |aggressively| is false, this function will try to cut cache memory + // usage by half. + void TrimMemory(bool aggressively); + // Raze the database to the ground. This approximates creating a // fresh database from scratch, within the constraints of SQLite's // locking protocol (locks and open handles can make doing this with |