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.

3
  • 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. Commented Sep 26, 2018 at 8:30
  • does this garbage collector will collect static variables too? if yes then can you provide example Commented Sep 26, 2018 at 8:37
  • If you go to 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. Commented Sep 26, 2018 at 8:53

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

u mean to say that if i write static o4 = null; my static variable o4 will be garbage collected
Yes it will be recognized by the GC.
blog.sessionstack.com/… this will be a nice read for you cause typescript is a superset of js.
is there any way to cross check what you said is right ? i m not douting your answer is just little knowledge for me
Check the link I posted I learned it from there. It's about 30mins read.
|
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.

6 Comments

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
You can read more about this here
i read about it but its about var , object ,but not mentioned about static as static is not based on object
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.
okk actually i want a confirm info on it because my full project works on static only
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.