Y at-il un moyen de savoir quand un objet sera éliminé par GC?Javascript: savoir quand un objet sera garbaged
Mon objet (appelez-le A) écrit certaines variables dans un objet-tableau global, donc quand l'objet sera garbaged sa propre variable restera dans l'objet-tableau global, prenant de la mémoire.
ps. J'ai de nombreux objets A et je préfère ne pas appeler "manuellement" une méthode pour libérer mon tableau-objet global.
C'est ma situation:
var global_array=[];
function A(x){
global_array.push({who:"A", what:x, id:A.instance++});
this.x=x;
}
A.instance=0;
A.prototype.useIt=function(){
return this.x*2;
}
//will be created an A object and will be garbaged after use by GC
function test(){
var a=new A(10);
var y=a.useIt();
}
test();
//i will never use <a> object again, but global_array hold {who:"A", what:10, id:0)}
NE VOULEZ PAS
A.prototype.dispose=function(){
// free global_array at the correct index
}
Merci.
J'ai modifié ma question pour vous répondre. – blow