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
11
votes
1 answer

GC.SuppressFinalize performance compared to non-finalizable object

Is a finalizable object with GC.SuppressFinalize the same as a normal unfinalizable object? The code below seems to prove they're treated differently, both on .NET 2 and 4: class Class1 { public Class1() { …
thecoop
  • 45,220
  • 19
  • 132
  • 189
11
votes
3 answers

How to implement object counter in Java

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…
Android Learner
  • 2,559
  • 6
  • 34
  • 43
11
votes
3 answers

Finalizer not called

I have a class in C# where I want to close out some communication ports properly when my class is being disposed. However, the finalizer is never being called when I exit the program. Why is that? Am I doing something wrong? I am calling the…
Spenduku
  • 409
  • 4
  • 12
11
votes
2 answers

C++/CLI: preventing garbage collection on managed wrapper of unmanaged resource

I have a C++ unmanaged class NativeDog that needs to be used from C#, so I've create a wrapper class ManagedDog. // unmanaged C++ class class NativeDog { NativeDog(...); // constructor ~NativeDog(); // destructor ... } // C++/CLI…
Laurent
  • 5,953
  • 14
  • 43
  • 59
11
votes
6 answers

Finalizer and IDisposable

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…
Raj Rao
  • 8,872
  • 12
  • 69
  • 83
11
votes
9 answers

Why are there finalizers in java and c#?

I'm not quite understanding why there are finalizers in languages such as java and c#. AFAIK, they: are not guaranteed to run (in java) if they do run, they may run an arbitrary amount of time after the object in question becomes a candidate for…
RCIX
  • 38,647
  • 50
  • 150
  • 207
10
votes
3 answers

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

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?
user811773
10
votes
3 answers

c#: How to handle finalizer exceptions from a 3rd-party library?

Finalizers are always called by .net framework, so the sequence could be out of control; and even if the constructor failed, the destructor still can be triggered. This could bring troubles, when such finalizer exceptions come from a 3rd-party…
athos
  • 6,120
  • 5
  • 51
  • 95
10
votes
1 answer

How to find non-destroyed but GC'ed Javascript objects in Chrome?

I have an application with some objects (of type BaseTexture) on which an explicit destroy function should be called when they aren't of any more use. Otherwise they might leak some memory. This should be done before they are garbage collected (and…
Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301
10
votes
2 answers

What's the Java equivalent of .net's GC.KeepAlive?

.NET has a function called GC.KeepAlive(Object). Its sole purpose is to ensure the lifetime of the referenced object lasts until code flow reaches the call. This is normally not necessary unless one is interoperating with native code. I have a…
Barry Kelly
  • 41,404
  • 5
  • 117
  • 189
10
votes
4 answers

RAII in Ruby (Or, How to Manage Resources in Ruby)

I know it's by design that you can't control what happens when an object is destroyed. I am also aware of defining some class method as a finalizer. However is the ruby idiom for C++'s RAII (Resources are initialized in constructor, closed in…
moogs
  • 8,122
  • 8
  • 44
  • 60
10
votes
2 answers

Can I access reference type instance fields/properties safely within a finalizer?

I always thought that the answer to this was no, but I cannot find any source stating this. In my class below, can I access (managed) fields/properties of an instance of C in the finalizer, i.e. in ReleaseUnmanaged()? What restrictions, if any, are…
Rob
  • 4,327
  • 6
  • 29
  • 55
10
votes
3 answers

Why doesn't Thread implement IDisposable?

I noticed that System.Threading.Thread implements a finalizer but not IDisposable. The recommended practice is to always implement IDisposable when a finalizer is implemented. Jeffrey Richter wrote that the guideline is "very important and should…
Special Touch
  • 476
  • 3
  • 15
10
votes
4 answers

Gracefully finalizing the SoftReference referent

I am using a search library which advises keeping search handle object open for this can benefit query cache. Over the time I have observed that the cache tends to get bloated (few hundred megs and keeps growing) and OOMs started to kick in. There…
mindas
  • 26,463
  • 15
  • 97
  • 154
10
votes
3 answers

What are the Finalizer Queue and Control+ThreadMethodEntry?

I have a WindowsForms app that appears to leak memory, so I used Redgate's ANTS Memory Profiler to look at the objects I suspect and find that they are only held by objects already on the Finalizer Queue. Great, exactly what is a the Finalizer…
flipdoubt
  • 13,897
  • 15
  • 64
  • 96