Questions tagged [finalize]

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

A finalize(), or 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 finalize() 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.

Starting Java9. finalize() method has been officially deprecated. javadoc for Java9's java.lang.Object finalize()

228 questions
4
votes
1 answer

Ensuring that "finally" runs on same thread when thread terminates

Here's a puzzle for you guys. I have a multithreaded program in which some threads work with managed resources like locks and semaphores. Some of the lock release primitives can only be performed from the same thread on which the lock acquire was…
Ken Birman
  • 1,088
  • 8
  • 25
3
votes
3 answers

Definition: Unfinalized versus finalizable object

In order to understand weak references in Java, I have had to consult the Java Language Specification. The following part, from section 12.6, puzzles me: An unfinalized object has never had its finalizer automatically invoked; a finalized…
3
votes
1 answer

Finalize Queue Not Releasing Fast Enough

I have a c# 3.5 framework Windows application that runs against an Oracle DB located on a server. One of the forms of the application has eight tabs across the top. Within the tab content area of each tab is a combobox. The combobox displays the…
joe
  • 31
  • 2
3
votes
3 answers

Java 8 is this assertion about finalize method correct?

I been reading the book OCA Java SE 8 Programmer I Exam Guide by Kathy Sierra + Bert Bates. There is something about the finalize method I don't get it quite well. It states on page 218: Calling finalize() can actually result in saving an object…
chiperortiz
  • 4,751
  • 9
  • 45
  • 79
3
votes
1 answer

Why exactly PhantomReference should be preferred to finalize?

They both can be used for cleanup, there is almost no guarantees, but PR requires more harness coding. So, having two options, why exactly I have to prefer one to another? Javadoc 9 describes finalize as very problematic, but that doesn't make its…
Pavel Vlasov
  • 4,206
  • 6
  • 41
  • 54
3
votes
0 answers

finalize() method breaks mid-execution?

Question I have a URLClassLoader anonymous class. In it, I've overridden the finalize() method so that the object will automatically close itself when garbage collected: protected void finalize() throws Throwable { close(); } and I've…
Kröw
  • 504
  • 2
  • 13
  • 31
3
votes
1 answer

rxjs takeUntil do not execute finalize

I have the following countdown: userClick=new Subject() resetCountdown(){this.userClick.next()} setCountDown() { let counter = 5; let tick = 1000; this.countDown = timer(0, tick) .pipe( take(counter), map(() =>…
farahm
  • 1,326
  • 6
  • 32
  • 70
3
votes
2 answers

How finalizable objects takes at least 2 garbage collection cycles before it can be reclaimed?

I'm reading this article and I can't really understand how the finalizable objects (objects which override the finalize method) takes at least 2 GC cycles before it can be reclaimed. It takes at least two garbage collection cycles (in the best…
Lavish Kothari
  • 2,211
  • 21
  • 29
3
votes
1 answer

Bug JDK-8191002 unclear if programming error or JRE error

Regarding the issue described in JDK-8191002, also discussed in Java Cipher - PBE thread-safety issue : it is unclear to me if the usage of Arrays.fill() in the finalize() method is correct or if it is a bug. Some answers suggest that a…
3
votes
3 answers

c# my destructor isn't being called?

I have this simple code and trying to call the destructor but I can't call it :( I know that GarbageCollector runs when it's necessary, so I used GC.WaitForPendingFinalizers(); but it didn't work either. Here is my code: class Program { …
Uur
  • 33
  • 1
  • 4
3
votes
2 answers

Cipher.doFinal output size

I am doing AES CBC decryption in java using javax.crypto . I am using the following Cipher class methods: public final void init (int opmode, Key key, AlgorithmParameters params) method for initialization, final int update(byte[] input, int…
vikas
  • 31
  • 1
  • 2
3
votes
3 answers

In a yield return function is it possible to ensure that the finalizer is called on the same thread?

I have a tricky problem that is turning up in some of my code. I have a cache manager that either returns items from the cache or calls a delegate to create them (expensively). I'm finding that I'm having problems with the finalize part of my method…
3
votes
4 answers

finalize() not getting called

Why is finalize() not being called here. The code compiled and ran successfully but there wasn't any output. package temp; public class Temp { int i; Temp(int j) { i = j; } public void finalize() { if (i == 10) { …
daft300punk
  • 169
  • 3
  • 16
3
votes
3 answers

Why Netbeans warns me of finalize?

I implemented a class in Java7. It does not inherit/implements anything. It uses Tess4J so I thought it would be nice to free the resources in the end. So I overrode the finalize() method like this: @Override protected void finalize() throws…
nuoritoveri
  • 2,494
  • 1
  • 24
  • 28
3
votes
3 answers

The finalize method in

I am reading Thinking in Java, 4th Edition. I found a problem: when I tested the source code in Eclipse IDE, I found the results are different. I hope someone can help me! Here is the source code class Book { boolean checkedOut = false; …
Wenchao Cai
  • 83
  • 1
  • 4