简体   繁体   English

JavaScript有内存堆吗?

[英]Does JavaScript have a memory heap?

In other words, what options do I have to allocate memory in JavaScript? 换句话说,我有什么选择在JavaScript中分配内存?

I know you can allocate memory either globally, or inside function scope. 我知道你可以在全局或在函数范围内分配内存。 Can I allocate memory dynamically? 我可以动态分配内存吗? What does the new operator really mean? new运营商的真正含义是什么?

Edit: here's a specific example. 编辑:这是一个具体的例子。 How would you implement reading an integer value from the user - n , and then read n integers into an array? 你将如何实现从用户读取整数值n ,然后将n整数读入数组?

you can't allocate memory. 你不能分配内存。 you can create objects. 你可以创建对象。 that's what new does. 那是new

now, javascript is a queer creature: functions are also objects in javascript. 现在,javascript是一个奇怪的生物:函数也是javascript中的对象。 So this mean that you can instantiate prettymuch everything using new . 所以这意味着你可以使用new来实例化所有东西。

So, the new operator means that a new object is being created. 因此, new运算符意味着正在创建一个新对象。

Javascript also garbage-collects these variables, just like it happens in java. Javascript也垃圾收集这些变量,就像在java中发生的那样。 So if you know java, it should be easy for you to draw parallels. 因此,如果您了解java,那么您应该很容易绘制相似之处。

cheers, 干杯,

jrh JRH

PS: when you allocate objects, you really are allocating memory. PS:当你分配对象时,你真的在​​分配内存。 Only, you are not doing that explicitly. 只是,你没有明确地这样做。 You can allocate an array, and make it behave like a memory buffer, but that will degrade javascript performance drastically: javascript arrays are not in-memory buffers, they are also objects (like everything else). 您可以分配一个数组,并使其行为类似于内存缓冲区,但这会大大降低javascript性能:javascript数组不是内存缓冲区,它们也是对象(就像其他所有内容一样)。

JavaScript has garbage collection and handles this for you. JavaScript有垃圾收集并为您处理。

However, you can help it by using the delete operator where appropriate. 但是,您可以在适当的位置使用delete运算符来帮助它。

From the Apple JavaScript Coding Guidelines : 来自Apple JavaScript编码指南

Just as you used the new operator to create an object, you should delete objects when you are finished with them, like this: 正如您使用new运算符创建对象一样,您应该在完成对象时删除对象,如下所示:

delete myObjectVariable;

The JavaScript runtime automatically garbage collects objects when their value is set to null. JavaScript运行时自动垃圾在其值设置为null时收集对象。 However, setting an object to null doesn't remove the variable that references the object from memory. 但是,将对象设置为null不会从内存中删除引用该对象的变量。 Using delete ensures that this memory is reclaimed in addition to the memory used by the object itself. 使用delete确保除了对象本身使用的内存之外还回收此内存。 (It is also easier to see places where your allocations and deallocations are unbalanced if you explicitly call delete.) (如果明确调用delete,也可以更容易地看到分配和解除分配不平衡的位置。)

Steve 史蒂夫

Hmmm sounds to me like you are coming from the memory focused language and trying to shoe horn that logic into JS. 嗯,听起来像是你来自以记忆为中心的语言,并试图将这种逻辑用于JS。 Yes JS uses memory (of course), but we have garbage collection to take care of cleaning it all up. 是JS使用内存(当然),但我们有垃圾收集来处理清理它。

If you are after specifics about the guts of memory allocation then you will have to hunt around for that. 如果你正在追踪关于内存分配内容的细节,那么你将不得不四处寻找。 But as a rule thumb, when you use var, new or declaring a new function (or closure) you are gobbling up memory. 但作为一个规则拇指,当你使用var,new或声明一个新函数(或闭包)时,你正在吞噬内存。 You can get vars to null to flag them for garbage collection and you can use the delete keyword too although few do either of these unless they work Server-side (like myself with ASP JScript) where its important. 您可以将vars设置为null以标记它们以进行垃圾收集,并且您也可以使用delete关键字,尽管很少有人执行这些操作,除非它们在服务器端(就像我自己使用ASP JScript)那样重要。

Javascript is really, really friendly — really, too friendly by half! 一半Javascript非常非常友好 - 真的,太友好了一半!

If you have an array of 3 elements, and you want to add a fourth, you can just act as if that array location already exists: 如果你有一个包含3个元素的数组,并且想要添加第四个元素,那么就可以表现为该数组位置已经存在:

    var arr = ['zero', 'one', 'two'];
    // Now you have arr[0], arr[1] and arr[2].
    // arr.length is equal to 3.
.
    // to add arr[8]:
    arr[8] = 'eight';
    // Now you have arr[0] through arr[8]. arr.length is equal to 9.
    // and arr[3] through arr[7] exist, and 
    // are initialized to undefined. (If I remember right.)

So being really specific with memory allocation is unnecessary when adding elelments. 因此,在添加元素时,不需要特别注意内存分配。

No, you don't need to and can't allocate memory. 不,你不需要也不能分配内存。 The JavaScript interpreter does that automatically. JavaScript解释器会自动执行此操作。

To answer the title of the question, if you are to trust in MDN , most JavaScript implementations have a heap: 要回答问题的标题,如果您信任MDN ,大多数JavaScript实现都有一个堆:

Heap

Objects are allocated in a heap which is just a name to denote a large mostly unstructured region of memory. 对象在堆中分配,这只是一个名称,表示大多数非结构化的内存区域。

Several Runtimes Communicating Together 几个运行时一起通信

A web worker or a cross-origin iframe has its own stack, heap, and message queue. Web worker或跨源iframe具有自己的堆栈,堆和消息队列。 Two distinct runtimes can only communicate through sending messages via the postMessage method. 两个不同的运行时只能通过postMessage方法发送消息进行通信。 This method adds a message to the other runtime if the latter listens to message events. 如果后者监听消息事件,则此方法将向另一个运行时添加消息。

For a deeper dive into memory management, there is also an article here although much of this is implementation specific. 为了更深入地了解内存管理,这里还有一篇文章尽管其中大部分内容都是特定于实现的。

You do not need to manually manage memory in Javascript. 您无需在Javascript中手动管理内存。 Both heap and stacks are used under the hood to manage memory and it depends on the implementation. 堆和堆栈都用于管理内存,它取决于实现。 Usually, local variables are on the stack and objects are on the heap. 通常,局部变量位于堆栈上,对象位于堆上。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM