CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2011
    Posts
    51

    Help on Garbage Collection

    Hi, Folks,

    I am writing a C# addin, which calls native C++ through CLI/C++. When I keep calling the same sequence of functions many times, the GC.GetTotalMemory keeps growing even if I call GC.Collect periodically. Does anyone have any idea what might be going on?

    Thanks in advance!

    CR

  2. #2
    Join Date
    Feb 2011
    Location
    DeLand, FL
    Posts
    41

    Re: Help on Garbage Collection

    C++ does not have a Garbage collector. If any of the code that you're writing is unmanaged it won't be garbage-collected even though you calll the GC manually. Make sure your C++ code cleans up after itself. If all you're doing is calling C++ code that someone else wrote and this is the case then they need to clean up their code. Personally I think GC encourages sloppy programming, particularly if your code needs to be high-performance. I do use it, but only when I'm certain that object clean-up doesn't have to be done in a timely manner.
    Last edited by Max Peck; April 21st, 2015 at 08:55 AM.
    If you think it's expensive to hire a professional, wait until you try hiring an amateur! - Red Adair

  3. #3
    Join Date
    Apr 2012
    Posts
    43

    Re: Help on Garbage Collection

    I'm going to wager that your C++ calls are allocating some native handles. For example, file handles. The .Net GC doesn't know about these resources. That means you have to clean them up yourself. This is why C# allows you to create finalizers in your classes, so when the GC is cleaning up your object, your code in the finalizer can clean up native resources.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured