5

Should I free the allocated memory by myself, or is there a kind of garbage collector?

Is it okay to use the following code in JavaScript?

function fillArray()
{
  var c = new Array;
  c.push(3);
  c.push(2);
  return c;
}

var arr = fillArray();
var d = arr.pop()

thanks

Diptendu
  • 2,120
  • 1
  • 15
  • 28

3 Answers3

8

Quoted from the Apple JavaScript Coding Guidelines:

Use delete statements. Whenever you create an object using a new statement, pair it with a delete statement. This ensures that all of the memory associated with the object, including its property name, is available for garbage collection. The delete statement is discussed more in “Freeing Objects.”

This would suggest that you use a delete command to then allow the garbage collector to free the memory allocated for your Array when you're finished using it. The point that the delete statement only removes a reference is worth noting in that it differs from the behaviour in C/C++, where there is no garbage collection and delete immediately frees up the memory.

akdom
  • 32,264
  • 27
  • 73
  • 79
Noldorin
  • 144,213
  • 56
  • 264
  • 302
  • 3
    Just keep in mind that the "delete" operator doesn't actually 'delete' an object—it just removes a reference to the object so that JavaScript's Garbage Collector can easily see that the object is not being used anymore. For more information, see the following question: http://stackoverflow.com/questions/742623/deleting-objects-in-javascript. – Steve Harrison May 15 '09 at 08:29
  • The `delete` operator removes a property from an object, so does not work on variables. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete – Alex Mund Jun 25 '15 at 14:30
3

Memory management in JavaScript is automatic and there is a garbage collector (GC).

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management

You cannot explicitly delete the variables d and arr, but you can remove references to their value by setting the variables to something else, such as null, to allow the GC to remove them from memory.

arr = null;
d = null;

Note that the delete keyword only deletes object properties.

Alex Mund
  • 431
  • 3
  • 9
3

The variables arr and d will exist as global variables and will exist until they are collected by the Garbage Collector.

The variables will be set as properties on the global object i.e. window in a browser environment but since they are declared with var, they will not be deletable from the global object.

In your particular case, the best course of action might be to assign null to the variables after you are finished with them. You may also want to consider containing their scope to a function and do what you need to do with them inside that function.

Russ Cam
  • 124,184
  • 33
  • 204
  • 266
  • This is wrong. The variables `arr` and `d` are not properties of an object, they are variables, so they cannot be 'deleted' using the `delete` keyword. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete – Alex Mund Jun 25 '15 at 14:31
  • I suggest assigning them to something else. `arr = null; d = null;` – Alex Mund Jun 25 '15 at 14:32
  • @AlexJM Probably fell into this trap - http://perfectionkills.com/understanding-delete/#firebug_confusion. Happy to update the answer and thanks for pointing out! – Russ Cam Jun 26 '15 at 01:35