简体   繁体   中英

Some best practice questions about C# Unmanaged object Disposing

I am developing mono and .net applications. I have some basic questions regarding the best practices.

  1. If I need to create an unmanaged object just to be used once (may be pass to a method or invoke one of it's method) and don't need it later, should I assign it to a variable first and then use it so that I can dispose it (or may be assign variable in a using() block) or should I just use like new Class().Method() , in order for GC to be able to collect it? What is the best practice?

  2. Do I need to dispose of objects which has only a local scope of a method or is it enough if I dispose of object that are properties of the class (global)?

  3. I have a Class A and a Class B. An object of B is created in some method M of Class A. This object (of class B) has another method M2 which invokes a second method in Class A, M3. So structure is as below

     Class A { void M() { var b = new B(); } public string M3() { return "OK"; } } Class B { void M2() { Console.WriteLine(new A().M3(); } } 

Will this create a cyclic reference a stop GC from collecting these two objects?

What are the other general principles for good Memory efficient programming?

This question has a significant risk of becoming "too broad", ie off-topic for StackOverflow. That said, some brief answers:

  1. If I need to create an unmanaged object just to be used once (may be pass to a method or invoke one of it's method) and don't need it later… What is the best practice?

Best-practice for using any unmanaged object is to wrap it in an instance of SafeHandle . Of course, most common unmanaged objects you'd have to deal with are already wrapped by .NET, either in some custom implementation of IDisposable (eg StreamReader ), or in newer framework code, as a SafeHandle subclass.

But when you are dealing with your own unmanaged objects that .NET has no knowledge of, you'll want to follow .NET's example and use SafeHandle .

  1. Do I need to dispose of objects which has only a local scope of a method or is it enough if I dispose of object that are properties of the class (global)?

If you create the object, and you have not passed it to some other code for it to own, and you are not going to return it from the method, then you need to dispose it. You do need to dispose objects which are referenced only within a method.

Typically you would use the using statement to do this.

  1. …Will this create a cyclic reference a stop GC from collecting these two objects?

Ignoring for a moment that your code example wouldn't compile, nor contains any reference cycles… :)

Unlike some memory management schemes, such as ref-counting, .NET's garbage collection does not have any issue with cyclical references at all. An object is determined to be collectable or not based on whether it is reachable from a root reference. Two unrooted objects which reference each other are still neither reachable from a root reference and so are both collectable.

What are the other general principles for good Memory efficient programming?

That's definitely too broad for StackOverflow. Even in the GC-based system .NET uses, there a lot of fine details that one can concern oneself with. The basic system eliminates a number of common programming errors, but it has its own idiosyncratic behaviors that are in some cases important to understand.

Some time spent browsing MSDN and web articles on the topic will help you learn more about garbage collection, in .NET and in general. If and when you have other specific questions, please feel free to post them on StackOverflow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM