1

I've been trying to collect bits and pieces of information about how managed vs unmanaged resources work in .NET to little avail. I haven't been able to find a single article that precisely defines how things work, so I've been piecing together inklings of information from all over the web, and might've just developed a misunderstanding.

I just plain don't understand what managed and unmanaged resources are intuitively. From what I can tell, managed resources are those that are managed by the garbage collector, and thus they have some memory allocated in the GC's heap. When the garbage is collected, it checks when an object has no more references, and removes it from the memory, then performs a "compaction" on the heap so as to remove the spaces of memory where the dead objects were.

Also, I know there's something about Generation 0, 1 and 2, and after each garbage collection the remaining objects in a generation get bumped up to the next generation. So Generation 0 has a small heap and is for very short-lived objects, whereas Generation 1 and 2 have more and more room in the heap for objects that have a greater lifetime and survive more garbage collections. I don't know, however, whether this "bumping up" of objects to the next generation happens before or after the compaction occurs.

I also know the garbage collector runs in a thread simultaneous to the current process thread and has the lowest priority, so it doesn't run that often. But that doesn't give me a good idea of when exactly it does run. Is it random? Is it when so few resources are used that the GC thread can run parallel to the current process thread?

Finally, when it comes to unmanaged resources I just have no clue. The only thing I can draw from them is that they "aren't" managed resources, and thus aren't managed by the GC and thus aren't part of the GC's heap. So, where are unmanaged resources then? Where do they go? From what I can tell it seems like there's another "heap" (though I've only seen the term used in ambiguous contexts) that isn't part of the GC, or something.

I have a very bad internal idea of how memory works in a .NET program, so the difference between managed and unmanaged resources is very fuzzy to me still. Also, I'm a visual learner; I learn by example, and I haven't been able to find too many examples that really aid my understanding of this.

Unless I'm misunderstanding this, all objects in C# created with the new keyword are managed resources. Then how does one even create unmanaged resources? I've read multiple times that file streams have unmanaged resources, but what exactly are those unmanaged resources? How can opening a file not be managed?

So all in all, I have a very bad internal map of what's going on regarding the memory, garbage collection, and disposal of managed and unmanaged resources in C# .NET programs. Can someone clarify this for me, or link me to an article that clearly explains this? I feel like I'm missing some key piece of information that I haven't found yet that will piece everything together. Am I?

EDIT: I know I'm asking a lot but all I'm looking for is for some clarification for the following concrete questions:

  1. What are unmanaged resources, and how do they get "created" at all?
  2. Where do unmanaged resources go if not into the heap of a GC?

Caution to the reader: If you are just reading the questions above, it's pretty obvious my question is a duplicate. But I've scanned many questions and articles top to bottom to present the information I know above, and there's information I'm fundamentally missing that I can't find anywhere. That's what this question is really about, and that's why I believe it's not a duplicate.

user3002473
  • 4,835
  • 8
  • 35
  • 61
  • While I understand your problem to explain what you don't know I don't think it is possible to come up with a new answer so far - duplicate gives couple examples of "unmanaged" ( = not memory allocated by .Net CG-controlled allocator) resources which hopefully clarify topic for you. Additionally I'd recommend reading description of [CreateFile function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx) which essentially introduces a lot of types of unmanaged resources in Windows. – Alexei Levenkov Nov 11 '15 at 06:18

0 Answers0