Title: CSharp / MemoryManagement

Q: What are the problems with a Mark and Sweep Garbage Collector?::
A: - A mark and sweep GC has to mark all live objects on the heap during each collection cycle.
- This can lead to long program freezes while the collection is running
- Also: repeatedly marking the same objects over and over in each cycle is very inefficient.

Q: How does a Generational Garbage Collector help reduce the problems of a Mark and Sweep Garbage Collector?::
A: - Generations help to reduce the number of objects in gen 0.
- Gen 1 and Gen 2 are collected much less frequently.
- The GC assumes that any object that reaches gen2 must be a long living object that does not need to be checked very often.

Q: What are the Implicit assumptions that the garbage collector operates on?
A: - Objects are either short-lived or long-lived.
- Short-lived objects will be allocated and discarded within a single collection cycle.
- Objects that survive two collection cycles are long-lived.
- 90% of all small objects are short-lived.
- All large objects (85K+) are long-lived.

Q: Which objects remain in the Gen0 list after every garbage collection cycle?
A: Gen 0 is completely cleared i.e. empty.

Q: When optimizing code for GC, what are the two considerations you must look for with regards to object size and lifetime?
A: - Avoid large short-lived objects
- Avoid small long-lived objects

Q: If you have a large short-lived object, what is the one optimization you can try?
A: If you have a large object like say a db connection and for some reason it is short-lived then one optimization that you can use to convert it to a long-lived object is to to pool it in an object pool and reuse it the next time.

Q: Should a finalizer process other referenced objects?
A: A finalizer must only process its own object and never other referenced objects.

Q: You have some referenced objects in your class, will you process them in a finalizer?
A: No. A finalizer must only process its own object and never other referenced objects.