rockstar - garbage collection Flashcards

1
Q

What is garbage collection in C#?

A
  • Automatic memory management process
  • Identifies and reclaims memory that is no longer used by the program
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain how garbage collection works in C#

A
  • Garbage collector runs in background
  • Identifies objects that are no longer reachable from the application’s root (local variables, static variables) and reclaims their memory
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Garbage collection generations - Gen0

A
  • Newly created objects
  • Designed for short-lived objects that are expected to be collected quickly
  • Objects in Gen0 are collected frequently
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Garbage collection generations - Gen1

A
  • Objects that survive Gen0 collection are promoted to Gen1
  • These objects have longer lifespan than Gen0 but are still relatively short-lived
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Garbage collection generations - Gen2

A
  • Objects that survive multiple Gen1 collections are promoted to Gen2
  • Long-lived objects
  • Collections here are less frequent
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Garbage collection process

A

Marking

Garbage collector identifies which objects can be cleaned and marks the live objects

Sweeping

Garbage collector reclaims the memory occupied by objects that are marked as live

Compacting

Objects that survive Gen0 are promoted to Gen1, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the purpose of the System.GC class in C#?

A
  • Provides methods and properties for interacting with garbage collector
  • Allows developers to request garbage collection, etc.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the difference between managed and unmanaged resources in the context of garbage collection?

A

Managed resources

Objects under control of .NET runtime

Subject to garbage collection

Unmanaged resources

External resources such as database connections

Require explicit cleanup

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can you force garbage collection in C#?

A
  • Calling the System.GC.Collect method
  • Not recommended because it can disrupt the natural behavior of the garbage collector
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are memory leaks, and how can you avoid them in C#?

A
  • Occurs when objects are not properly released
  • Causing memory consumption to grow indefinitely

To avoid:

  • ensure that objects are properly disposed of
  • especially unmanaged resources by using IDisposable or the ‘using’ statement
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Explain the concept of the “root” in garbage collection.

A
  • Set of references that are directly accessible from the program
  • Includes local variables, static variables, and references from active threads
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the impact of garbage collection on application performance?

A
  • Temporary pauses can occur called “garbage collection pauses”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are weak references in C#, and when are they useful?

A
  • They indicate objects that can be garbage collected even if they have references
How well did you know this?
1
Not at all
2
3
4
5
Perfectly