Saturday, 17 November 2018

performance - Managed vs Unmanaged Resources in .NET. What's the difference?




I was reading Wrox's Professional C# 4 and .NET 4 chapter on "Memory Management and Pointers", specifically about how Garbage Collection works in .NET. It said the reason that "the garbage collector does not know how to free unmanaged resources (such as file handles, network connections, and database connections)", which is why such classes should either declare a destructor (aka "finalizer") or implement IDisposable.



It seems like all these examples of "unmanaged resources" are related to interaction with a system that is extrinsic from the application and independent of the .NET Framework. However, I'm not sure if that is the complete distinction that is being made, so,



What exactly is the distinctive characteristic that an unmanaged resource has and a managed resource doesn't have?


Answer



You got it right:
Managed resources are managed by the CLR, unmanaged aren't. In other words: Managed resources live only in the .NET world where as unmanaged resources are from the normal Win32 world.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...