none
.Net Memory Management - Compacting the Heap! RRS feed

  • Question

  • Hi there,

    I have been faced with a question that I cannot answer, and would like to know if anyone has the answer to.

    A .net application is assigned memory, it is assigned more memory that is needed i.e. on start up of the application.
    When the system starts running out of resources, or when the application is quiet does the runtime compact the heap to free resources.

    What I would like to know is, will the runtime compact the heap when unmanaged applications (C++) need the resources?

    Thanks

    Thursday, August 6, 2009 3:41 PM

Answers

  • The answer is no.  The garbage collector only runs in response to requests for memory from the managed app.  If you don't use new, it won't run.  It is completely unaware of private bytes allocated by unmanaged code.  If you know how memory it needs, you should tell with GC.AddMemoryPressure() so the collector can make better decisions.

    Hans Passant.
    • Marked as answer by eryang Thursday, August 13, 2009 8:55 AM
    Friday, August 7, 2009 12:54 AM
    Moderator

All replies

  • The garbage collector automatically collecting old and unneeded objects from the heap, and it does move them around alot to try to keep the heap as compacted as possible.  So I believe the answer to your question would be yes. 

    As for it recognizing when another application needs a resource, it's not going to adjust in reaction to another application, but rather, it will just adjust when it sees the heap could be compacted.  In other words, it just performs constant maintenance "to be polite" versus being reactionary.
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiTwitterLinkedInForumsBrowser
    Thursday, August 6, 2009 3:46 PM
    Moderator
  • Great, thanks for the fast reply!



    Thursday, August 6, 2009 3:51 PM
  • The answer is no.  The garbage collector only runs in response to requests for memory from the managed app.  If you don't use new, it won't run.  It is completely unaware of private bytes allocated by unmanaged code.  If you know how memory it needs, you should tell with GC.AddMemoryPressure() so the collector can make better decisions.

    Hans Passant.
    • Marked as answer by eryang Thursday, August 13, 2009 8:55 AM
    Friday, August 7, 2009 12:54 AM
    Moderator
  • Hans, wouldn't the assumption be that the app wouldn't be taking up so much memory if "new" is never used?  And when these items go out of scope, would it not move the memory around to create as much of a small contiguous region as it could, thus "compacting" the memory naturally?  This has been my understanding, but I've definately been known to be wrong in the past when it comes to GC stuff in the past.  
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiTwitterLinkedInForumsBrowser
    Friday, August 7, 2009 2:19 AM
    Moderator
  • "Application is quiet" was the OP's question.  If it is quiet, it is not allocating memory and thus the GC won't run.  Similarly, "starts running out of resources" is not a reason for the GC to run.  It runs when the gen 0 heap is full.

    Hans Passant.
    Friday, August 7, 2009 4:53 AM
    Moderator