Are .NET singletons made less performant as the application runs for longer?

Viewed 66

I have been reading about garbage collection and memory management in .NET. I would like to ask the following to check my understanding:

Suppose I have a singleton that is created at the start of the application and is in generation 0 of the GC. Then imagine I have another class that references this singleton and is also near to it in memory (as it is also in generation 0). Is it reasonable to infer that they could be in the same cache line?

As the application runs, the GC promotes the object through to generation 1, and eventually generation 2. Any new objects created could now be quite far away from the singleton in memory. Am I right in assuming access between the new objects and the singleton could potentially be slower as they cannot be in the same cache line?

Does the GC make any attempt to keep objects that access each other frequently closer together in memory?

I am not looking to attempt to optimise code or second guess the garbage collector, I realise this is a contrived example. I am merely interested in the theory behind how the GC uses memory and how CPU caching works.

1 Answers
Related