Will an object referenced within an object get garbage collected? - javascript

var a = {
b: "this";
};
a = null;
When removing the reference to the object literal that 'a' is referencing to in the beginning, will the reference to "this" be removed as well, or will this cause a memory leak?
Do I have to change to code to this:
delete a.b;
a = null;
?

This will not cause a memory leak. A garbage collector typically works by walking the set of live references, marking the set of objects it finds and collecting anything it didn't see. In this case neither the value initially assigned to a or the literal "this" will be found and both will be eligible for collection

Related

"undefined" or "null" which the Garbage Collector is in favor of

Suppose I create two objects:
var a = new SomeObject();
var b = document.getElementById("someElement");
/* Do something with those two object */
After do something with those two objects I need to clear them in case of memory leak.
The question is which one should I choose btween "null" and "undefined" that the Garbage Collector will prefer.
// firstly, remove DOM node
b.parentElement.remove(b);
// then clear the variants using "null" or "undefined"
a = null;
b = null;
/* or:
a = undefined;
b = undefined;
*/
Any comments will be appreciated!
The garbage collector works by keeping track of which objects are reachable by your code. The idea is that if an object is not reachable, the block of memory it occupies can be freed without breaking your program.
One way in which an object can be reached is if it's assigned to a variable. Assigning a new value to a variable that presently points to an object (e.g. by assigning null to a) will reduce the reference count on that object by one. When this or any object's reference count is reduced to zero, it will qualify for garbage collection.
All that said, it's not really important what value you use to replace the value of a variable, as long as it's not some other object that would otherwise be garbage collected. For this reason, both undefined and null will work.
Note that you only need to scrub variables like this if a variable remains reachable by your code after the function that the variable was declared in returns. This could happen if an anonymous function closes on the variable, if the variable is "global" (tacked onto the global object or declared without the var keyword), or if the variable is actually a field on an object that's still reachable.

Is assigning an object with `null` creates a memory leak?

I am assigning null value over a variable which holds a memory reference for an object. Does that action creates a memory leak?
var x = { a : 10 };
x = null;
So What I believe the above code would do is, after assinging the x with null, the reference that the x is holding would be replaced, but the value which was present in that replaced(older) memory reference will still be there. This sounds like a memory leak.
Can anyone confirm whether my assumption is correct or not? And also explain about how garbage collector in javascript behaves at this situation to flush the memory leak? Relevant links for the expiation would be helpful.
There is no leak. Nothing is referring to {a : 10} any more once you set x to refer to something else.
So it's scheduled for garbage collection and the collector will collect it at leisure.

Where and for how long does my referenceless javascript object exist?

var SomeObj = function() {
this.i = 0;
};
setTimeout(function() {
new SomeObj; // I mean this object
}, 0);
At what point is the SomeObj object garbage collected?
It is eligible for garbage collection as soon as it is no longer used.
That means immediately after the constructor call in your case.
How timely this actually happens is an implementation detail. If you run into GC issues, you need to dig into your specific Javascript engine.
An object that is not referenced from anywhere doesn't "exist" at all from the view of your program. How long it still resides somewhere in memory depends on the garbage collection characteristics of your interpreter, and when/whether it feels the need to collect it.
In your specific case, the object does become eligible for garbage collection right after it has been created and the reference that the expression yields is not used (e.g. in an assignment). In fact, the object might not get created at all in the first place, an optimising compiler could easily remove the whole function altogether - it has no side effects and no return value.

How does the browser's javascript garbage collection work?

Do I have to destroy instances myself? ...if I don't assign them a variable...will they automatically go away?
new ImageUploadView();
vs
var Iu = ImageUploadView();
If there is no reference to an object in javascript, the garbage collector will clean it up.
The way the garbage collector works is it looks for javascript objects for which nobody has a reference to. If nobody has a reference to it, then it can't be used again, so it can be deleted and the memory it occupied reclaimed. On the other hand, if any javascript entity still has a reference to the object, then it is still "in use" and cannot be deleted.
In your first code example:
new ImageUploadView();
unless the constructor of the object stores away the this pointer into some other variable or object or creates some closure that causes references to the object to be held, then there will be no reference to this new object and it will be cleaned up by the garbage collector.
If you second code example:
var Iu = ImageUploadView();
as long as the Iu variable exists and stays in scope it will contain whatever the ImageUploadView() function returns. Note, the second example, is just executing a function and storing it's value. It is not necessarily creating anything. If ImageUploadView() just returns true, then that's all the Iu variable will contain.
The first method is fine. Assuming that the instance of ImageUploadView is appropriately cleaning up after itself, it will be collected by the garbage collector.
With large objects, it's not necessarily a good practice to assume that the browsers built in garbage collector will clean up once it's out of scope. You're better off to clear it ourself using "delete". For example:
delete MyImageUploadView;
Edit: it may be preferable to set the object to null if it isnt being referenced as a property.

Deallocating memory used by Javascript objects

Should I free the allocated memory by myself, or is there a kind of garbage collector?
Is it okay to use the following code in JavaScript?
function fillArray()
{
var c = new Array;
c.push(3);
c.push(2);
return c;
}
var arr = fillArray();
var d = arr.pop()
thanks
Quoted from the Apple JavaScript Coding Guidelines:
Use delete statements. Whenever you
create an object using a new
statement, pair it with a delete
statement. This ensures that all of
the memory associated with the object,
including its property name, is
available for garbage collection. The
delete statement is discussed more in
“Freeing Objects.”
This would suggest that you use a delete command to then allow the garbage collector to free the memory allocated for your Array when you're finished using it. The point that the delete statement only removes a reference is worth noting in that it differs from the behaviour in C/C++, where there is no garbage collection and delete immediately frees up the memory.
Memory management in JavaScript is automatic and there is a garbage collector (GC).
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management
You cannot explicitly delete the variables d and arr, but you can remove references to their value by setting the variables to something else, such as null, to allow the GC to remove them from memory.
arr = null;
d = null;
Note that the delete keyword only deletes object properties.
The variables arr and d will exist as global variables and will exist until they are collected by the Garbage Collector.
The variables will be set as properties on the global object i.e. window in a browser environment but since they are declared with var, they will not be deletable from the global object.
In your particular case, the best course of action might be to assign null to the variables after you are finished with them. You may also want to consider containing their scope to a function and do what you need to do with them inside that function.

Categories

Resources