简体   繁体   中英

Does JavaScript have a memory heap?

In other words, what options do I have to allocate memory in 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?

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?

you can't allocate memory. you can create objects. that's what new does.

now, javascript is a queer creature: functions are also objects in javascript. So this mean that you can instantiate prettymuch everything using new .

So, the new operator means that a new object is being created.

Javascript also garbage-collects these variables, just like it happens in java. So if you know java, it should be easy for you to draw parallels.

cheers,

jrh

PS: when you allocate objects, you really are allocating memory. 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 has garbage collection and handles this for you.

However, you can help it by using the delete operator where appropriate.

From the Apple JavaScript Coding Guidelines :

Just as you used the new operator to create an object, you should delete objects when you are finished with them, like this:

delete myObjectVariable;

The JavaScript runtime automatically garbage collects objects when their value is set to null. However, setting an object to null doesn't remove the variable that references the object from memory. Using delete ensures that this memory is reclaimed in addition to the memory used by the object itself. (It is also easier to see places where your allocations and deallocations are unbalanced if you explicitly call delete.)

Steve

Hmmm sounds to me like you are coming from the memory focused language and trying to shoe horn that logic into JS. Yes JS uses memory (of course), but we have garbage collection to take care of cleaning it all up.

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. 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.

Javascript is really, really friendly — really, too friendly by half!

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:

    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.

To answer the title of the question, if you are to trust in MDN , most JavaScript implementations have a heap:

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. Two distinct runtimes can only communicate through sending messages via the postMessage method. 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. 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.

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