Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to release unmanaged resources in C #

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 "how to release unmanaged resources in C#". In daily operation, it is believed that many people have doubts about how to release unmanaged resources in C#. 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 doubt of "how to release unmanaged resources in C#"! Next, please follow the editor to study!

The .NET platform provides GC (Garbage Collection) in memory management, which is responsible for automatically releasing managed resources and memory recycling, but it cannot release unmanaged resources. In this case, we must provide our own methods to release unmanaged resources allocated within objects. For example, you use a COM object in the object's implementation code.

The easiest way is to release unmanaged resources by implementing protected void Finalize () (which the destructor becomes at compile time), because GC checks to see if the object implements the Finalize () method when it is released, and if so, calls it. However, it is said that this will reduce efficiency.

A better way is to release our objects by implementing an interface that explicitly provides the client with a method to manually release the object, rather than waiting for GC (not to mention the inefficiency).

There is an IDisposable interface in the System namespace, which is very appropriate to do this, so we don't have to declare another interface ourselves.

In addition, this implementation does not have to be used after using unmanaged resources. If the class you design will have larger instances at run time (such as Geometry in GIS), to optimize program performance, you can also implement this interface to let the client caller release these objects manually when confirming that they are not needed.

Example:

Using System

Using System.Collections.Generic

Using System.Text

Namespace Example20 {

Class Program {

Class Class1: IDisposable {

/ / destructor, which is compiled into protected void Finalize ()

GC will call the method ~ Class1 () {before reclaiming the object.

Dispose (false)

}

/ / by implementing this interface, customers can explicitly release objects without waiting for GC to release resources

It is said that that will reduce efficiency void IDisposable.Dispose () {

Dispose (true)

}

/ / Design the release of unmanaged resources as a virtual function that provides the ability to release resources of the base class in an inherited class.

Protected virtual void ReleaseUnmanageResources ({

/ / Do something.

}

/ / Private function is used to release the unmanaged resource private void Dispose (bool disposing) {

ReleaseUnmanageResources ()

/ / when true, it means that the customer explicitly called the release function. You need to tell GC not to call the object's Finalize method.

/ / when false, GC must have called the object's Finalize method

So there is no need to tell GC that you do not call my Finalize method if (disposing {

GC.SuppressFinalize (this);} static void Main (string [] args) {

/ / tmpObj1 does not release resources manually, just wait for GC to release it slowly. Class1 tmpObj1 = new Class1 ()

/ / tmpObj2 calls the Dispose method, which is said to be more efficient than waiting for GC to release it.

/ / personally, I think it's because we have to view its metadata object by object to confirm whether the Dispose method is implemented.

/ / of course, the most important thing is that we can determine the release time ourselves to save memory and optimize the efficiency of the program.

Class1 tmpObj2 = new Class1 () ((IDisposable) tmpObj2). Dispose ()

}

What is the P/Invoke of C# learning experience?

A transaction (transition) occurs when controlled code interacts with uncontrolled code, which usually occurs when the platform invocation service (Platform Invocation Services) is used, that is, P/Invoke

Such as calling the system's API or dealing with COM objects, through the System.Runtime.InteropServices namespace, although it is very convenient to use Interop, it is estimated that each call transaction has to execute 10 to 40 instructions, which costs a lot of money, so we should try to call transactions as little as possible. if necessary, it is recommended to execute multiple actions in one call instead of only a small number of actions each time.

What is the difference between StringBuilder and String of C# learning experience?

Although String is a reference type, a new object will be generated during the assignment operation, but StringBuilder will not, so use StringBuilder instead of String when you concatenate a large number of strings or operate on a string frequently.

Example:

Using System

Using System.Collections.Generic

Using System.Text

Namespace Example22 {

Class Program {

Static void Main (string [] args) {

Const int cycle = 100000

Long vTickCount = Environment.TickCount

String str = null

For (int I = 0; I

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report