Core Concepts of Garbage Collection, and How Does it Work in C#

Concepts of Garbage Collection

Introduction

This Blog describes the core concepts of garbage collection.  The garbage collector (GC), part of the common language runtime (CLR), automatically manages memory. The garbage collector controls an application’s memory allocation and release. As a result, developers using managed code don’t need to create code to carry out memory management functions. Automatic memory management helps solve frequent issues like forgetting to release an object, which results in a memory leak or trying to use released for a freed object, and memory is still available.

Enrolling in Dot Net Training in Chennai at FITA Academy will enhance your technical skills in Dot Net, create c# applications and web process forms, and access them using Dot Net.

Benefits of the garbage collector

The garbage collector offers these advantages:

  • Removes the need for manual memory release on the part of developers.
  • Efficiently allows objects to the managed heap.
  • Clears the memory of objects no longer in use to make it available for new allocations. Managed objects’ constructors do not need to initialise each data field because they begin with clean content by default.
  • Ensures memory safety by preventing an item from using memory allocated for another object for its purposes.

Fundamentals of Memory

Important CLR memory ideas are enumerated in the list below:

  • Each process has a distinct virtual address space of its own. On a single computer, every process utilises the same physical memory and, if present, the page file.
  • Each process on 32-bit machines has a 2-GB user-mode virtual address space by default.
  • As an application developer, you never directly access physical memory; instead, you only work with virtual address space. On the managed heap, the garbage collector allocates and releases virtual memory for you.
  • Windows functions are used when writing native code to interact with the virtual address space. These procedures let you allocate and release virtual memory on native heaps.

Register in Dot Net Online Training to learn more about Dot Net Fundamentals. 

  • Three states are available for virtual memory:
      1. Free: The memory block is free for allocation and has no references.
      2. Reserved: You may use the memory block; no further allocation requests may use it. However, until the data is committed, you cannot save it in this memory block.
      3. Committed: The memory block is designated for physical storage.
  • When a virtual address space is fractured, empty blocks exist, often known as address space holes. When a request for virtual memory allocation is made, the virtual memory management must locate a single free block big enough to accommodate the request. Despite having 2 GB of available space, unless all of that space is in a single address block, an allocation that needs 2 GB will fail.
  • You risk running out of memory if there isn’t enough physical or virtual address space to commit. Even when there is little physical memory pressure (demand for physical memory), the page file is still used. The operating system must create space in physical memory to hold data the first time physical memory pressure is high, further backing up some information stored in physical memory to the page file. Since the data is not paged until required, paging may occur even when there is little physical memory demand.

Conclusion

So far, we have described the core concepts of garbage collection. You may rely on garbage collection to automatically carry out the necessary memory management operations for most objects your application creates. Unmanaged resources, however, demand intentional cleansing. The most prevalent unmanaged resource encloses an operating system resource, such as a network connection, window handle, or file handle. The garbage collector needs precise knowledge about clearing the help, even though it can monitor the lifespan of a managed object that contains an unmanaged resource.

Joining Dot Net Training in Coimbatore will help you with a thorough knowledge of programming concepts in the Dot Net framework.