3

From TypeScript point of View
I want to know is there any way to destroy static variables,static array in TypeScript
I know we can make it null but want to destroy in order to save memory.
This question may be dumb little but need help because in my project i have a lot use of static variables and static array.

chokiChoki
  • 164
  • 13
  • Your Application's memory management has nothing to do with TypeScript. TypeScript is a design time language and your code will actually transpile to JavaScript. So whatever you're writing will eventually be JavaScript. That being said, JavaScript being a high-level language has it's own Garbage Collector that frees up memory when not used. It uses algorithms like Mark and Sweep to collect up unused memory. So there's nothing that you have to do while developing in JavaScript apart from avoiding any memory leaks. – SiddAjmera Sep 26 '18 at 08:30
  • does this garbage collector will collect static variables too? if yes then can you provide example – chokiChoki Sep 26 '18 at 08:37
  • If you go to https://www.typescriptlang.org/play/, and type a class with a static and a non-static property on it, you'll notice that static members are nothing but properties on the Class and not on class's instance. So I think when the class is not used anywhere, it would be garbage collected and the static properties would be garbage collected with it. – SiddAjmera Sep 26 '18 at 08:53

3 Answers3

0

Usually when you removes references to the object (assuming nobody else is using it). The garbage collector will free up the memory.

There are basically 2 solutions to this problem, Use function scope or else dereference them manually.

Ussaid Iqbal
  • 786
  • 1
  • 7
  • 16
  • 1. can u provide me with an example or function scope and dereference, 2. is garbage collector is there in typescript , actually i have never heard of it – chokiChoki Sep 26 '18 at 08:24
  • You can read more about this [here](http://qnimate.com/javascript-global-variables-and-memory-leakage/) – Ussaid Iqbal Sep 26 '18 at 08:26
  • i read about it but its about var , object ,but not mentioned about static as static is not based on object – chokiChoki Sep 26 '18 at 08:33
  • 1
    Well as per my knowledge when you set a static var to null and its not being used anywhere garbage collector will free up the memory. – Ussaid Iqbal Sep 26 '18 at 08:35
  • okk actually i want a confirm info on it because my full project works on static only – chokiChoki Sep 26 '18 at 08:38
  • As @SiddAjmera explained you don't have to worry about it, the garbage collector will do the job for you! – Ussaid Iqbal Sep 26 '18 at 08:39
0

I will quote this "

o4 = null;`   
// 'o4' has zero references to it. 
// It can be garbage collected."

What this means by make it "null" you make the object array or whatever ready for the GC and the GC will clear it out of your memory.

So by making it null you will clear it out of the memory.

Swoox
  • 3,707
  • 2
  • 21
  • 32
0

Destroy or Set to null?

If you are using the delete keyword, it will remove the property itself.

let obj = {a:1, b:2}
delete obj.a
console.log(obj)
// {b:2}

If you are assigning a null to a property, it will remove reference to the object.
Note: However, it will not free up memory if other references to that object exists.

let obj = {a:1, b:2}
obj.a = null
console.log(obj)
// {a:null, b:2}

To my knowledge, there is no guarantee to trigger the Garbage Collector in browsers. Here's a reference.

Sohail05
  • 11
  • 1
  • 5