C# Memory Leak

  • Thread starter Thread starter Sonia
  • Start date Start date
S

Sonia

Hi, I have the question:
If to use static method and allocate new object in it will this new
object be collected by GC?

For Example:

static void SomeMethod()
{
object someObj = new Object();
}

Thanks Sonia
 
Yes, however,if the code looked this this ...

static object someObj;

static void SomeMethod()
{
someObj = new Object();
}

this the GC would not be able to collect the allocated object until someObj was set equal to null. This is because the static someObj reference is always reachable from code.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi, I have the question:
If to use static method and allocate new object in it will this new
object be collected by GC?

For Example:

static void SomeMethod()
{
object someObj = new Object();
}

Thanks Sonia


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.806 / Virus Database: 548 - Release Date: 05/12/2004



[microsoft.public.dotnet.framework]
 
The method is static, the object is not, so, yes, it will be cleaned up by
the GC when it falls out of scope. If the object was static, as well, it
would not.


---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
Richard said:
Yes, however,if the code looked this this ...

this the GC would not be able to collect the allocated object until
someObj was set equal to null. This is because the static someObj
reference is always reachable from code.
Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi, I have the question:
If to use static method and allocate new object in it will this new
object be collected by GC?

For Example:

static void SomeMethod()
{
object someObj = new Object();
}

Thanks Sonia


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.806 / Virus Database: 548 - Release Date: 05/12/2004



[microsoft.public.dotnet.framework]

Thank you Richard for the answer. And if this SomeMethod will be called
in Loop, das it make
the memory leak?

static object someObj;
static void SomeMethod()
{
someObj = new Object();
}

Sonia
 
No, once the object is no longer referenced, it is garbage collected. It
will not leak as in C++.

Tony

Sonia said:
Yes, however,if the code looked this this ...

this the GC would not be able to collect the allocated object until
someObj was set equal to null. This is because the static someObj
reference is always reachable from code.
Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi, I have the question:
If to use static method and allocate new object in it will this new
object be collected by GC?

For Example:

static void SomeMethod()
{
object someObj = new Object();
}

Thanks Sonia


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.806 / Virus Database: 548 - Release Date: 05/12/2004



[microsoft.public.dotnet.framework]

Thank you Richard for the answer. And if this SomeMethod will be called
in Loop, das it make
the memory leak?

static object someObj;
static void SomeMethod()
{
someObj = new Object();
}

Sonia
 
Can you ten me some examples of code where Memory Leak
will be created in c# ?

I have Memory leak problem in my c# project and cannot find it.
I use 2 coms, but i am sure it is not there.

Sonia
 
Are you using the Image class? I've found that you have to call Dispose
on Images when you're done with them or they just sit around taking up
memory.
 
Are you using the Image class? I've found that you have to call Dispose
on Images when you're done with them or they just sit around taking up
memory.

No, I am not. My solution include Server and Windows client Side. The
Leak is on the server (2 added threads - 1 get data from Sensors
Source(by Simulation from FileStreem and save each data block to
Queue.Enqueue(Data),the 2 get it from Queue.Dequeue and process). By
Simulation the file is reading from begin to end,from begin to end,...
by 5 seconds Frequency and data is updated to MS SQl DB table. After 2
hours work Mem Usage of sqlservr.exe and
my server grows Catastrophic (before this it is stable) , I get Windows
Message
"You system is running low on virtual memory..." and so on.

Sonia.
 
Sonia said:
No, I am not. My solution include Server and Windows client Side. The
Leak is on the server (2 added threads - 1 get data from Sensors
Source(by Simulation from FileStreem and save each data block to
Queue.Enqueue(Data),the 2 get it from Queue.Dequeue and process). By
Simulation the file is reading from begin to end,from begin to end,...
by 5 seconds Frequency and data is updated to MS SQl DB table. After
2 hours work Mem Usage of sqlservr.exe and
my server grows Catastrophic (before this it is stable) , I get
Windows Message
"You system is running low on virtual memory..." and so on.

Bruce probably has the solution. If you are using objects that have
resources then you should only keep hold of those resources for as long as
you need them. In general, if the object has a Dispose method (implements
IDisposable) then you should call this method when you know you no longer
need the object (typically I usually assign the reference to null/Nothing
too at this point). If you don't do that the resource will be held until
finalization occurs, and this could be after many hours. In general objects
that implement Dispose also implement Finalize and this last method will
actually make the object live longer!

So check your code for objects that have a Dispose method and call this
method as soon as you can, also, make sure that you create such an object as
late as possible: create late, release early.

Richard
 
Back
Top