Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
25
rated 0 times [  32] [ 7]  / answers: 1 / hits: 21250  / 12 Years ago, sat, march 24, 2012, 12:00:00

I have written a function for deleting object preiodically.



function add(variable, expireSecond){
this.list[variable] = {expireSecond: expireSecond, addTime: (new Date().getTime()) / 1000};
}

function deleteExpiredObject(){
var currentTime = (new Date().getTime()) / 1000;
for (var key in this.list) {
var item = this.list[key];
if (item.expireSecond + item.addTime < currentTime){
delete this.list[key];
}
}
}


When I use it, I tried to do the following:



add(xxx[1], 300);


But when I called deleteExpiredObject(), it seems that the memory is not free after the object is expired. Is it due to non-zero reference of the object in xxx[1]? How to solve? Is there any library I can use?



Thanks!


More From » node.js

 Answers
66

This is more of a Javascript question than pertaining directly to node.js. Delete in Javascript is used to remove properties from an object. If that property references an object, the object isn't deleted, but if there are no more references to it, then it should be cleaned up on the next garbage collection cycle.



Here are a few more questions related to Javascript and the delete keyword which you may find useful:



`new` without `delete` on same variable in Javascript



When should I use delete vs setting elements to null in JavaScript? (Closed as dupe, but has good answers)



Deleting Objects in JavaScript


[#86634] Thursday, March 22, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
diane

Total Points: 264
Total Questions: 104
Total Answers: 95

Location: Liechtenstein
Member since Wed, Dec 8, 2021
3 Years ago
;