确定HTML5数据库内存使用情况 [英] Determining HTML5 database memory usage

查看:171
本文介绍了确定HTML5数据库内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在创建数据库时,需要设置最大大小(我使用了5MB,正如许多例子中所建议的那样)

我想知道我真的使用了多少内存(例如添加1000条记录之后)一个想法将达到5MB的限制,并采取相应的行动。

Chrome控制台不会显示这些数字。
Thanks。

解决方案

如果您愿意,可以计算这些数字。基本上,localStorage和webStorage的默认限制是5MB,其中名称和值保存为UTF16,因此它实际上是存储字符2.5 MB的一半。在webStorage中,您可以通过在清单中添加unlimited_storage来增加它。



在WebStorage中同样适用,但您必须查看所有表格并弄清楚



在localStorage中,您可以通过执行一个填充脚本来测试:

  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('填充行:'+ row);
populator();
}
populator();

上面的代码会在第25行崩溃,因为没有足够的空间使它大约在2.5MB左右。你可以做相反的事,并计算每行有多少字符,并确定你有多少空间。

另一种方法是添加有效载荷和检查异常如果它存在,如果它存在,那么你知道你的空间不足。

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

Internet Explorer执行了一项名为remainingSpace的操作,但不起作用Chrome / Safari:
http:// msdn.microsoft.com/en-us/library/cc197016(v=VS.85).aspx


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

这篇关于确定HTML5数据库内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆