开发者

Determining HTML5 database memory usage

开发者 https://www.devze.com 2023-01-19 15:12 出处:网络
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'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.


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


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/

0

精彩评论

暂无评论...
验证码 换一张
取 消