Which managed classes in .NET Framework allocate (or use) unmanaged memory?

末鹿安然 提交于 2019-12-01 16:51:12

问题


Is there a known (documented) set of .NET types that allocate memory in the unmanaged portion of the process' memory?

For example, Microsoft documents that the WPF infrastructure allocated unmanaged memory for its retained rendering model in order to optimize performance. Are there other such portions of the .NET framework that utilize large amounts of unmanaged memory?


回答1:


If it implements IDisposable there is a very good chance it owns unmanaged data, or it's owning a managed class that ultimately owns unmanaged data. If it has Finalize(), it's sign that it directly owns unmanaged data.

As a rule of thumb, if it implements IDisposable, then Dispose() it as soon as you're done.




回答2:


As far as I know, there is no single document that describes or identifies which classes in the framework use unmanaged resources. The MSDN documentation for the specific class may, but that would require you to look at specific classes.

Overall, it's a safe bet that many of the classes make use of some unmanaged code at some point. For instance, many of the Windows Forms controls are simply wrappers around the Win32 controls so they make use of unmanaged resources.




回答3:


You need to be careful when a class implements IDisposable. This usually indicates usage of unmangaged resources, that is however not limited to memory but might also be filehandles, sockets etc.

A good indicator for this is when the class uses an IntPtr.



来源:https://stackoverflow.com/questions/1515438/which-managed-classes-in-net-framework-allocate-or-use-unmanaged-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!