Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

destroying java script object

Status
Not open for further replies.

saugor

Programmer
Sep 19, 2002
2
US
I have following simple program to test garbage collection using IE 5.5

<html>
<script>
var data = new Array();
function addElements()
{
if(data == null)
data = new Array();

for (var i=0; i<5000; ++i)
{
var x = data.length;
data[x] = new Object();
data[x].data1 = &quot;hello&quot;;
data[x].data2 = &quot;ashu&quot;;
}
numObj.value = data.length;
}
function removeElements()
{
data = new Array();
// data = null;
numObj.value = data.length;
}
</script>
<body>
<INPUT TYPE=&quot;text&quot; NAME=&quot;numObj&quot;>
<INPUT TYPE=&quot;submit&quot; name=&quot;add&quot; value=&quot;add&quot; onclick=&quot;addElements()&quot;>
<INPUT TYPE=&quot;submit&quot; name=&quot;clear&quot; value=&quot;clear&quot; onclick=&quot;removeElements()&quot;>
</body>
</html>

The problem is when I click on clear button, the memory allocated on clicking on add button dosen't get cleared. The gc seems to be happening only when I click add button again. (keep task manager open --> click add 3-4 times --> click clear button --> memory used by browser dosen't reduce --> click add again --> memory used decreases)

Any idea why this is happening? Can we call gc in internet explorer explicitly? Actually in my application many javascript objects are created when application loads. I want to destroy all the unnecessary objects after it loads.
 
Hi,
interesting problem. I'm not the greatest at programming theory, so I hope I'm not totally off-base, but....

first of all, null tests as a type of object, so you can't remove objects by assigning null to them. e.g.;

<script>
a=null;
alert(typeof(a));
</script>

The delete operator won't really remove an element of an array, it only makes it undefined:

<script>
a=[1,2,3];
delete a[1];
alert(a);
</script>

deleting the entire array causes an &quot;undefined&quot; error in the example below. I guess that implies it doesn't exist.

<script>
a=[1,2,3];
delete a;
alert(a);
</script>

to really delete array elements I think you have to use the &quot;slice()&quot; method, or the netscape specific array methods e.g., &quot;splice()&quot;.

<script>
a=[1,2,3];
alert(a);
a=a.slice(0,2);
alert(a);
</script>

As i understand it, unlike other languages where you have to explicitly remove objects, in JavaScript garbage collection is automatic, and the JS interpreter automatically senses when an object or variable is unreachable, and memory can be freed for other use. Basically, I think you have to overwrite a variable or object to &quot;destroy&quot; the original with a replacement, as in the &quot;slice()&quot; example above. But to make it &quot;disappear&quot; altogether, hmm....
I don't know how you would test for that. If something is &quot;undefined&quot; in certain instances there can still be a slot for it in memory, e.g., in the example above where one of the array element is &quot;deleted&quot;.

Maybe some of the better minds on this forum can shed a little more light on the subject.
sundemon
 
xutopia's got a point, there. The subject is moot in JavaScript.
 
Actually I tried delete, splice, slice.. etc but no effect what so ever. I understand that garbage collection in javascript is automatic so if some data goes out of reference in javascript, it should be garbage collected. Programmer should not bother. Right?

I did some further testing, following were interesting observations:

1. After clicking clear button, if I minimize the IE, the Virtual Memory footprint remains same but memory usage goes down drastically (to 10% of orignal). If I maximize it again, memory increases but looks as if gc has happened. So may be minimizing and maximizing the IE window triggers gc.

2. Same thing happens if I open a new window from original window. On closing the new window, the memory usage of original window decreases (gc is triggered).

So, it looks as if in IE, gc happens only on certain actions and is not a background process like java. It is a problem for me because I want to keep the memory usage down by destroying all the un-used objects from the memory after application loads on the browser.

So anybody has any idea how to resolve this problem? Can we trigger gc explicitly or really destroy the javascript objects from the memory?
 
From what I've read, each browser (and version) handles garbage collection somewhat in its own way. If you want that much control over garbage collection, you need to write your own browser and distribute it. Or go work for Bill Gates and move up until you can influence the people who write the code for IE.

However, you're not really talking about Javascript garbage collection, you want to control the memory management of IE. These are 2 different things.

And this means that you're probably wasting your time trying to force IE to do something in a way it wasn't designed to operate - handle memory efficiently. Upgrade to IE 6 and see if it's been redesigned better in that area.
 
I've seen very few sites that requires large amounts of memory. Unless you have 1000+ JS objects on your page usually the memory requirement is rather small.

I don't see this as being an issue. Gary Haran
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top