Is it necessary to nullify primitive values for grabage collection?
Asked Answered
M

3

6

If I have the following code:

function MyClass() {
    this.data = {
        // lots of data
    };
}

var myClassInstace = new MyClass();

var myobj = {
    num:123,
    str:"hello",
    theClass:myClassInstance
};

I know it's absolutely necessary to do:

myobj.theClass = null;

To free up myClassInstance and its data property for GC. However, what should I do with myobj.num and myobj.str? Do I have to give them a value of null too? Does the fact that they're primitive change anything regarding GC?

Middlebuster answered 6/11, 2016 at 17:29 Comment(3)
Do you want to preserve myobj but ensure all of its values do not occupy memory or do you want to totally wipe myobjt from memory?Carlynne
"I know it's absolutely necessary to do: myobj.theClass = null;..." No, it's not.Cassady
Yes, I want to totally wipe myobj. And if I want to do that, I'm pretty sure it's necessary to do myobj.theClass = null if I don't want to cause memory leaks.Middlebuster
C
5

The JavaScript runtime that implements garbage collection will be able to collect items as soon as values are no longer reachable from code. This is true for object references as well as primitives. The details of the exact moment the item is collected varies by implementation, but it is not even necessary to set your object references to null (as you state) unless you need the object cleaned up sooner than the natural termination of the current function.

This all ties into the fundamental concept of "scope" and the Scope Chain. When an item is no longer in any other objects scope chain it can be collected. Understanding this clearly will answer this question and also help to understand closures, which are scenarios where items stay in memory longer than you might have expected.

Cassady answered 6/11, 2016 at 17:45 Comment(6)
Saying that Javascript uses reference counting is incorrect. First, JavaScript is a language, not an implementation. Different implementations may use different types of garbage collection. Second, V8 at least does not use reference counting, it uses a generational collector with a mark and sweep algorithm. jayconrod.com/posts/55/a-tour-of-v8-garbage-collectionCaricature
@JustinBlank Updated my answer.Cassady
Let's suppose my main object myobj is a game and the MyClass is a bullet, for example. myobj (the game object) will be living until the tab is closed. If I have many MyClass (bullet) instances that are referenced by it, they need to be gone ASAP to make room for other stuff. I'm just wondering whether setting any primitives (of the game object) to null will help that whole process or not.Middlebuster
@HristiyanDodov Rather than managing the garbage collection process by setting your variables to null, control it by controlling the scope of your variables. If you create bullet instances, you have to be doing it from a lexical scope somewhere, when your bullet variable goes out of scope, so will your bullets. In other words, strive to structure the bullet objects as short-lived objects by giving them smaller scopes. As I said, primitives work just like objects do from a scope perspective.Cassady
Wait, the bullets themselves will go out of scope when their class definition (in this case, MyClass) goes out of scope, or the variable holding the individual instances of MyClass (which are basically the bullets)? I know the latter is true, but the former?Middlebuster
@HristiyanDodov Let's be clear that although we now have a class keyword, JavaScript doesn't actually have classes - only objects. Classes can't go out of scope, only object instances can. If an object has a property that references another object (in your case the game and the bullets) and the fist object goes out of scope then there will be no more code paths to the object referenced by the first object's property, so the secondary object will go out of scope as well.Cassady
C
1

There are a lot of "it depends here", ranging from what your code is doing to what browser you're running in. However, if your object is JIT compiled to not use a map for its attributes, then the number should be an 8 byte double stored inline inside the object. Nulling it will do nothing.

The string and the myclass instance will be a pointer to memory allocated outside the object (since a string can be arbitarily many bytes, it can't be stored inside the object. A compiler could conceivably store one instance of the string in memory and never free it, however). Nulling them can allow the garbage collector to free them before the main object goes out of scope.

However, the real question is why you're worried about this. Unless you have profiled your code and identified garbage collection or memory leaks as a problem, you should not be trying to optimize GC behavior. In particular, unless your myobj object is itself going to be live for a long time, you should not worry about nulling fields. The GC will collect it when it goes out of scope.

Caricature answered 6/11, 2016 at 17:44 Comment(0)
R
0

setting to undefined (not null) will work however delete is better example delete myobj.theClass

Just to avoid misunderstanding I will say that there is no way to really delete an object from memory in JavaScript. you delete it's references or set them to undefined so that the GC can do it's work and really delete.

Rase answered 6/11, 2016 at 17:32 Comment(1)
@Karpak U did not understand the question and not the answer,Rase

© 2022 - 2024 — McMap. All rights reserved.