none
Help with manual garbage collection RRS feed

  • Question

  • If I'm done using an object that I had instantiated in a method and i set it to null, will the garbage collector for sure clean that memory up if I call GC.Collect() right after I set the object to null?

    Thursday, September 11, 2008 5:15 AM

Answers

  • I just got that recommendation last night and tried it out. Memory usage is now MUCH better but it's still climbing steadily.

    I'm using GC.GetTotalyMemory(false) and writing the memory values to a text file that I can plug into an Excel spreadsheet... I let the app run for about 15 hours or so. Memory usage started at about 1,700KB and when I looked at it 15 hrs later, it was at 50,000KB. Better than 2,000,000 like it was before.

    This app will run on a server that is always on so I need memory usage to be consistent at the very least.
    • Marked as answer by Zhi-Xin Ye Wednesday, September 17, 2008 10:22 AM
    Monday, September 15, 2008 11:04 PM

All replies

  • If you don't hold any other references to the collection somewhere else.

    Mattias, C# MVP
    Thursday, September 11, 2008 7:34 AM
    Moderator
  • Setting a local variable to null is not necessary, the garbage collector is smart enough to know when the object is no longer referenced by that variable.  Beware that you can't test this in a Debug build, the JIT compiler extends the lifetime of a stack variable to the end of the method to make debugging easy. 

    Some sample code to try, run in both in the Debug and Release builds:

    using System;

    namespace ConsoleApplication1 {
      class Program {
        static void Main(string[] args) {
          TestCollect();
          Console.ReadLine();
        }
        static void TestCollect() {
          Test t = new Test();
          GC.Collect();
          GC.WaitForPendingFinalizers();
          Console.WriteLine("Collected = {0}", Test.SeenGC);
        }
      }
      class Test {
        public static bool SeenGC;
        ~Test() { SeenGC = true; }
      }
    }


    Hans Passant.
    Thursday, September 11, 2008 9:19 AM
    Moderator
  • OK - So how can I ensure that an object gets deleted? And, can I control WHEN an object gets destroyed... I'm using C#. I wish I could create a pointer to a managed object so I could delete it when I want.
    Friday, September 12, 2008 7:07 PM
  • remarkpk said:

    OK - So how can I ensure that an object gets deleted? And, can I control WHEN an object gets destroyed... I'm using C#. I wish I could create a pointer to a managed object so I could delete it when I want.



    If your object holds native resources you could implement the IDisposable pattern.

    (If your object doesn't hold native resources, well, you shouldn't care.)

    Curt - http://www.codeneverwritten.com/
    Friday, September 12, 2008 9:34 PM
  • Well, in particular, I'm using an IHTMLDocument2 object and calling its "write()" method to write some HTML to the document. When I do this, my memory increases until my application throws an OutOfMemoryException.

    So, i assume that the memory used when writing HTML to the object does not get freed. I also know that the original IHTMLDocument2 interface is implemented in unmanaged C++. So, how can I free the memory used by this object and/or method call??
    Friday, September 12, 2008 10:11 PM
  • You are using an unmanaged COM interface.  It's got its own ideas about releasing memory.  Browser leak memory, they more you use the DOM, the worse the leaks.  It's not a CLR problem.
    Hans Passant.
    Saturday, September 13, 2008 12:10 AM
    Moderator
  • Right - So if I can use those interfaces in a managed application, there must be some way that I can free the resources consumed by them, right???

    How would you do it in COM? CoUninitialize()?

    Is there ANY other way to iterate through HTML nodes like you would do with XmlDocument and XmlNode/XmlNodeList??
    Saturday, September 13, 2008 2:49 AM
  • Have you looked into Marshal.ReleaseComObject? If you're holding references to COM objects then yes, you should release them. If that's not the problem and it's leaks in the DOM implementation then it's likely you can't do anything about it.
    Curt - http://www.codeneverwritten.com/
    Monday, September 15, 2008 9:10 PM
  • I just got that recommendation last night and tried it out. Memory usage is now MUCH better but it's still climbing steadily.

    I'm using GC.GetTotalyMemory(false) and writing the memory values to a text file that I can plug into an Excel spreadsheet... I let the app run for about 15 hours or so. Memory usage started at about 1,700KB and when I looked at it 15 hrs later, it was at 50,000KB. Better than 2,000,000 like it was before.

    This app will run on a server that is always on so I need memory usage to be consistent at the very least.
    • Marked as answer by Zhi-Xin Ye Wednesday, September 17, 2008 10:22 AM
    Monday, September 15, 2008 11:04 PM