简体   繁体   中英

Heap and Native memory allocation in JavaScript: how managed?

JavaScript has Heap (garbage collected) memory, and Native (Typed Arrays, DOM elements) memory.

Question: is there a balance between the two so that if I want to have a LOT of typed arrays, it works, but simply reduces the heap?

The usual model is that there is memory allocated such that native starts at the top, and heap at the bottom, so to speak. And when more memory is needed, the memory is increased and the native and heap moved to the top/bottom again, with additional memory between. Or some similar approach that trades off between the two.

Odd question, I realize, but I'm writing large programs that are trying to minimize memory usage via Typed Arrays.

Thanks!

If you're trying to minimize memory usage and your code lends itself to typed arrays, then a typed array should use less memory than a regular array. All this memory comes from the same place so your question about heap vs. native doesn't really make sense to me. The more memory you use for typed arrays, the less memory you will have available for anything else that uses memory. Your best bet at optimization is to just use less memory and not worry about what kind of thing is using that memory as memory is memory whether it has a typed array in it or a giant string in it.

Typed arrays are garbage collected just like regular arrays. Typed arrays are more memory efficient only because they use less storage for each element in the array, not because they use a different type of memory.

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