0

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!

4
  • Why do you think that typed arrays are not allocated from a heap? All of this is implementation-specific to a particular browser. Commented Nov 15, 2013 at 23:37
  • See developer.chrome.com/docs/devtools: Memory for new JavaScript objects is allocated from a dedicated JavaScript heap (or VM heap).These objects are managed by V8's garbage collector and therefore, will stay alive as long as there is at least one strong reference to them. Native objects are everything else which is not in the JavaScript heap. Native object, in contrast to heap object, is not managed by the V8 garbage collector throughout it’s lifetime, and can only be accessed from JavaScript using its JavaScript wrapper object. Commented Nov 16, 2013 at 4:56
  • .. BTW: I agree about implementation, I should have said in my original post that chrome was my interest. But none the less, I've seen other references to the difference between Native and Heap memory so I think this holds for other browsers. Commented Nov 16, 2013 at 4:57
  • 1
    Why do you think a typed array is a "native object" and not allocated from the heap and not garbage collected? DOM elements are managed by the browser, not by JS so they are a different beast, but they are also garbage collected (by the browser). I'm asking you these questions because I think you're working on some false assumptions. Commented Nov 16, 2013 at 4:58

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I think I agree. Paul Irish had a chrome Profiler experimental extension that would let you see heap vs "native" memory, but it was pulled a while back. So I got interested in the difference. I know all the "native" memory have a heap reference, thus as you say can be deallocated when the heap object is collected. One fascinating exception however: the GPU. It has its own collection of typed arrays which I do not think are part of the cpu memory space. Unfortunately, its memory is fast to store but slow to sample.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.