In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you VB6 and VB.NET in the creation of classes and VB.NET objects in what changes, I believe that most people do not know much, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!
Termination of VB.NET object
In VB6, the object is terminated after the * reference is removed. In other words, when no other code references the object, the object automatically terminates. The specific trigger for this termination event is Class_Terminate. This method 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, in VB6, programs have to find various ways to terminate the object.
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 out which VB.NET objects are no longer referenced, and terminates those objects, known as "garbage collection," with a somewhat corny but more visual name.
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 VB.NET object when it is dereferenced; it takes advantage of garbage collection, which occurs when the application is idle, so it mitigates 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 VB.NET 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 a VB.NET 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.
These are all the contents of the article "what are the changes in VB6 and VB.NET in creating classes and VB.NET objects?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.