Home All Groups Group Topic Archive Search About
Author
8 Dec 2004 11:14 AM
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
Author
8 Dec 2004 12:03 PM
Richard Blewett [DevelopMentor]
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]
Author
8 Dec 2004 4:53 PM
Sonia
Richard Blewett [DevelopMentor] wrote:
> 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.
Show quoteHide quote
>
>  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
Author
8 Dec 2004 4:58 PM
Tony
No, once the object is no longer referenced, it is garbage collected. It
will not leak as in C++.

Tony

Show quoteHide quote
"Sonia" <so***@mer.co.il> wrote in message
news:1102524809.978112.324430@z14g2000cwz.googlegroups.com...
>
> Richard Blewett [DevelopMentor] wrote:
>> 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
>
Author
8 Dec 2004 5:21 PM
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
Author
8 Dec 2004 9:33 PM
brucewood
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.
Author
8 Dec 2004 10:48 PM
Sonia
brucew***@canada.com wrote:
> 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.
Author
20 Dec 2004 9:11 PM
Richard Grimes [MVP]
Sonia wrote:
> 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
--
..NET training, development, consulting and mentoring
www.richardgrimes.com
my email evpun***@zicf.bet is encrypted with ROT13 (www.rot13.org)
Author
8 Dec 2004 4:41 PM
Cowboy (Gregory A. Beamer) - MVP
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!
***************************

Show quoteHide quote
"Sonia" wrote:

> 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
>
>