Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .openpublishing.redirection.json
Original file line number Diff line number Diff line change
Expand Up @@ -3657,7 +3657,7 @@
},
{
"source_path": "docs/standard/garbagecollection/gc.md",
"redirect_url": "/dotnet/standard/garbage-collection/gc"
"redirect_url": "/dotnet/standard/garbage-collection/fundamentals"
},
{
"source_path": "docs/standard/garbagecollection/implementing-dispose.md",
Expand Down Expand Up @@ -3687,6 +3687,10 @@
"source_path": "docs/standard/garbagecollection/weak-references.md",
"redirect_url": "/dotnet/standard/garbage-collection/weak-references"
},
{
"source_path": "docs/standard/garbage-collection/gc.md",
"redirect_url": "/dotnet/standard/garbage-collection/fundamentals"
},
{
"source_path": "docs/standard/garbage-collection/memory-management-and-gc.md",
"redirect_url": "/dotnet/standard/garbage-collection/index"
Expand Down
4 changes: 2 additions & 2 deletions docs/core/run-time-config/garbage-collector.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Settings are arranged into groups on this page. The settings within each group a

## Flavors of garbage collection

The two main flavors of garbage collection are workstation GC and server GC. For more information about differences between the two, see [Fundamentals of garbage collection](../../standard/garbage-collection/fundamentals.md#workstation-and-server-garbage-collection).
The two main flavors of garbage collection are workstation GC and server GC. For more information about differences between the two, see [Workstation and server garbage collection](../../standard/garbage-collection/workstation-server-gc.md).

The subflavors of garbage collection are background and non-concurrent.

Expand Down Expand Up @@ -66,7 +66,7 @@ Project file:

- Configures whether background (concurrent) garbage collection is enabled.
- Default: Enabled (`true`).
- For more information, see [Background garbage collection](../../standard/garbage-collection/fundamentals.md#background-workstation-garbage-collection) and [Background server garbage collection](../../standard/garbage-collection/fundamentals.md#background-server-garbage-collection).
- For more information, see [Background garbage collection](../../standard/garbage-collection/background-gc.md).

| | Setting name | Values | Version introduced |
| - | - | - | - |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ MyClass mc = new MyClass();
MyClass mc2 = mc;
```

When the object is created, enough memory is allocated on the managed heap for that specific object, and the variable holds only a reference to the location of said object. Types on the managed heap require overhead both when they are allocated and when they are reclaimed by the automatic memory management functionality of the CLR, which is known as *garbage collection*. However, garbage collection is also highly optimized and in most scenarios, it does not create a performance issue. For more information about garbage collection, see [Automatic memory management and garbage collection](../../../standard/garbage-collection/gc.md).
When the object is created, enough memory is allocated on the managed heap for that specific object, and the variable holds only a reference to the location of said object. Types on the managed heap require overhead both when they are allocated and when they are reclaimed by the automatic memory management functionality of the CLR, which is known as *garbage collection*. However, garbage collection is also highly optimized and in most scenarios, it does not create a performance issue. For more information about garbage collection, see [Automatic memory management and garbage collection](../../../standard/garbage-collection/fundamentals.md).

## Declaring Classes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ The following configuration file disables background garbage collection:

If there's a **gcConcurrentSetting** setting in the machine configuration file, it defines the default value for all .NET Framework applications. The machine configuration file setting overrides the application configuration file setting.

For more information on concurrent and background garbage collection, see the [Concurrent garbage collection](../../../../standard/garbage-collection/fundamentals.md#concurrent-garbage-collection), [Background workstation garbage collection](../../../../standard/garbage-collection/fundamentals.md#background-workstation-garbage-collection), and [Background server garbage collection](../../../../standard/garbage-collection/fundamentals.md#background-server-garbage-collection) sections in the [Fundamentals of Garbage Collection](../../../../standard/garbage-collection/fundamentals.md) article.
For more information on concurrent and background garbage collection, see [Background garbage collection](../../../../standard/garbage-collection/background-gc.md).

## Example

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,5 @@ The following example shows how to enable garbage collection for multiple CPU gr

- [Runtime Settings Schema](index.md)
- [Configuration File Schema](../index.md)
- [To disable concurrent garbage collection](gcconcurrent-element.md#to-disable-background-garbage-collection)
- [Workstation and server garbage collection](../../../../standard/garbage-collection/fundamentals.md#workstation-and-server-garbage-collection)
- [Disable concurrent garbage collection](gcconcurrent-element.md#to-disable-background-garbage-collection)
- [Workstation and server garbage collection](../../../../standard/garbage-collection/workstation-server-gc.md)
65 changes: 65 additions & 0 deletions docs/standard/garbage-collection/background-gc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
title: Background garbage collection
description: Learn about background garbage collection in .NET and how it differs in workstation and server garbage collection.
ms.date: 04/21/2020
helpviewer_keywords:
- garbage collection, background
- background garbage collection
---
# Background garbage collection

In background garbage collection (GC), ephemeral generations (0 and 1) are collected as needed while the collection of generation 2 is in progress. Background garbage collection is performed on one or more dedicated threads, depending on whether it's background or server GC, and applies only to generation 2 collections.

Background garbage collection is enabled by default. It can be enabled or disabled with the [gcConcurrent](../../../docs/framework/configure-apps/file-schema/runtime/gcconcurrent-element.md) configuration setting in .NET Framework apps or the [System.GC.Concurrent](../../core/run-time-config/garbage-collector.md#systemgcconcurrentcomplus_gcconcurrent) setting in .NET Core apps.

> [!NOTE]
> Background garbage collection replaces [concurrent garbage collection](#concurrent-garbage-collection) and is available in .NET Framework 4 and later versions. In .NET Framework 4, it's supported only for *workstation* garbage collection. Starting with .NET Framework 4.5, background garbage collection is available for both *workstation* and *server* garbage collection.

A collection on ephemeral generations during background garbage collection is known as *foreground* garbage collection. When foreground garbage collections occur, all managed threads are suspended.

When background garbage collection is in progress and you've allocated enough objects in generation 0, the CLR performs a generation 0 or generation 1 foreground garbage collection. The dedicated background garbage collection thread checks at frequent safe points to determine whether there is a request for foreground garbage collection. If there is, the background collection suspends itself so that foreground garbage collection can occur. After the foreground garbage collection is completed, the dedicated background garbage collection threads and user threads resume.

Background garbage collection removes allocation restrictions imposed by concurrent garbage collection, because ephemeral garbage collections can occur during background garbage collection. Background garbage collection can remove dead objects in ephemeral generations. It can also expand the heap if needed during a generation 1 garbage collection.

## Background workstation vs. server GC

Starting with .NET Framework 4.5, background garbage collection is available for server GC. Background GC is the default mode for server garbage collection.

Background server garbage collection functions similarly to background workstation garbage collection, but there are a few differences:

- Background workstation garbage collection uses one dedicated background garbage collection thread, whereas background server garbage collection uses multiple threads. Typically, there's a dedicated thread for each logical processor.

- Unlike the workstation background garbage collection thread, the background server GC threads do not time out.

The following illustration shows background *workstation* garbage collection performed on a separate, dedicated thread:

![Background workstation garbage collection](./media/fundamentals/background-workstation-garbage-collection.png)

The following illustration shows background *server* garbage collection performed on separate, dedicated threads:

![Background server garbage collection](./media/fundamentals/background-server-garbage-collection.png)

## Concurrent garbage collection

> [!TIP]
> This section applies to:
>
> - .NET Framework 3.5 and earlier for workstation garbage collection
> - .NET Framework 4 and earlier for server garbage collection
>
> Concurrent garbage is replaced by background garbage collection in later versions.

In workstation or server garbage collection, you can [enable concurrent garbage collection](../../../docs/framework/configure-apps/file-schema/runtime/gcconcurrent-element.md), which enables threads to run concurrently with a dedicated thread that performs the garbage collection for most of the duration of the collection. This option affects only garbage collections in generation 2; generations 0 and 1 are always non-concurrent because they finish fast.

Concurrent garbage collection enables interactive applications to be more responsive by minimizing pauses for a collection. Managed threads can continue to run most of the time while the concurrent garbage collection thread is running. This results in shorter pauses while a garbage collection is occurring.

Concurrent garbage collection is performed on a dedicated thread. By default, the CLR runs workstation garbage collection with concurrent garbage collection enabled on both single-processor and multi-processor computers.

The following illustration shows concurrent garbage collection performed on a separate dedicated thread.

![Concurrent Garbage Collection Threads](./media/gc-concurrent.png)

## See also

- [Workstation and server garbage collection](workstation-server-gc.md)
- [Run-time configuration options for garbage collection](../../core/run-time-config/garbage-collector.md)
Loading