finalizer

Are child objects still alive when Object.Finalize is called by GC?

徘徊边缘 提交于 2019-12-24 00:53:11
问题 Say, I have this class: class Test { readonly object _child = new Object(); // ... ~Test() { // access _child here // ... } } Is the _child object guaranteed to still be alive when ~Test is called by the garbage collector? Or should I first "pin" the _child with GCHandle.Alloc in the constructor? 回答1: Being a readonly field _child you can't lose its reference (unless you set it to null via reflection). Which means that till the Test is garbage collected _child will stay in memory for sure.

Invalidate Stream without Closing

巧了我就是萌 提交于 2019-12-24 00:03:27
问题 This is a followup to anonymous file streams reusing descriptors As per my previous question, I can't depend on code like this (happens to work in JDK8, for now): RandomAccessFile r = new RandomAccessFile(...); FileInputStream f_1 = new FileInputStream(r.getFD()); // some io, not shown f_1 = null; f_2 = new FileInputStream(r.getFD()); // some io, not shown f_2 = null; f_3 = new FileInputStream(r.getFD()); // some io, not shown f_3 = null; However, to prevent accidental errors and as a form of

When can't you use SafeHandle over Finalizer/IDisposable?

安稳与你 提交于 2019-12-23 13:06:47
问题 When seeing about the whole finalizer/IDisposable issue, it is usual to see that, at the end, after all the long description, there will be something to the meaning of "LOL what I said was actually useless, you should use SafeHandle instead bye!" So I am wondering in what case does SafeHandle not fit, such that you have to resort to the finalizer/IDisposable old way? 回答1: Clearly, when the unmanaged resource you are wrapping is not acquired through a handle. Which is rare but not unheard of.

Testing Finalizers and IDisposable

我们两清 提交于 2019-12-23 10:19:53
问题 The question is how can I test the fact that object disposes resources when finalise is called. The code for the class: public class TestClass : IDisposable { public bool HasBeenDisposed {get; private set; } public void Dispose() { HasBeenDisposed = true; } ~TestClass() { Dispose(); } } Please note that I don't care about the correct implementation of Dispose/Finalize just now as I want to find the way to test it first. At this stage it is enough to assume the HasBeenDisposed will be set to

Why does the c# garbage collector not keep trying to free memory until a request can be satisfied?

六月ゝ 毕业季﹏ 提交于 2019-12-23 07:38:40
问题 Consider the code below: using System; namespace memoryEater { internal class Program { private static void Main(string[] args) { Console.WriteLine("alloc 1"); var big1 = new BigObject(); Console.WriteLine("alloc 2"); var big2 = new BigObject(); Console.WriteLine("null 1"); big1 = null; //GC.Collect(); Console.WriteLine("alloc3"); big1 = new BigObject(); Console.WriteLine("done"); Console.Read(); } } public class BigObject { private const uint OneMeg = 1024 * 1024; private static int _idCnt;

when finalize() is being executed? [duplicate]

久未见 提交于 2019-12-22 16:46:08
问题 This question already has answers here : When is the finalize() method called in Java? (17 answers) Closed 5 years ago . In an interview i was asked,suppose JVM runs gc when object of class A is not in used. class A{ //some code here protected void finalize(){ //code here } } does it guarantee the execution of finalize(). I said yes The next ques was if obj of Class A is being used, if now JVM runs GC does it execute finalize(). I said no, it'll not execute this finalize() as JVM does not

AppDomain.Unload throws in Finalizer?

社会主义新天地 提交于 2019-12-22 05:21:59
问题 So here is the story so far, I have this worker thingy which uses an AppDomain to perform some task. The domain is expensive to setup and teardown. So I create a cache per-thread of WeakReference objects to the worker thingy like so: class Worker { [ThreadStatic] static Dictionary<string, WeakReference> _workers; public static Worker Fetch( ... ) { you get the idea } private AppDomain _domain; public Worker(...) { _domain = AppDomain.Create( ... ); } ~Worker() { AppDomain.Unload(_domain); } /

How to identify the GC Finalizer thread?

早过忘川 提交于 2019-12-22 04:43:05
问题 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? 回答1: 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

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

让人想犯罪 __ 提交于 2019-12-22 04:09:15
问题 In other words, class Foo { object obj; Foo() { obj = new object(); } ~Foo() { obj.ToString(); /* NullReferenceException? */ } } 回答1: 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

Run a Method on deletion of an Object

六月ゝ 毕业季﹏ 提交于 2019-12-21 23:09:11
问题 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