When does GC actally run? Is it like it runs in a certain interval or when the 开发者_运维问答application demand memory or what?
Short answer: When needed.
Longer answer:
- When an allocation cannot be honored
- When an AppDomain is unloaded
- When Windows reports low memory
- When GC.Collect is called
Additionally finalization may run at some point following GC.
from MSFT (see link for more details):
The .NET Framework's garbage collector manages the allocation and release of memory for your application. Each time you create a new object, the common language runtime allocates memory for the object from the managed heap. As long as address space is available in the managed heap, the runtime continues to allocate space for new objects. However, memory is not infinite. Eventually the garbage collector must perform a collection in order to free some memory. The garbage collector's optimizing engine determines the best time to perform a collection, based upon the allocations being made. When the garbage collector performs a collection, it checks for objects in the managed heap that are no longer being used by the application and performs the necessary operations to reclaim their memory.operations to reclaim their memory.
1.The .NET Framework's garbage collector manages the allocation and release of memory for your application.
- Each time you create an object, the runtime allocates memory for the object from the managed heap. 3.The garbage collector must perform a collection in order to free some memory. The garbage collector's optimizing engine determines the best time to perform a collection, based upon the allocations being made. 4.When the garbage collector performs a collection, it checks for objects in the managed heap that are no longer being used by the application and performs the necessary operations to reclaim their memory.
精彩评论