finalizer

Resurrection difference in using Object Initializer

为君一笑 提交于 2019-12-17 17:04:16
问题 I have this code: Essentially i'm trying to demonstrate the use of the c# finalizer and make an object that cannot die, I called it Zombie. Now, normally this demo works great, but today I tried using the same code with the object initializer instead of just assigning to the property (Name in this case). I noticed there is a difference. Namely that the finalizer never gets called, not even when I'm trying my best to make the Garbage Collector do it's work. Could someone explain the difference

is memory leak? why java.lang.ref.Finalizer eat so much memory

时光怂恿深爱的人放手 提交于 2019-12-17 04:27:33
问题 I ran a heap dump on my program. When I opened it in the memory analyzer tool, I found that the java.lang.ref.Finalizer for org.logicalcobwebs.proxool.ProxyStatement was taking up a lot of memory. Why is this so? 回答1: Some classes implement the Object.finalize() method. Objects which override this method need to called by a background thread call finalizer, and they can't be cleaned up until this happens. If these tasks are short and you don't discard many of these it all works well. However

F# Equivalent of Destructor

余生长醉 提交于 2019-12-14 03:40:08
问题 I am translating a C# class that wraps an unmanaged library to F#. I have run into the seemingly simple problem of rewriting the destructor that follows. class Wrapper { // P/Invoke ellided private SomeType x; public Wrapper() { x = new SomeType(); Begin(); } public ~Wrapper() { End(); } The simplified F# code I have at this point is as follows: type Wrapper() = [<Literal>] static let wrappedDll = "Library.dll" [<DllImport(wrappedDll , EntryPoint = "Begin")>] static extern void Begin() [

How to properly destroy a class

笑着哭i 提交于 2019-12-13 13:15:43
问题 In Ruby, I have a DAO class, which is extended by a class that makes managing the connections easier, which is extended by a class that represents and manipulates data in a DB, which is further extended by another class. To use an animal metaphor it would look like this: class Animal ... end class Mammal < Animal ... end class Feline < Mammal ... end class Cat < Feline ... end class Lion < Cat ... end ... In PHP, there is __destruct method that runs when you destroy/delete a class. And should

Good uses of the finalize() method [duplicate]

纵然是瞬间 提交于 2019-12-13 11:35:46
问题 This question already has answers here : Why would you ever implement finalize()? (21 answers) Closed 2 years ago . This is mostly out of curiosity. I was wandering if anyone has encountered any good usage for Object.finalize() except for debugging/logging/profiling purposes ? If you haven't encountered any what would you say a good usage would be ? 回答1: If your Java object uses JNI to instruct native code to allocate native memory, you need to use finalize to make sure it gets freed. 回答2:

Can the C# Disposable Pattern be simplified when only _unmanaged_ resources are at play?

为君一笑 提交于 2019-12-13 05:20:01
问题 (Note: this question is related to Calling GC.SuppressFinalize() from within a finalizer but is not a duplicate, as this question is explicitly specifically about the case of no managed resources , whereas that one is explicitly about when having both managed and unmanaged resources .) The classic Disposable Pattern in C# goes roughly like this: public class MyClass : IDisposable { bool alreadyDisposed = false; void Dispose(bool calledFromDisposeMethod) { if (!alreadyDisposed) {

How to implement object counter in Java

折月煮酒 提交于 2019-12-12 08:24:30
问题 An interviewer asked me that How can you implement a class Foo, where you will be able to count instances of that class. There are more threads which are creating instance of that class Foo. I replyed that with following code public class Foo { private static int count = 0; public Foo() { incrementCount(); } public void incrementCount() { synchronize (Foo.class) { count++; } } } She again asked me that If a thread ends, counter should be decrement, how can you do that? I didn't answer this

Finalizer Throws Random Exceptions, Raises Random Errors, Hangs App

孤者浪人 提交于 2019-12-12 01:34:09
问题 I have a class in C++/CLI that uses unmanaged resources (a HANDLE for a native thread (i.e. from CreateThread()) and an LPVOID for a fiber from CreateFiber/ConvertThreadToFiber). Under the advice I got from MSDN I'm cleaning up the unmanaged resources in the finalizer (!Fiber()), and the destructor (~Fiber()) is calling the finalizer. Here's the code: Fiber::~Fiber () { this->!Fiber(); } Fiber::!Fiber () { if (thread!=NULL) { delete thread; thread=NULL; } if (fiber!=NULL) { DeleteFiber(fiber)

Why is Cleaner action not invoked?

蹲街弑〆低调 提交于 2019-12-11 17:36:29
问题 This is a follow-up question from this one. Specifically I'm trying to verify correct use of an API which involves calling close() on Java objects holding heavy native resources over JNI. To re-iterate from that question, this is needed in both tests and production, tests because resources leak from one testcase to another and in production because, well, native resources should just be disposed in controlled manner. The code (all such Java objects that I want disposed would extend JPeer):

Is it harmless to call GC.SuppressFinalize within the finalizer?

▼魔方 西西 提交于 2019-12-11 04:06:56
问题 Because the finalizer/IDisposable and so-called " IDisposable pattern" topic tends to bring out lots of posturing, pontificating, and militant opinion ( not -respectively, here, here, here, and more), I really hesitate to ask this. Hoping to preempt those well-worn debates, I'm keeping to a very simple question which doesn't appear to have a concise answer on StackOverflow... Is calling GC.SuppressFinalize(this) vacuous once the object's finalizer has begun executing? More specifically or