简体   繁体   English

JavaScript中的堆和本机内存分配:如何管理?

[英]Heap and Native memory allocation in JavaScript: how managed?

JavaScript has Heap (garbage collected) memory, and Native (Typed Arrays, DOM elements) memory. JavaScript具有堆(垃圾收集)内存和本机(类型数组,DOM元素)内存。

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. 类型化的数组具有更高的内存效率,仅是因为它们为数组中的每个元素使用较少的存储,而不是因为它们使用了不同类型的内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript memory 分配:堆和栈 - Javascript memory allocation: heap and stack NodeJS:达到堆限制分配失败 - memory 中的 JavaScript 堆 - NodeJS: Reached heap limit Allocation failed - JavaScript heap out of memory AWS Lambda-Nodejs:分配失败-JavaScript堆内存不足 - AWS Lambda - Nodejs: Allocation failed - JavaScript heap out of memory NodeJS:无效的数组长度分配失败 - JavaScript 堆内存不足 - NodeJS: invalid array length Allocation failed - JavaScript heap out of memory 半空间复制分配失败,javascript 堆内存不足 - semi-space copy allocation failed, javascript heap out of memory 分配失败 - loopback.js 中的 javascript 堆内存不足 - allocation failed - javascript heap out of memory in loopback.js JavaScript内存:堆,本机,WebGL / GPU - JavaScript Memory: Heap, Native, WebGL/GPU 接近堆限制的无效标记压缩分配失败 - JavaScript 堆内存不足 - Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory on expo 致命错误:达到堆限制分配失败 - memory 中的 JavaScript 堆 - FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory 接近堆限制的无效标记压缩分配失败 - JavaScript 堆内存不足 - Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory in angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM