In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the VB.NET object principle of the example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
After collecting some information on the Internet and now discussing it with you, we all know that the VB.NET object is terminated after the * reference is removed. When no other code references this object, the VB.NET object automatically terminates. The termination event is Class_Terminate, which uses reference counting to determine whether the object is terminated, is a direct product of VB, and is closely related to COM.
So we call the Class_Terminate event when we need to terminate the use of this object, making it easy to control the object. But it also has its shortcomings. Obviously, although it is easy to create circular references between two objects, they will be run in memory forever. This is one of the flaws in VB6 that lead to memory leaks. This memory leak problem was insurmountable in previous versions of VB6. In VB6, circular references occur only on different components. In VB6, classes created by classes in the same component are automatically terminated, even if they have circular references. However, if the objects come from different components, the circular reference problem still exists. This is a big problem, and it brings trouble to many VB developers. Therefore, programs in VB6 have to find various ways to terminate VB.NET objects.
Unlike COM,.NET, reference counting is not used to determine whether an object is terminated. Instead, it uses a well-known "garbage collection" scheme to terminate the object. You may be in a fog when you hear about the "garbage collection" solution, which actually means that in VB.NET we don't have to predefine an object's termination plan, so we can't predict exactly when the object will be terminated. Let's discuss "garbage collection" in detail. "garbage collection"
In .NET, reference counting is not a basic functional part. Instead, the object is terminated through a "garbage collection" mechanism. At a certain time (which determines special rules), a task runs through all objects to find objects that have not been referenced, and terminates those objects, the so-called "garbage collection," the name is a bit corny, but more visual. From what has been discussed above, we can know that we cannot know exactly when the object is terminated. After we remove all references to the object, it does not mean that the object is terminated quickly. At this point, the object still exists in memory, and it is not purged from memory until the garbage collection handler runs.
The main benefit of garbage collection is that it removes the circular application problems caused by reference counting. If two objects have references to each other, and there is no other code in the program that references each other, the garbage collector will find them and terminate them. This is not possible in COM because they will always be in memory. Garbage collection also has another potential performance advantage: it doesn't put a lot of effort into terminating the object when the object is dereferenced; it takes advantage of garbage collection, which occurs when the application is idle, so it reduces the impact on users. However, garbage collection also occurs when the application is running and loading, when the system will be running under lower system resources.
In addition, we can manually trigger the garbage collection handler by writing code:
System.GC.Collect ()
The above process takes some time, but we don't have to perform it every time we want to terminate the object. We *
Finalize method
This garbage collection mechanism provides features that are comparable to Class_Terminate events in VB6. When the object is terminated, the garbage collection code calls the Finalize method, which, like Class_Terminate, can do some memory cleanup.
Protected Overrides Sub Finalize () (some memory cleanup can be done here) End Sub
The above code can use either the Protected scope or overloaded keywords. It is worth pointing out that this method is called before the object is terminated by the garbage collection mechanism, so it is very similar to Class_Terminate.
However, we also need to remember that this method can be called after the object is dereferenced and is implemented through a piece of client code.
Implement the Dispose method
In some cases, the Finalize method is not acceptable. If we have an object, it uses some very limited valuable system resources, such as database connections, file processing, system locking, and so on. At this point, we need to make sure that system resources are released when the object is dereferenced. To do this, we can execute a method that can be called by client code to force the object to be cleared and release system resources. Although this is not a very good solution, it is indeed very effective. Traditionally, this method is named Dispose and its code is as follows:
Public Sub Dispose () (some cleanup can be done here) End Sub
When necessary, we can call this method to ensure that the memory cleanup is carried out. From the above discussion, we can deeply understand some of the changes that inverted VB6 and VB.NET have made in creating classes and objects.
Thank you for reading this article carefully. I hope the article "sample Analysis of the principle of VB.NET objects" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.