In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what is the CLR memory management mechanism". In the daily operation, I believe many people have doubts about what the CLR memory management mechanism is. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what is the CLR memory management mechanism?" Next, please follow the editor to study!
Because CLR (common language runtime) can know all object references in the system, while the CLR memory management mechanism runs, GC can get information about whether objects are referenced or not. If an object is no longer referenced, it is automatically recycled through GC.
However, the condition for GC recycling is that it is executed when specific resources are insufficient. If we want to control ourselves, we can also explicitly instruct GC to work. The method is:
System.GC.Collect ()
When recycling, GC will first identify whether the object is referenced and mark the characteristics of the object. Only objects that are not referenced are recycled. To avoid heap fragmentation, GC reallocates memory and relocates objects that are not recycled after they are reclaimed. This will inevitably lead to the decline of system performance when GC is recycled.
Timely manual intervention in memory allocation is a better choice. We know that in C++, for the created class, there is a corresponding destructor to delete memory. In C #, the same approach can be used. When a class object is instantiated and deleted, its destructor is automatically called. CLR provides a mechanism for object termination (object finalization) and introduces the Finalize method. However, in C #, the Finalize method cannot be implemented directly, but the Finalize () method of the base class is called in the destructor.
The recycling mechanism of GC is asynchronous, and we can use the Dispose () method provided by CLR to delete each object. The Dispose () method is provided by the IDiposable interface. Therefore, for the class object to be instantiated, to implement the Dispose operation, you must have the class implement the interface and provide the Dispose () method.
Public class Garbage:IDisposable// implements the interface {public void Dispose () / / provides the Dispose () method; {GC.SuppressFinalize (this); / / reclaims the object;} ~ Garbage () / / destructor; {Dispose ();}}
A better solution, however, is to use the using statement. Put an instance of the object into the using, and once the using is over, the system will automatically know the object.
Using (Garbage g = new Garbage ()) {/ / execute the operation;}
Note, however, that the class of the object that is instantiated in the using statement must also implement the IDisposable interface and the Dispose () method. In addition, because IComponent extends IDisposable, the IComponent type is always IDisposable. So the component type we developed can be used in using, or we can use the Dispose () method. Therefore, instances of components provided by the system, such as DataSet,DataTable, can also be implemented in this way to clear objects.
At this point, the study on "what is the memory management mechanism of CLR" 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.