finalizer

GC.Collect() not collecting immediately?

ぐ巨炮叔叔 提交于 2019-11-26 18:36:57
问题 In the course of a discussion in chat, I wrote this console application. Code: using System; class Program { static void Main(string[] args) { CreateClass(); Console.Write("Collecting... "); GC.Collect(); Console.WriteLine("Done"); } static void CreateClass() { SomeClass c = new SomeClass(); } } class SomeClass { ~SomeClass() { throw new Exception(); } } Result: Collecting... Done Unhandled Exception: System.Exception: Exception of type 'System.Exception' was thrown. at SomeClass.Finalize() I

Is it safe to call an RCW from a finalizer?

浪尽此生 提交于 2019-11-26 18:14:49
问题 I have a managed object that calls a COM server to allocate some memory. The managed object must call the COM server again to free that memory before the managed object goes away to avoid a memory leak. This object implements IDisposable to help ensure that the correct memory-releasing COM call is made. In the event that the Dispose method is not called, I would like the object's finalizer to free the memory. The trouble is, the rules of finalization is that you must not access any reference

Finalizer and IDisposable

梦想与她 提交于 2019-11-26 16:56:37
问题 Based on the documentation (MSDN: link), it is clear that one should use the IDisposable pattern when implementing a finalizer. But do you need to implement a finalizer if you implement IDisposable (so as to provide a deterministic way of disposing the object), and you dont have any unmanaged resources to clean up? As I see it, if the class has only managed resources and if you dont call Dispose, the managed resources will automatically get cleaned up by the GC and hence no need to implement

Why do finalizers have a “severe performance penalty”?

空扰寡人 提交于 2019-11-26 16:23:30
问题 Effective Java says : There is a severe performance penalty for using finalizers. Why is it slower to destroy an object using the finalizers? 回答1: Because of the way the garbage collector works. For performance, most Java GCs use a copying collector, where short-lived objects are allocated into an "eden" block of memory, and when the it's time for that generation of objects to be collected, the GC just needs to copy the objects that are still "alive" to a more permanent storage space, and

Xamarin Android Finalizer not getting called when leaving the activity to go to another Activity

故事扮演 提交于 2019-11-26 16:02:25
问题 The Finalizer is never called after leaving the activity. Does that mean the activity is still alive even though I moved on to the next activity. namespace XamarinTest { [Activity(Label = "XamarinTest", Icon = "@drawable/icon")] public class MainActivity : Activity { private int count = 1; private TextView density; protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView(Resource.Layout.ScreenData); density =

Why should we call SuppressFinalize when we don't have a destructor

妖精的绣舞 提交于 2019-11-26 15:54:05
问题 I have few Question for which I am not able to get a proper answer . 1) Why should we call SuppressFinalize in the Dispose function when we don't have a destructor . 2) Dispose and finalize are used for freeing resources before the object is garbage collected. Whether it is managed or unmanaged resource we need to free it , then why we need a condition inside the dispose function , saying pass 'true' when we call this overridden function from IDisposable:Dispose and pass false when called

Should “Dispose” only be used for types containing unmanaged resources?

五迷三道 提交于 2019-11-26 15:52:01
问题 I was having a discussion with a colleague recently about the value of Dispose and types that implement IDisposable . I think there is value in implementing IDisposable for types that should clean up as soon as possible, even if there are no unmanaged resources to clean up . My colleague thinks differently; implementing IDisposable if you don't have any unmanaged resources isn't necessary as your type will eventually be garbage collected. My argument was that if you had an ADO.NET connection

Is the destructor called if the constructor throws an exception?

本秂侑毒 提交于 2019-11-26 14:03:52
问题 Looking for an answer for C# and C++. (in C#, replace 'destructor' with 'finalizer') 回答1: Preamble: Herb Sutter has a great article on the subject: http://herbsutter.wordpress.com/2008/07/25/constructor-exceptions-in-c-c-and-java/ C++ : Yes and No While an object destructor won't be called if its constructor throws (the object "never existed"), the destructors of its internal objects could be called. As a summary, every internal parts of the object (i.e. member objects) will have their

Are .net finalizers always executed?

风流意气都作罢 提交于 2019-11-26 11:10:02
问题 Are finalizers guaranteed to be executed in .NET at some point (spare power outages and the like)? I know how GC works and that it is nondeterministic when exactly they\'ll run. (The search did not display good answers, so I\'m adding this question in high expectation of a merge with the not-so-easy-to-discover actual answers. Apart from that, I already know the answer and am going to add it after a few days in case nobody mentioned it.) 回答1: Finalizers may actually be never executed, as

Use of Finalize/Dispose method in C#

倖福魔咒の 提交于 2019-11-25 23:58:04
问题 C# 2008 I have been working on this for a while now, and I am still confused about the use of finalize and dispose methods in code. My questions are below: I know that we only need a finalizer while disposing unmanaged resources. However, if there are managed resources that make calls to unmanaged resources, would it still need to implement a finalizer? However, if I develop a class that doesn\'t use any unmanaged resource - directly or indirectly, should I implement the IDisposable to allow