JavaScript memory limit

2019-01-09 05:35发布

问题:

Is there a maximum amount of data a JavaScript application can store?

I guess this is handled by the browser and each one has its limitation?

If there isn't a limit, will a page file be created? If so, wouldn't that be insecure?

回答1:

AFAIK, there is no upper limit, your script can basically use memory until the system runs out of memory (including swap). No upper limit doesn't mean you have to eat it all, users may not like it.



回答2:

I googled "web browser memory limits" and found a thread with a very good response: https://groups.google.com/d/topic/comp.lang.javascript/332YSnPsw_w/discussion

You can also search comp.lang.javascript for javascript memory limit.

See also this Stack Overflow post: Maximum size of an Array in Javascript, which suggests you can store up to 232-1 = 4,294,967,295 = 4.29 billion elements

(I was trying to find a good answer, and the "there is no upper limit" answer provided here was just silly to me. I cannot run into a production issue for a multi-million dollar project and say to management, "Well, I assumed there is no upper limit and everything would be okay." Try to do a proof-of-concept, e.g. loading lots of combobox controls in your JavaScript UI framework of choice, etc. You may discover your framework has some performance degradation.)

Here are some examples of frameworks with well-known performance degradation:

  1. Angular: Poor change detection approach.
    • For each async event, compare each of the bindings (Model-Dom binding) to its old value to decide if to re-render.
      1. NG1: >2500 watchers, performance grinds to a halt
      2. NG2: the same problem remains but you have a long tiring workaround: Switch to immutables and spread ChangeDetectionStrategy.onPush all over your app to turn off the default problematic strategy
  2. React
    • Again, Immutable collections of JS objects only scale so far.
      1. create-react-app internally uses Immutable.JS, and Immutable.JS can only create about 500k immutable collections before it dies.


回答3:

There are no memory limitations for a Javascript program. Your script can hog all the RAM on your machine. However, it is not recommended to use up all the memory on users machine. If you are dealing with a lot of data I would suggest that you check out caching.



回答4:

Firefox supports the option "javascript.options.mem.max" and if you search on that you can find discussions about sensible values that people have found workable.

Not sure how many people can be bothered going in there and setting it, but speaking for myself I set it to 128000 (which is 128M).



回答5:

I think the memory limitation is from the browser. We can use the DevTools to figure that out. Like in chrome, press F12 and enter window.performance.memory you can see the memory info.

 window.performance.memory

enter image description here