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
415
votes
13 answers

Use of Finalize/Dispose method in C#

C# 2008 I have been working on this for a while now, and I am still confused about the use of finalize and dispose methods in code. My questions are below: I know that we only need a finalizer while disposing unmanaged resources. However, if there…
ant2009
  • 27,094
  • 154
  • 411
  • 609
72
votes
3 answers

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

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?
fuyou001
  • 1,594
  • 4
  • 16
  • 27
68
votes
15 answers

Should "Dispose" only be used for types containing unmanaged resources?

I was having a discussion with a colleague recently about the value of Dispose and types that implement IDisposable. I think there is value in implementing IDisposable for types that should clean up as soon as possible, even if there are no…
Steve Dunn
  • 21,044
  • 11
  • 62
  • 87
51
votes
1 answer

Can a simple difference in Python3 variable names alter the way code runs?

This code... class Person: num_of_people = 0 def __init__(self, name): self.name = name Person.num_of_people += 1 def __del__(self): Person.num_of_people -= 1 def __str__(self): return 'Hello, my…
Corey
  • 816
  • 9
  • 19
51
votes
8 answers

Is the destructor called if the constructor throws an exception?

Looking for an answer for C# and C++. (in C#, replace 'destructor' with 'finalizer')
Qwertie
  • 16,354
  • 20
  • 105
  • 148
48
votes
5 answers

Static Finalizer

What is the right way to perform some static finallization? There is no static destructor. The AppDomain.DomainUnload event is not raised in the default domain. The AppDomain.ProcessExit event shares the total time of the three seconds (default…
Michael Damatov
  • 15,253
  • 10
  • 46
  • 71
45
votes
5 answers

GC.Collect() and Finalize

Ok, it's known that GC implicitly calls Finalize methods on objects when it identifies that object as garbage. But what happens if I do a GC.Collect()? Are the finalizers still executed? Someone asked me this and I answered a "Yes" and then I…
Sandeep
  • 5,581
  • 10
  • 42
  • 62
40
votes
4 answers

The difference between a destructor and a finalizer?

Please Note: This question is about the difference in terminology between the words "destructor" and "finalizer" and their correct usage. I have merely provided examples of their use in C# and C++/CLI to demonstrate why I am asking the question. I…
Greg Beech
  • 133,383
  • 43
  • 204
  • 250
34
votes
5 answers

Why should we call SuppressFinalize when we don't have a destructor

I have few Question for which I am not able to get a proper answer . 1) Why should we call SuppressFinalize in the Dispose function when we don't have a destructor . 2) Dispose and finalize are used for freeing resources before the object is garbage…
somaraj
  • 435
  • 1
  • 5
  • 6
31
votes
4 answers

Why structs cannot have destructors?

What is best answer on interview on such question you think? I think I didn't find a copy of this here, if there is one please link it.
Valentin Kuzub
  • 11,703
  • 7
  • 56
  • 93
31
votes
1 answer

Why is the finalize() method deprecated in Java 9?

(This question is different from Why would you ever implement finalize()? This question is about deprecation from the Java platform, and the other question is about whether one should use this mechanism in applications.) Why is the finalize() method…
J.J. Beam
  • 2,612
  • 2
  • 26
  • 55
30
votes
3 answers

finalize() called on strongly reachable objects in Java 8

We recently upgraded our message processing application from Java 7 to Java 8. Since the upgrade, we get an occasional exception that a stream has been closed while it is being read from. Logging shows that the finalizer thread is calling…
Nathan
  • 1,418
  • 16
  • 32
29
votes
4 answers

Replacing finalize() in Java

Object.finalize() is deprecated in Java 9, and I think I understand the reasons why, but I'm having trouble seeing how to replace it. I have a utility class called Configuration which essentially has a single instance that owns everything in the…
Michael Kay
  • 156,231
  • 11
  • 92
  • 164
27
votes
6 answers

Why do finalizers have a "severe performance penalty"?

Effective Java says : There is a severe performance penalty for using finalizers. Why is it slower to destroy an object using the finalizers?
unj2
  • 52,135
  • 87
  • 247
  • 375
24
votes
4 answers

Should Java 9 Cleaner be preferred to finalization?

In Java, overriding the finalize method gets a bad rap, although I don't understand why. Classes like FileInputStream use it to ensure close gets called, in both Java 8 and Java 10. Nevertheless, Java 9 introduced java.lang.ref.Cleaner which uses…
Aleksandr Dubinsky
  • 22,436
  • 15
  • 82
  • 99
1
2 3
29 30