In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is C# managed resources". In daily operation, I believe many people have doubts about what C# managed resources are. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "what is C# managed resources?" Next, please follow the editor to study!
C# managed and unmanaged resources
In C#, resources are divided into managed resources and unmanaged resources. GC can automatically reclaim managed resources (such as managed memory) when reclaiming useless object resources, but unmanaged resources (such as Socket, files, database connections) must be explicitly released in the program.
The recycling of managed resources first requires GC to identify useless objects and then recycle their resources. General useless objects are objects that are not reachable through the current system root object and the call stack object. An important feature of objects leads to the complexity of useless object judgment: mutual reference between objects! If there is no mutual reference, the judgment of useless objects and real-time recycling can be realized through the simple and efficient way of "reference counting". It is due to the existence of mutual references that GC needs to design more complex algorithms, which leads to the problem that it loses the real-time performance of resource recovery and becomes an uncertain way.
For the release of unmanaged resources, C# provides two ways:
1.Finalizer: a destructor written like C++, but in essence it is very different. Finalizer is a Terminator called before the object is reclaimed by GC. The original intention is to release unmanaged resources here, but due to the uncertainty of the running time of GC, it usually leads to untimely release of unmanaged resources. In addition, Finalizer may have unexpected side effects, such as: the recycled object is no longer referenced by other available objects, but it is made available internally by Finalizer, which destroys the atomicity of the GC garbage collection process and increases GC overhead.
2.Dispose Pattern:C# provides the using keyword to support Dispose Pattern to release resources. This releases unmanaged resources in a determined way, and the using structure provides exception security. Therefore, it is generally recommended to use Dispose Pattern, supplemented by checking in Finalizer, and release resources in Finalizer if you forget to explicitly Dispose objects.
It can be said that GC not only brings security and convenience to the program, but also pays a large price: first, it loses the real-time performance of managed resource recovery, which is fatal in real-time systems and resource-constrained systems; second, it does not unify the management of C# managed resources and unmanaged resources, resulting in conceptual separation. One of the positioning of C++ is the underlying development capabilities, so it is not difficult to understand that GC has not become a language feature of C++. Although we can see GC in Central0x and various third-party libraries, GC is not that important to C++ and is a useful complement at best. C++ is proud of C, and what competes with C # GC is its RAII.
At this point, the study of "what is C# managed resources" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.