views:

43

answers:

2

I'm adding sqlite support to a my Google Chrome extension, to store historical data.

When creating the database, it is required to set the maximum size (I used 5MB, as suggested in many examples)

I'd like to know how much memory I'm really using (for example after adding 1000 records), to have an idea of when the 5MB limit will be reached, and act accordingly.

The Chrome console doesn't reveal such figures. Thanks.

A: 

I'd like to add a suggestion.

If it is a Chrome extension, why not make use of Web SQL storage or Indexed DB?

http://html5doctor.com/introducing-web-sql-databases/

http://hacks.mozilla.org/2010/06/comparing-indexeddb-and-webdatabase/

Source: http://caniuse.com/

Dave
Yes, I'm using Web SQL ( I wrote "sqlite", I thought it was the same ) I think Mohamed wrote mainly about local storage because they have the same limits and was easier to make the example.
UVL
Yes, was easier to make the example :) Chrome uses SQLite as its database storage for WebStorage and LocalStorage. But they both apply have the same implications.
Mohamed Mansour
@UVL. Web SQL is different from SQLite. A specific browser or can use SQLite as its WebSQL, localStorage and IndexedDB. But it is a matter of implementation, and both are different things. This is why I suggested you using the API version (as I thought you were using a standalone implementation).
Dave
+1  A: 

Hi UVL,

You can calculate those figures if you wanted to. Basically, the default limit for localStorage and webStorage is 5MB where the name and values are saved as UTF16 therefore it is really half of that which is 2.5 MB in terms of stored characters. In webStorage, you can increase that by adding "unlimited_storage" within the manifest.

Same thing would apply in WebStorage, but you have to go through all tables and figure out how many characters there is per row.

In localStorage You can test that by doing a population script:

var row = 0;
localStorage.clear();
var populator = function () { 
  localStorage[row] = '';
  var x = ''; 
  for (var i = 0; i < (1024 * 100); i++) { 
    x += 'A'; 
  } 
  localStorage[row] = x; 
  row++;
  console.log('Populating row: ' + row); 
  populator();
}
populator();

The above should crash in row 25 for not enough space making it around 2.5MB. You can do the inverse and count how many characters per row and that determines how much space you have.

Another way to do this, is always adding a "payload" and checking the exception if it exists, if it does, then you know your out of space.

try {
  localStorage['foo'] = 'SOME_DATA';
} catch (e) {
  console.log('LIMIT REACHED! Do something else');
}

Internet Explorer did something called "remainingSpace", but that doesn't work in Chrome/Safari: http://msdn.microsoft.com/en-us/library/cc197016(v=VS.85).aspx

Mohamed Mansour
@nice reply. thx for sharing.
Dave