1

in order to see garbage collection in action. I want to write a script for chrome enviorment that declares a variable and prints it to the console. and then get collected by garbage collector (- so when I try to access it through dev tools I will get a reference error. according to this link : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management , under Real-life example section - if I use internet explore 7 and run the script:

var div;

window.onload = function() {
    div = document.getElementById('myDivElement');
};

which is the example given in the link above but without the circular reference part. if I will try to open ie7 console and try to reach div - I would get a reference error ( like I want ) because div would be garbage collected. how can I do something similar now in chrome?

s0xzwasd
  • 2,895
  • 3
  • 17
  • 26
  • Why waste your time with long-dead and deeply-buried browsers? – Bergi Dec 22 '20 at 10:05
  • @ATP . it dosn't answer my question . as my goal is to write short example of script , in which trying to access a variable after gc would return a reference error . I tried to manually run the gc from the performance tab , on the script that was written in my question but I stiil have reference to div . – יואב שובב Dec 22 '20 at 10:36
  • "*trying to access a variable after gc would return a reference error*" - actually no, that's not how garbage collection works. If the variable is still accessible, it won't get collected. – Bergi Dec 22 '20 at 10:43
  • @bergi . Is it possible to write a sample script code , in html file , that when I open with chrom dev tools and trying to access a variable from the script I would get reference error since the variable was cleaned ? since the script has no further references to the variable ? – יואב שובב Dec 22 '20 at 10:57
  • No, that is not really possible. If the script is trying to access the variable, it won't be collected. (Unless you mean something like [attempting to access variables with the debugger that have been optimised away](https://stackoverflow.com/questions/28388530/why-does-chrome-debugger-think-closed-local-variable-is-undefined)) – Bergi Dec 22 '20 at 11:00

0 Answers0