Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / December 2004

Tip: Looking for answers? Try searching our database.

C# Memory Leak

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sonia - 08 Dec 2004 11:14 GMT
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
Richard Blewett [DevelopMentor] - 08 Dec 2004 12:03 GMT
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




[microsoft.public.dotnet.framework]
Sonia - 08 Dec 2004 16:53 GMT
> 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
>
[quoted text clipped - 21 lines]
>
>  [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
Tony - 08 Dec 2004 16:58 GMT
No, once the object is no longer referenced, it is garbage collected. It
will not leak as in C++.

Tony

>> Yes, however,if the code looked this this ...
>>
[quoted text clipped - 40 lines]
>
> Sonia
Sonia - 08 Dec 2004 17:21 GMT
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
brucewood@canada.com - 08 Dec 2004 21:33 GMT
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.
Sonia - 08 Dec 2004 22:48 GMT
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.
Richard Grimes [MVP] - 20 Dec 2004 21:11 GMT
> 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
[quoted text clipped - 6 lines]
> 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
Signature

.NET training, development, consulting and mentoring
www.richardgrimes.com
my email evpuneqt@zicf.bet is encrypted with ROT13 (www.rot13.org)

Cowboy (Gregory A. Beamer) - MVP - 08 Dec 2004 16:41 GMT
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!
***************************

> Hi, I have the question:
> If to use static method and allocate new object in it will this new
[quoted text clipped - 8 lines]
>
> Thanks Sonia

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.