|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Destroying objects, the IDisposable interface and memory managementHow can I force an objects destructor to execute? I'm writing a WCF server and client. The server makes a dictionary of objects that are operated on at the request of the client. I wrote it to be a dictionary because I saw that the dictionary had a remove method to remove objects from the dictionary. Now I thought that removing the object would make it so that there was no more valid reference to the object and thus garbage collection would come along and dispose of it. This isn't happening though. I remove all elements in my dictionary and the objects desctructors are not called. The one way I know this is that each object makes a temporary file for processing data and should delete this temp file when the destructor is called. However, these temp files hang out until the WCF server reaches the obj.Close() method call. So, how does one force a call to a destructor? Andy On Mon, 15 Jun 2009 14:31:53 -0700 (PDT), Andrew Falanga
<af300***@gmail.com> wrote: Show quoteHide quote >Hi, You can force garbage collection, with:> >How can I force an objects destructor to execute? I'm writing a WCF >server and client. The server makes a dictionary of objects that are >operated on at the request of the client. I wrote it to be a >dictionary because I saw that the dictionary had a remove method to >remove objects from the dictionary. Now I thought that removing the >object would make it so that there was no more valid reference to the >object and thus garbage collection would come along and dispose of >it. This isn't happening though. I remove all elements in my >dictionary and the objects desctructors are not called. The one way I >know this is that each object makes a temporary file for processing >data and should delete this temp file when the destructor is called. >However, these temp files hang out until the WCF server reaches the >obj.Close() method call. > >So, how does one force a call to a destructor? GC.Collect() but that's rarely needed (or a good idea). Normally you'd implement the IDisposable interface, and release your unmanged resources in the Dispose method. You'd then call Dispose(true) on each of your objects after removing them from the dictionary. GSEJ On Mon, 15 Jun 2009 14:31:53 -0700, Andrew Falanga <af300***@gmail.com>
wrote: > How can I force an objects destructor to execute? You can't. At best, you can have the finalizer (the "more correct" term for that special method) call a method that you can call explicitly when you want the same work to be done explicitly. Typically, you'd name that method Dispose(), implement IDisposable, and Bob's your uncle. > I'm writing a WCF The List<T> class also has a Remove() method. So does ArrayList. And any > server and client. The server makes a dictionary of objects that are > operated on at the request of the client. I wrote it to be a > dictionary because I saw that the dictionary had a remove method to > remove objects from the dictionary. number of other collection classes. Dictionary<TKey, TValue> might be the right class to use, or it might not. But the presence of a Remove() method isn't what determines that. > Now I thought that removing the That's right. Assuming the object truly is unreachable, finalization and > object would make it so that there was no more valid reference to the > object and thus garbage collection would come along and dispose of > it. This isn't happening though. [...] collection of the object might happen right away. It might happen later. It might not happen at all. Your code should not depend on garbage collection behavior, including execution of the finalizer. Instead, implement IDisposable in objects that have unmanaged resources in need of cleaning up, and make sure to call the Dispose() method when you are done with the object. Note that part of your issue may be that the dictionary instance isn't the only place the objects are referenced. Your statement implying that the finalizer eventually does get called after some "obj.Close()" method is called (though you don't bother to tell us what "obj" is) suggests that the objects you expected to get finalized and collected are still being referenced somewhere else, until that Close() method is called. Without a concise-but-complete code example that reliably demonstrates the issue, it's impossible to provide specific advice. In the meantime, hopefully the above helps explain the basics. Pete On Jun 15, 4:16 pm, "Peter Duniho" <NpOeStPe***@nnowslpianmk.com> I didn't think so, but wanted to know if there's a way to do it.wrote: > On Mon, 15 Jun 2009 14:31:53 -0700, Andrew Falanga <af300***@gmail.com> > wrote: > > > How can I force an objects destructor to execute? > > You can't. At best, you can have the finalizer (the "more correct" term > for that special method) call a method that you can call explicitly when > you want the same work to be done explicitly. > > The List<T> class also has a Remove() method. So does ArrayList. And any That wasn't the reason. I started with a List<> object but discarded> number of other collection classes. > > Dictionary<TKey, TValue> might be the right class to use, or it might > not. But the presence of a Remove() method isn't what determines that. > it because removing items from the list reordered the list and broke my indexing methods. The Dictionary<> object made more sense. Show quoteHide quote > > Now I thought that removing the I thought that mentioning I was making a WCF service it would be clear> > object would make it so that there was no more valid reference to the > > object and thus garbage collection would come along and dispose of > > it. This isn't happening though. [...] > > That's right. Assuming the object truly is unreachable, finalization and > collection of the object might happen right away. It might happen later. > It might not happen at all. Your code should not depend on garbage > collection behavior, including execution of the finalizer. Instead, > implement IDisposable in objects that have unmanaged resources in need of > cleaning up, and make sure to call the Dispose() method when you are done > with the object. > > Note that part of your issue may be that the dictionary instance isn't the > only place the objects are referenced. Your statement implying that the > finalizer eventually does get called after some "obj.Close()" method is > called (though you don't bother to tell us what "obj" is) suggests that > the objects you expected to get finalized and collected are still being > referenced somewhere else, until that Close() method is called. that the obj.Close() call is the object of type WCFServer. Sorry that it wasn't as clear as I intended. > Basically, what I have is this:> Without a concise-but-complete code example that reliably demonstrates the > issue, it's impossible to provide specific advice. In the meantime, > hopefully the above helps explain the basics. > using System.Collections.Generic; using some.class.library; // this library does image comparisons using both managed and unmanaged code class WCFServer { int index = 0; Dictionary<int, ComparisonClass> graphics; int GetImgReference(string path) { graphics.Add(++index, ComparisonClass(path)); return index; } bool RemoveImgReference(int index) { return graphics.Remove(index); } } class ServerProgram { void public static Main() { WCFServer myServ = new WCFServer(); myServ.Open(); Console.WriteLine("server started, press <enter> to exit"); Console.ReadLine(); // wait WCFServer.Close(); } } Bear in mind that I'm just developing things now. Then, on the WCF client side, the client makes a new class that calls to the server using these functions. So the server starts, opens the sockets and waits for clients to connect. The client calls the MakeImgReference (path) function and obtains an index into the dictionary for the objects stored there. Then, using the RemoveImgReference(index) function, deletes objects from the dictionary. My plan was that by removing the element from the dictionary whatever resources used by that object would be freed and garbage collection would take place. However, this isn't happening. Thanks, Andy
Other interesting topics
WebBrowser - invoking a JS function like it was called from an input element
retrive bios clock date and time File database less than 10-20 MB String.Format D0, D1, D2, D3 change by variable Enumeration and Class Change From HTTP To HTTPS On PostBack Getting last builddate in applcation weird problem with string to double conversion WPF: Binding to a resource via code Linq Query on XML file. Please, what am I doing wrong? |
|||||||||||||||||||||||