Dispose or not?
-
hi all, i have a array object in one of my method, i would like to know, should i dispose it when end the coding or not? It will automate throw into garbage colletor? Below is what i am done in my application,
private void DrawConnectedLine(Graphics g)
{
try
{
int[,] drawobjectlist = new int[totaltransporter,n];
:
:
:
drawobjectlist = null;
}
catch(Exception exp)
{
}
}thanks in advance, regards cocoonwls
-
hi all, i have a array object in one of my method, i would like to know, should i dispose it when end the coding or not? It will automate throw into garbage colletor? Below is what i am done in my application,
private void DrawConnectedLine(Graphics g)
{
try
{
int[,] drawobjectlist = new int[totaltransporter,n];
:
:
:
drawobjectlist = null;
}
catch(Exception exp)
{
}
}thanks in advance, regards cocoonwls
When you set an object to null, you're not actually disposing it, your just removing it's memory reference. While it's not a bad practice to set it's value to null once you're done with it, Visual Studio automatically removes the pointer for you when the method ends. It's good to see a developer that's concerned about memory management! Cheers! Richard
My code this week has no errors. But it's Monday morning and I haven't got out of bed.
-
hi all, i have a array object in one of my method, i would like to know, should i dispose it when end the coding or not? It will automate throw into garbage colletor? Below is what i am done in my application,
private void DrawConnectedLine(Graphics g)
{
try
{
int[,] drawobjectlist = new int[totaltransporter,n];
:
:
:
drawobjectlist = null;
}
catch(Exception exp)
{
}
}thanks in advance, regards cocoonwls
Since your array contains integer, no need to dispose it. If you are using any types which has Dispose method, calling it after using is a good practice. Read[^] Scott's article on this subject.
Navaneeth How to use google | Ask smart questions
-
hi all, i have a array object in one of my method, i would like to know, should i dispose it when end the coding or not? It will automate throw into garbage colletor? Below is what i am done in my application,
private void DrawConnectedLine(Graphics g)
{
try
{
int[,] drawobjectlist = new int[totaltransporter,n];
:
:
:
drawobjectlist = null;
}
catch(Exception exp)
{
}
}thanks in advance, regards cocoonwls
cocoonwls wrote:
should i dispose it when end the coding or not? It will automate throw into garbage colletor?
Disposal and garbage collection are two different things. Disposing an object doesn't cause it to be garbage collected. You can't call the Dispose method on an array, as it doesn't have one. An array only uses managed resources, so there is no need for any disposal.
cocoonwls wrote:
drawobjectlist = null;
Setting the reference to null serves no pupose at all in this case. The garbage collector already knows that the array is not used any more, and it can even have collected it before you remove the reference. You are just assigning a null reference to a variable that is not used any longer.
Despite everything, the person most likely to be fooling you next is yourself.
-
hi all, i have a array object in one of my method, i would like to know, should i dispose it when end the coding or not? It will automate throw into garbage colletor? Below is what i am done in my application,
private void DrawConnectedLine(Graphics g)
{
try
{
int[,] drawobjectlist = new int[totaltransporter,n];
:
:
:
drawobjectlist = null;
}
catch(Exception exp)
{
}
}thanks in advance, regards cocoonwls
Just to reiterate what has already been said, an array itself doesn't support disposal but the objects contained in the array may, in which case you want to iterate over the items and call
Dispose
on each item. In this particular case, you have an array ofint
, which doesn't require disposal. Setting the array tonull
in this scenario will have no effect. The JIT compiler (and by extension the GC) is smart enough to know when the object is no longer in use. The thing to keep in mind is that the GC will only collect this array after it is no longer in use and when enough memory pressure exists to trigger a collection cycle. It won't necessarily be collected when the method goes out of scope.Scott Dorman
Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]
Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
-
hi all, i have a array object in one of my method, i would like to know, should i dispose it when end the coding or not? It will automate throw into garbage colletor? Below is what i am done in my application,
private void DrawConnectedLine(Graphics g)
{
try
{
int[,] drawobjectlist = new int[totaltransporter,n];
:
:
:
drawobjectlist = null;
}
catch(Exception exp)
{
}
}thanks in advance, regards cocoonwls