Check out a free preview of the full Advanced Web Development Quiz course

The "Q9: Garbage Collection" Lesson is part of the full, Advanced Web Development Quiz course featured in this preview video. Here's what you'd learn in this lesson:

Students are instructed to analyze the code and determine when obj1 and obj2 will be garbage collected.

Preview
Close

Transcript from the "Q9: Garbage Collection" Lesson

[00:00:00]
>> What is true about this code block? Obj 1 and obj 2 cannot be garbage collected leading to a memory leak. Obj 1 and obj 2 will be garbage collected immediately after setting them to null. Obj 1 and obj 2 will only be garbage collected after closing the browser tab, or D obj 1 and obj 2 can be garbage collected during the next garbage collection cycle.

[00:00:26]
All right, let's get to the answer. So the right answer is D, obj 1 and obj 2 can be garbage collected during the next garbage collection cycle. So, that shouldn't have been highlighted yet. But anyway, so we have a code block here and we have an add member function.

[00:00:43]
If you can kinda see that, that returns the name and createdAt. Then we create two objects with the addMember and addMember Sarah. So we have those variables kind of in our global execution context now, and they point to the objects in the heap, like the heap is where we store all the dynamic, like objects, functions, anything that can change at runtime time.

[00:01:04]
So then we set object 1's friend property to point to object 2, and object two is friend property to point to object one, right? These are references and job decsripts. They are just basically just references to like addresses and the heap of these objects. Also these objects should have included createAt field, I forgot to have that in my animation, sorry about that but.

[00:01:27]
So the object one should also have a created at that. Yeah, anyway we have that now. And then we set the object one and object two to null. Now when this happens, the object gets removed from the global context, but object one and object two here now still have a reference to each other.

[00:01:41]
Now in the case of modern browsers, they also get deleted because they use something called, or whenever an object is not reachable anymore from the global context, the objects are removed from the heap as well. So in this case, there is no reference anymore from the global context, so they use this mark and sweep algorithm to say, okay, these objects are not reachable anymore, remove them from memory.

[00:02:06]
Older browsers or maybe some still use that, they use the reference counting algorithm, in which they count the references that an object still has, but in this case, you can see that the object still has a reference to another object. So that algorithm would kinda be like, okay, we cannot clear these up because they still reference each other, which leads to a memory leak.

[00:02:26]
But in modern browsers, that's not really an issue anymore because they get removed as soon as there is no reference from the global context to that object. And of course, it doesn't have to be a direct reference, right? It can also be a nested one. So, some objects can reference other objects all the way down.

[00:02:44]
They also won't get garbage collected. But yeah, that's just kind of the big difference between modern garbage collecting and older versions.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now