Hi all,

I want help from all you wonderful guys. I want to know how can we free memory occupied by an Array. That means all the elements associated with this array should be deleted.

let say:

var bigArray = ["313123", "123123", "sadasd",.........................];

So which will be the best and assured way :

1)

delete bigArray;

OR
2)

bigArray = [];

OR
3) any other ...

Thanks,

Recommended Answers

wow i have created big javascript applications so far and yet i have never needed to delete something from memory. why do you need it?

Jump to Post

Hi everyone,

Lucky,
here's a simple demo, showing how you can removed items inside your array using the pop(index) method.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>www.daniweb.com</title>
<script …
Jump to Post

i dont think he is looking for such a processor consuming way of deleting the memory. i think delete keyword works better for his needs. or basically setting the array to null.

Jump to Post

All 11 Replies

wow i have created big javascript applications so far and yet i have never needed to delete something from memory. why do you need it?

Hi everyone,

Lucky,
here's a simple demo, showing how you can removed items inside your array using the pop(index) method.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>www.daniweb.com</title>
<script type="text/javascript">
<!--

window.onload = function() {
var bigArray = [];

   for ( var i = 0; i < 500; i++ ) { // creating 500 items into bigArray >>> 
      bigArray[ i ] = "Item" + i;
   } alert("bigArray has a total of " + ( bigArray.length ) + " items inside its collection");
   var aLen = ( bigArray.length - 1 ); 
   for ( var x = aLen; bigArray[ x ]; x-- ) { // Removing all 500 items from the bigArray.
      bigArray.pop( x ); 
   } alert( "\nAll items' from the ( bigArray ) has been removed.\nItem count: " + bigArray.length ); 
};
// --> 
</script>
</head>
<body>
<div></div>
</body>
</html>

i dont think he is looking for such a processor consuming way of deleting the memory. i think delete keyword works better for his needs. or basically setting the array to null.

Obtaining serkan's adviced of way of freeing the memory and minimized the above application:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>www.daniweb.com</title>
<script type="text/javascript">
<!--

window.onload = function() {
   var bigArray = [];  
   bigArray2 = [];
   for ( var i = 0; i < 500; i++ ) { // creating 500 items into bigArray >>> 
      bigArray[ i ] = "Item" + i;
      bigArray2[ i ] = "Item" + i;
   } 
/*
  You can use the [b]delete[/b] operator to delete variables declared implicitly but not those declared with the var statement. 

Output Sample: */

delete bigArray; // ignore's the delete process.

alert( bigArray.length ); // outputs the same length

// either one of the following statement will work, if you have defined it using the var statement.

bigArray = null; // or:

// bigArray = []; // reset's all items...

delete bigArray2; // this one works and delete's the bigArray2 variable.

};
// --> 
</script>
</head>
<body>
<div></div>
</body>
</html>

Thanks for taking me into consideration :)

Thanks gentlemen,

So, using 'delete' OR '[]' will delete all the items (and memory) associated with the array.

Please confirm this.

Hi lucky,

both ways will provide the same results, the only difference between the two procedure, is that when you use the delete operator, your variable will no longer be accessible within the function unless you redefined it.

Hope we've claimed your needs...

@serkan - welcome :), you had a better solution compared to my first post.

wow i have created big javascript applications so far and yet i have never needed to delete something from memory. why do you need it?

We usually encounter this problem when working with embedded devices (settop boxes etc.) where there is very less memory. We need to free memory where ever its possible.

Appreciated!
Thanks to all.

Hello, everybody!

I don't like to dig a pretty old thread. However I must tell you and anyone who will view this thread that:

you should use

delete [] somebigArray;

but not

delete somebigArray;

the later doesn't take effect on the clear of array-type variable.


try this:

<html>
<body>

<script type="text/javascript">
var bigArray= [];
for( var i=0;i<500;i++)
{
bigArray[i]="item"+(i+1);
}
document.write("the length of bigArray is "+bigArray.length+"<br>");
document.write("the items of bigArray are: "+bigArray+"<br><br>");

delete bigArray;
document.write("the length of bigArray is "+bigArray.length+"<br>");
document.write("the items of bigArray are: "+bigArray+"<br><br>");

bigArray=[];
document.write("the length of bigArray is "+bigArray.length+"<br>");
document.write("the items of bigArray are: "+bigArray+"<br>");

delete [] bigArray;
document.write("the length of bigArray is "+bigArray.length+"<br>");
document.write("the items of bigArray are: "+bigArray+"<br><br>");




</script>

</body>
</html>

Neither I nor my browser recognizes delete [] bigArray; as a valid line of javascript.

In which environment does it work Fansico?

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.