Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone explain what the point of nulling an object before it goes out of scope is?

I have seen code with the following logic in a few places:

public void func()
{
   _myDictonary["foo"] = null;
   _myDictionary.Remove("foo");
}

What is the point of setting foo to null in the dictionary before removing it?

I thought the garbage collection cares about the number of things pointing to whatever *foo originally was. If that's the case, wouldn't setting myDictonary["foo"] to null simply decrease the count by one? Wouldn't the same thing happen once myDictonary.Remove("foo") is called?

What is the point of _myDictonary["foo"] = null;

edit: To clarify - when I said "remove the count by one" I meant the following:
- myDictonary["foo"] originally points to an object. That means the object has one or more things referencing it.
- Once myDictonary["foo"] is set to null it is no longer referencing said object. This means that object has one less thing referencing it.

like image 283
JSWork Avatar asked Mar 07 '11 17:03

JSWork


People also ask

What happens when a reference goes out of scope?

The reference disappears, the object continues to live.

What does it mean for an object to go out of scope?

The metonymy "going out of scope" is used to express the idea that the dynamic activation/instantiation of the environment associated with some scope is terminating. And so the variables defined in that scope are going away (thus "out of scope").

When a local object goes out of scope during execution?

Destructor is called when the object of a class goes out of the scope in the program. The cases when object goes out of scope, The program goes out of the scope of a function. The program ends.

How do I make an object reference null in Java?

Here Foo bar = new Foo(); you created object of Foo and put reference to the variable bar . Here Foo a = bar; and Foo b = bar; you put the reference to the variables a and b . So you have now one object and three variables pointing to him. Here bar = null; you clear the bar variable.


3 Answers

There is no point at all.

If you look at what the Remove method does (using .NET Reflector), you will find this:

this.entries[i].value = default(TValue);

That line sets the value of the dictionary item to null, as the default value for a reference type is null. So, as the Remove method sets the reference to null, there is no point to do it before calling the method.

like image 90
Guffa Avatar answered Nov 15 '22 22:11

Guffa


Setting a dictionary entry to null does not decrease the ref count, as null is a perfectly suitable value to point to in a dictionary.

The two statements do very different things. Setting the value to null indicates that that is what the value should be for that key, whereas removing that key from the dictionary indicates that it should no longer be there.

like image 21
Daniel DiPaolo Avatar answered Nov 15 '22 21:11

Daniel DiPaolo


There isn't much point to it.

However, if the Remove method causes heap allocations, and if the stored value is large, a garbage collection can happen when you call Remove, and it can also collect the value in the process (potentially freeing up memory). Practically, though, people don't usually worry about small things like this, unless it's been shown to be useful.


Edit:

Forgot to mention: Ideally, the dictionary itself should worry about its own implementation like this, not the caller.

like image 26
user541686 Avatar answered Nov 15 '22 23:11

user541686