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

What is the up-front cost of an object being finalizable?

Discussions of finalizable objects in Java typically discuss the common indirect costs that happen when finalizable objects (and their associated resources) cannot be quickly garbage collected. I'm more interested, at the moment, in what the actual…
Theodore Murdock
  • 1,538
  • 1
  • 13
  • 28
8
votes
6 answers

Phantom Referenced Objects

Phantom References serve for post-mortem operations. The Java specification states that a phantom referenced object will not be deallocated until the phantom-reference itself is cleaned. My question is: What purpose does this feature (object not…
Shimi Bandiel
  • 5,773
  • 3
  • 40
  • 49
8
votes
1 answer

Why have a static weakreference to service object?

I've come across this android code below. Is there ever a use case of creating a static weakreference object in a service to get its reference? I know that static variables are not eligible for Garbage collection. In general, Does creating a weak…
Daniel P
  • 83
  • 2
  • 3
8
votes
4 answers

the correct technique for releasing a socket/event/ummaged code with the dispose/finalize pattern

How to implement the Dispose pattern when my class contains a socket & event? Should it be something like this? class MyClass { Socket m_ListenerSocket = new Socket(); book m_Disposed=false; public void Dispose() { Dispose(true); …
roman
  • 607
  • 2
  • 10
  • 18
7
votes
2 answers

Objects not being finalized and Finalizer thread not doing anything

On our server, we started to have problems with OutOfMemoryError. We analyzed the heap dumps using Eclipse Memory Analysis, and found, that many objects were held to do finalization (about 2/3 of the heap): We found, that it could be some…
Oliv
  • 10,221
  • 3
  • 55
  • 76
7
votes
4 answers

VB.NET - Should a Finalize method be added when implementing IDisposable?

In Visual Studio, when I type the line "Implements IDisposable", the IDE automatically adds: a disposedValue member variable a Sub Dispose() Implements IDisposable.Dispose a Sub Dispose(ByVal disposing As Boolean) The Dispose() should be left…
Laurent
  • 5,953
  • 14
  • 43
  • 59
6
votes
1 answer

Kotlin super.finalize()

While migration to Kotlin from Java I faced with a problem. I overrided Object's finalize() method: @Override protected void finalize() throws Throwable { stopTimer(); super.finalize(); } When I tried to do the same with Kotlin I found to…
Sigmund
  • 748
  • 1
  • 8
  • 28
6
votes
6 answers

Best practice for implementing in Ada (2005 or 2012) an equivalent of the java finalize block

Java has the finalize block which allows to execute some statements after a block is left (executed even if an exception is raised). Example: try { ... } catch (Exception e) { ... } finally { ... // any code here } Ada has the controlled…
ciceron
  • 586
  • 5
  • 10
6
votes
2 answers

Is there any need for java's finalize method?

Everything that I have read about the java finalize method says NOT to use it. It seems that it is almost never guaranteed to be called and may present problems even when it is. There are a few other questions that ask when to use it, and it seems…
Matthew
  • 7,440
  • 1
  • 24
  • 49
6
votes
2 answers

Why is finalize() only called once by garbage collector?

Quotes from SCJP 6 study guide: In the finalize() method you could write code that passes a reference to the object in question back to another object, effectively uneligiblizing the object for garbage collection. If at some point later on this…
Rahul Kurup
  • 693
  • 3
  • 9
  • 22
6
votes
3 answers

How is an object marked as finalized in Java (so that the finalize method wouldn't be called the second time)?

The main question is in the topic but let me show my vision of finalization proccess in Java so that I can ask you a little more. Well the gc starts garbage collection by marking all live objects. When all reachable objects are marked as "live".…
Anton Kasianchuk
  • 1,177
  • 5
  • 13
  • 31
6
votes
2 answers

MongoDB Map Reduce - Finalize to skip some results

I have a map reduce function that works on a collection as follows: function Map() { emit ( this.name, { count : 1, flag : this.flag } ); } function Reduce(key, values) { var count =…
checklist
  • 12,340
  • 15
  • 58
  • 102
5
votes
3 answers

How does finalize() work in java?

So, I recently discovered the finalize method in Java (not sure why I missed it before, but there it is). This seems like it could be the answer to a lot of the issues I'm working with, but I wanted to get a bit more information first. Online, I…
5
votes
2 answers

When is finalize called on singletons when a classloader is released?

By "released" I mean there are no references to the classloader remaining. We're running into a problem where a frequently-redeployed Java EE application eats up permgen space. Analysis shows that a singleton in the Java EE app has passed…
Ed Staub
  • 15,480
  • 3
  • 61
  • 91
5
votes
6 answers

What happened internally (JVM) when System.gc() or finalize() method called?

What happened internally (JVM) when System.gc() or finalize() method called? Is this really collect garbage or reduce performance ?
Isabel Jinson
  • 8,541
  • 16
  • 59
  • 75
1 2
3
15 16