简体   繁体   中英

how much data can javascript in the browser hold?

I have created a JSFiddle to see how much data I can push into my browser.

The link is http://jsfiddle.net/GWxAk/

The code is simple. It's just trying to push as many strings as possible into an array. The strings have an approximate length of 300-310 characters.

My question is: does the result depend on how much memory I have got on my PC ? does it really differ browser to browser ?

For instance if I have 8gb of ram will I get much more then if I have 4gb ?

var s = '';
for (var i = 0; i < 300; i++) {
    s += 'a';
}

array = [];
count = 0;

function doMore() {
    for (var i = 0; i < 1000; i++) {
        count++;
        array.push(s + count);
    }
};

function repeat() {
    doMore();
    document.body.innerHTML = 'size:' + array.length;
    setTimeout(repeat, 100);
}

repeat();

In my case chrome hangs at 14850000 and I have 4gb of ram That is an array of almost 15 million items. Not bad I guess

Do you guys get the same ? Can somebody tell how to give as much memory as possible to the browser

Thanks

Again, my machine with 16GB of RAM. I can watch the browser RAM usage climb as it increases, so I would assume it's limited by RAM as well.

IE crapped out at 16,840,000
Chrome at 14,850,000
Firefox 32,890,000
Safari recycles itself around 8,720,000 (LOL @ Apple)

Here is a screenshot of memory usage and firefox http://screencast.com/t/3Xl31yGgHWC

Lets assume UTF8, meaning your 'a' is 2 bytes/8bits.

  • 14,850,000 * 300 = 4455000000 characters

  • 14850000 * 300 * 2 = 8,910,000,000 bytes

  • 8910000000 /1024 = 8,701,171.875 KB
  • (8910000000 /1024 ) / 1024 = 8,497.238159179688 MB
  • ((8910000000 /1024 ) / 1024 ) / 1024 = 8.298084139823914 GB

As such we can surmise from your test that the maximum length of a string in Chromes JS engine is 4,455,000,000 characters, or ~ 8.3 GB in memory.

But ofcourse this is not what's happening. You only have 4GB of RAM yet ~4298MB has appeared out of nowhere according to the figures, and there's the structures of the array variable itself and the java VM and chrome itself ot account for etc etc

Not to mention that you're pushing s+count not s on its own, so the length of the string being added is rising as the number of digits in count increases. If s was the same, then its likely the value would be interned to save memory by the V8 engine. For reference, the number of additional characters added because of the count variable, and due to it's non linear increase in length, is 9,7438,889 characters or 185.85MB of data.

So something else must be happening here.

As for the limits of the V8 JS engine:

http://code.google.com/p/v8/issues/detail?id=847

The 32bit memory address space is the upper limit, and for 64bit, that link suggest ~1.9GB although it's very much likely to be the upper limit of what your OS can support and is physically available.

So to summarise:

  • 32bit will always be an upper bound, not of your specific js variable, but of the entire bundle of js VM, renderer, page contents, etc
  • Your test is not quite reliable as the items it is counting are not identical
  • If they were identical, you would fall foul of special case handling of strings

edit:

我在Chrome上,我尝试了你的测试,它挂在你提到的相同的14850000上,即使我只有2GB的内存,我在我的linux安装中运行一个带有Windows的虚拟机,我给了700Mb的内存,所以我想是的铬有限制

On a 32-bit machine your upper limit will always be 4GB, no if ands or buts. In practice, it will be wildly different machine to machine (not unusual to have 10+ tabs open)

Best way to go: keep only the data your user is actively working on or anything you can't quickly retrieve from the server handy. Everything else, get when the user asks for it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM