Questions tagged [finalizer]

A finalizer is a special method in an object-oriented language that is executed when an object is garbage collected.

A finalizer is a special method in an object-oriented language that is executed when an object is garbage collected.

Java Documentation (Javadoc) defines the moment when the finalizer is invoked as:

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

However, this may never happen in the life of a program if the object is always accessible. Due to the lack of programmer control over their execution, it is usually recommended to avoid finalizers for any but the most trivial operations.

442 questions
8
votes
2 answers

Will this be a valid base class for IDisposable

IDisposable pattern is expensive to implement. I've counted 17 lines of code before even starting to actually dispose resources. Eric Lippert recently wrote a blog post bringing up an interesting point: any time a finalizer runs, it is a bug. I…
Xiaoguo Ge
  • 2,177
  • 20
  • 26
8
votes
4 answers

java: wait(), notify() and synchronized blocks

I learned that calling an Object's wait() method will release the object monitor, if present. But I have some questions regarding calling notify() on this object by another thread: (when) will the waiting thread wake up, if another (a 3rd) thread…
MRalwasser
  • 15,605
  • 15
  • 101
  • 147
8
votes
1 answer

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

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…
QWill
  • 113
  • 1
  • 6
8
votes
2 answers

Disposing MemoryCache in Finalizer throws AccessViolationException

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,…
ChaseMedallion
  • 20,860
  • 17
  • 88
  • 152
8
votes
3 answers

'Uncaught exception thrown by finalizer' when opening MapActivity

I have these lines in my code: // create tab4 intent = new Intent(this, ActWhereAmI.class) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); tabspecWhereAmI = tabHost .newTabSpec("tab4") …
Bob
  • 22,810
  • 38
  • 143
  • 225
8
votes
4 answers

Is closing the connection in finalize best practice?

Possible Duplicate: Why would you ever implement finalize()? I saw some java files with the following code: public void finalize() { if (conn != null) { try { conn.close(); } catch (SQLException e) { } …
developer
  • 9,116
  • 29
  • 91
  • 150
7
votes
4 answers

Aren't destructors guaranteed to finish running?

Destructors are weird. I was attempting to eliminate the need of using the disposable pattern by using 'smart' reference management, ensuring that the garbage collector could collect objects at the correct time. In one of my destructors I had to…
Steven Jeuris
  • 18,274
  • 9
  • 70
  • 161
7
votes
6 answers

In what situation(s) would a reference point to an object that was queued for garbage collection?

I'm reading through a C# topic on Dispose() and ~finalize and when to use which. The author argues that you should not use references within your ~finalize because it's possible the object you're referencing may already be collected. The example…
chopperdave
  • 526
  • 4
  • 12
7
votes
2 answers

Why .NET Object has method Finalize()?

I know that Finalize method is used by garbage collector to let object free up unmanaged resources. And from what I know, Object.Finalize is never called directly by GC (object is added to f-reachable queue during it's construction if it's type…
Sasha
  • 8,537
  • 4
  • 49
  • 76
7
votes
2 answers

Finalized is holding lot of memory 2.5 gig out of 4 g

I have read many articles how finalizer works. Here is my understanding: If a class have finalize method implemented Jvm would create an instance of Finalizer as a watch dog on that object. When GC runs it would mark object to be disposed and add…
7
votes
5 answers

What if a finalizer makes an object reachable?

In Java, finalize is called on an object (that overrides it) when it's about to be garbage collectioned, so when it's unreachable. But what if the finalizer makes the object reachable again, what happens then?
Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301
7
votes
4 answers

How to identify the GC Finalizer thread?

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…
Dror Helper
  • 30,292
  • 15
  • 80
  • 129
7
votes
1 answer

Memory leak Finalizer error

I've been studying memory leaks and using memory analyser tool to check them. So, as a practice, I have following code which leaks an activity as the anonymous inner class holds a reference to the activity. Here's the code : public class…
gaurav jain
  • 3,119
  • 3
  • 31
  • 48
7
votes
4 answers

Performance implications of finalizers on JVM

According to this post, in .Net, Finalizers are actually even worse than that. Besides that they run late (which is indeed a serious problem for many kinds of resources), they are also less powerful because they can only perform a subset of the…
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
7
votes
2 answers

Why "Finalize method should not reference any other objects"?

I have been pondering why it is recommended that we should not release managed resources inside finalize. If you see the code example at http://msdn.microsoft.com/en-us/library/system.gc.suppressfinalize.aspx , and search for string "Dispose(bool…
mishal153
  • 1,498
  • 3
  • 26
  • 37