finalizer

How to use PhantomReference as finalize() Replacement

倾然丶 夕夏残阳落幕 提交于 2019-12-05 08:04:09
Javadoc 8 for PhantomReference states: Phantom references are most often used for scheduling pre-mortem cleanup actions in a more flexible way than is possible with the Java finalization mechanism. So I tried creating a thread that is calling the close() method of a Test Object that is eligible for garbage collection. The run() tries to get all Test Objects pre-mortem . Actually the retrieved Test Objects are all null . The expected behavior is, that the Test Objects are retrieved and the close method is called. No matter how many Test Objects you create there is not a single Test Object that

How to identify the GC Finalizer thread?

扶醉桌前 提交于 2019-12-05 05:46:22
I have a .NET (C#) multi-threaded application and I want to know if a certain method runs inside the Finalizer thread. I've tried using Thread.CurrentThread.Name but it doesn't work (returns null). Anyone knows how can I query the current thread to discover if it's the Finalizer thread? The best way to identify a thread is through its managed id: Thread.CurrentThread.ManagedThreadId; Since a finalizer always runs in the GC's thread you can create a finalizer that will save the thread id (or the thread object) in a static valiable. Sample: public class ThreadTest { public static Thread GCThread

Disposing MemoryCache in Finalizer throws AccessViolationException

混江龙づ霸主 提交于 2019-12-05 03:01:34
EDIT See edit note at the bottom of the question for additional detail. Original question I have a CacheWrapper class which creates and holds onto an instance of the .NET MemoryCache class internally. MemoryCache hooks itself into AppDomain events, so it will never be garbage-collected unless it is explicitly disposed. You can verify this with the following code: Func<bool, WeakReference> create = disposed => { var cache = new MemoryCache("my cache"); if (disposed) { cache.Dispose(); } return new WeakReference(cache); }; // with false, we loop forever. With true, we exit var weakCache = create

Is it safe to access a reference type member variable in a finalizer?

谁说我不能喝 提交于 2019-12-05 02:31:53
In other words, class Foo { object obj; Foo() { obj = new object(); } ~Foo() { obj.ToString(); /* NullReferenceException? */ } } It's not safe since obj might have already been garbage collected. Also note that the garbage collector will not set the reference to null. So even checking for obj != null will not help you. See here for details: http://msdn.microsoft.com/en-us/magazine/cc163392.aspx#S3 " Generalizing this principle, in a Dispose method it’s safe to clean up all resources that an object is holding onto, whether they are managed objects or native resources. However, in a finalizer it

Which objects are finalized in Go by default and what are some of the pitfalls of it?

£可爱£侵袭症+ 提交于 2019-12-04 23:20:36
问题 The function runtime.SetFinalizer (x, f interface{}) sets the finalizer associated with x to f . What kind of objects are finalized by default? What are some of the unintended pitfalls caused by having those objects finalized by default? 回答1: The following objects are finalized by default: os.File: The file is automatically closed when the object is garbage collected. os.Process: Finalization will release any resources associated with the process. On Unix, this is a no-operation. On Windows,

Why does SHA1.ComputeHash fail under high load with many threads?

≯℡__Kan透↙ 提交于 2019-12-04 19:25:26
问题 I'm seeing an issue with some code I maintain. The code below has a private static SHA1 member (which is an IDisposable but since it's static , it should never get finalized). However, under stress this code throws an exception that suggests it has been closed: Caught exception. Safe handle has been closed" Stack trace: Call stack where exception was thrown at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success) at System.Security.Cryptography.Utils.HashData

Run a Method on deletion of an Object

戏子无情 提交于 2019-12-04 18:49:53
I am learning ruby and haven't found a way to override an equivalent object.delete function: This is how I am doing it: class Foo @@no_foo=0 def initialize @@no_foo+=1 end def delete #class specific cleanup... @@no_foo-=1 end def Foo.no_foo return "#@@no_foo" end end def delete(obj) #object independent cleanup... obj.delete return nil end foo1 = Foo.new foo2 = Foo.new puts Foo.no_foo puts foo2 foo2 = delete(foo2) puts foo2 puts Foo.no_foo As you can see, this is a bit of a hacky way of going about things... is there an easier way to go about this? Basically I would like to make my objects

Can we switch off finalizers?

别等时光非礼了梦想. 提交于 2019-12-04 17:26:03
问题 As there is little guarantee about when and even if finalizers run and finalizers are almost considered a smell nowadays - is there any way to persuade the JVM to completely skip all finalization processes? I ask because we have a mammoth application which, when moved to a newer JVM (not sure which at this stage) is brought to its knees by what looks very much like the known problems with finalisers (exceptions being thrown and therefore very slow GC). Added There is some discussion on

Why is finalizer called on object

北城余情 提交于 2019-12-04 14:08:28
Here is example program that exhibits surprising finalization behavior: class Something { public void DoSomething() { Console.WriteLine("Doing something"); } ~Something() { Console.WriteLine("Called finalizer"); } } namespace TestGC { class Program { static void Main(string[] args) { var s = new Something(); s.DoSomething(); GC.Collect(); //GC.WaitForPendingFinalizers(); s.DoSomething(); Console.ReadKey(); } } } If I run the program, what gets printed is: Doing something Doing something Called finalizer This appears as expected. Because there is a reference to s after the call to GC.Collect()

Cancelling a Task when an object is Finalized

不羁的心 提交于 2019-12-04 12:53:39
I have a class which starts a Task and want to ensure that the Task stops when the object is garbage collected. I have implemented the IDisposable pattern to ensure that if the object is disposed manually or used within a using block, then the Task stops correctly. However , I cant guarantee that the end user will call Dispose() or use the object within a using block. I know that the Garbage Collector will eventually call the Finalizer - does this mean that the task is left running? public class MyClass : IDisposable { private readonly CancellationTokenSource feedCancellationTokenSource = new