|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
C# Memory LeakHi, 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] Richard Blewett [DevelopMentor] wrote:
> Yes, however,if the code looked this this ... someObj was set equal to null. This is because the static someObj> >> > this the GC would not be able to collect the allocated object until reference is always reachable from code. Show quoteHide quote > Thank you Richard for the answer. And if this SomeMethod will be called> 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] in Loop, das it make the memory leak? static object someObj; > Sonia> static void SomeMethod() > { > someObj = new Object(); > } 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 > 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. brucew***@canada.com wrote:
> Are you using the Image class? I've found that you have to call No, I am not. My solution include Server and Windows client Side. TheDispose > on Images when you're done with them or they just sit around taking up > memory. 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 wrote:
> No, I am not. My solution include Server and Windows client Side. The Bruce probably has the solution. If you are using objects that have > 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. 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) 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 > >
BDE
Componts choice: Janus, Infragistics or ComponentOne DotNet 2.0: Is a generic of a subclass a subclass of the generic of its base? Copy DataColumn Between Tables? AppDomain.ExecuteAssembly() Error 1704 .Net Framework Install Currently Suspended Type is not defined: 'SQLDataSetCommand' RollBack replacement for ActiveX in .NET Can I, in code, set the Log Size properties of Custom Event Logs? |
|||||||||||||||||||||||