JavaScript中的堆和本机内存分配:如何管理? [英] Heap and Native memory allocation in JavaScript: how managed?

查看:83
本文介绍了JavaScript中的堆和本机内存分配:如何管理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript有Heap(垃圾收集)内存和Native(Typed Arrays,DOM元素)内存。

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.

奇怪的问题,我意识到,但我正在编写大型程序,试图通过Typed Arrays来最小化内存使用。

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

谢谢!

推荐答案

如果你想减少记忆用法和你的代码适用于类型化数组,然后类型化数组应该使用比常规数组更少的内存。所有这些内存都来自同一个地方所以你对堆与本机的问题对我来说并没有多大意义。用于类型化数组的内存越多,对于使用内存的任何其他内容,可用的内存就越少。优化的最佳选择是只使用更少的内存而不用担心使用该内存是什么样的内存,因为内存是内存,无论它是否有一个类型化的数组或其中有一个巨大的字符串。

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.

这篇关于JavaScript中的堆和本机内存分配:如何管理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆