4

Possible Duplicate:
Setting Objects to Null/Nothing after use in .NET
Do you need to dispose of objects and set them to null?

For large, or high traffic website:

First question:

Will set Object=null (not Disposable ) release memory? Or is other way to release memory?

Second question:

Is explicitly release memory as above necessary in normal code?

Community
  • 1
  • 1
Eric Yin
  • 8,737
  • 19
  • 77
  • 118
  • 6
    That doesn't free memory, it just overwrites a pointer. And this has been asked a thousand times before. –  Jan 25 '12 at 17:59
  • 1
    @delnan: I still think it is a reasonable question becuase in the olden days of COM you HAD to do this to release reference counts. – n8wrl Jan 25 '12 at 18:08

6 Answers6

7

No, it won't do it immediately, and no, it's usually unnecessary if you're writing your code the right way.

If you have a resource that implements IDisposable, call Dispose() on it, or even better, put it in a using(...) block - it's much faster and will release the proper resources. If you have several large objects in the same scope that aren't COM objects or don't implement some form of disposal mechanism, doing:

someObject = null;
GC.Collect();

might help, but you're probably better off restructuring your code so you don't end up in that situation.

If you're doing this before objects go out of scope, it's completely superfluous and makes things worse. More so if you set things to null in your finalizer. For example, never do this:

public void aFunction() {
    SomeThing anObject = new SomeThing();
    // ...
    anObject = null;
}

nor this:

public ~MyClass() {
    this.Something = null; // WRONG!
    this.SomethingElse.Dispose(); // DANGEROUS!
    this.SomeObject.Notify("I got finalized!"); // ALSO DANGEROUS!
}

And, for the sake of completeness, you do this:

Marshal.ReleaseComObject(someObj);

to release a COM object.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • 1
    Further, setting these variables to null at the bottom of a method is considered a *deoptimization.* You're effectively fighting with the JITter and GC. http://stackoverflow.com/a/2742867/47580 – Mike Hofer Jan 25 '12 at 18:30
2

Setting a reference to null won't delete the memory used by the object but it will eventually get garbage-collected.

Suppose you have two objects A and B, with A referencing B.

If there are no longer any references to A, then A and B will both be garbage-collected. There's no point in clearing A's reference to B.

However, if A is a long-lived object and B is no longer required, it can be worth clearing the reference to B so that it will be garbage-collected.

In the context of asp.net, you'll mostly be dealing with short-lived objects so this is typically a non-issue.

arx
  • 16,686
  • 2
  • 44
  • 61
2

Setting an object reference to null won't release memory, however it will allow the garbage collector to release the memory at some later time.

As long as you hold a reference to an object, it won't be garbage collected, but in normal ASP.NET code you'll seldom have to think about it since most objects are only referenced for the duration of the request. When the request is garbage collected, most objects created during it will be automatically garbage collected too.

The major exception to this "soft rule" is static or shared resources where leaving references may cause them to live for many requests and build up over time. In other words, avoid static variables and you'll mostly be fine by default.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
1

No, it isn't. If the object isn't disposeable, the garbage collector will take care of it just fine. Also, you aren't releasing memory, you are modifying a variable.

Femaref
  • 60,705
  • 7
  • 138
  • 176
1

You don't "release" memory in a garbage-collected environment. Start by reading this.

Stu
  • 15,675
  • 4
  • 43
  • 74
1

I havent worked for a very heavy traffic websites or web apps. But i have my business 2 businness portal and Practice Management System running on the web where i am getting around 1000-3000 hits daily with concurrent users. For that i am using static objects throughout the layers. Keeping public static variable in mind. It gives me few benefits ,

  1. I need lesser objects to creates, as a single static object gets shared
  2. Cleaner code for later maintenance.
  3. static ctors calls, as they are called once.

Object cleanup , I left on GC, as GC checks for any orphan objects and automatically free ups its space.

AFAIK, for your questions,

  1. setting null will resets the pointer instead freeing up the memory.
  2. It is not mandatory that you release memory in normal code, but its a good practice to remove all un-necessary objects from the memory so as free up the memory and let your app work appropriately without any issue.
  • Static objects in a web application can be very dangerous because they are shared, and can be accessed by more than one thread at the same time. – John Saunders Jan 25 '12 at 18:21