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
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
5
votes
2 answers

problem with a final procedure (segfault with gfortran)

consider the following small program that reproduces the segmentation fault I get with gfortran gcc 9.2.0 for mac os): module stringmod type :: str_t character(len=:), allocatable :: s contains final :: finalize end type str_t …
R. Hassani
  • 155
  • 8
5
votes
1 answer

Null reference used for synchronization (monitor-enter)

When I turn off my Internet connection and log out from Firebase app, I can't return my app. Also get next error: 2019-11-05 20:26:19.364 5593-5611/com.mandarine.target_list E/System: Uncaught exception thrown by finalizer 2019-11-05…
Morozov
  • 4,968
  • 6
  • 39
  • 70
5
votes
1 answer

When GC.KeepAlive(this) is needed when doing P/Invoke on unmanaged resources?

I have a TestNet wrapper for a native component. The native component exposes a blocking TestNative::Foo() that communicates with managed part through calling managed callbacks and a weak GCHandle that is used to retrieve the reference to the .NET…
ceztko
  • 14,736
  • 5
  • 58
  • 73
5
votes
0 answers

Gradle missing dynamic task

I have a declared task: tasks.getByName('connectedAndroidTest').finalizedBy 'archiveReports' But when I am trying to build project it tells me that: Caused by: org.gradle.api.UnknownTaskException: Task with name 'connectedAndroidTest' not found in…
5
votes
3 answers

What happens if an exception is thrown during finalize()

What would happen if an exception is thrown during the execution of finalize()? Is the stack unwind like normally? Does it continue finalize() and ignore the exception? Does it stop finalize() and continue GC the object? Or something else? I'm not…
Peter Ølsted
  • 311
  • 1
  • 4
  • 11
5
votes
1 answer

Implement finalizable dispose pattern with multiple related finalizable objects

I'm roughly familiar with the Dispose pattern for non-finalizable types, eg, types that wrap some sort of managed resource that we want to have deterministic cleanup done on. These sort of types typically do not implement a finalizer, since it is…
antiduh
  • 11,853
  • 4
  • 43
  • 66
5
votes
3 answers

Testing Finalizers and IDisposable

The question is how can I test the fact that object disposes resources when finalise is called. The code for the class: public class TestClass : IDisposable { public bool HasBeenDisposed {get; private set; } public void Dispose() { …
Dmytrii Nagirniak
  • 23,696
  • 13
  • 75
  • 130
5
votes
3 answers

Can code be run when an object falls out of scope in .Net?

Is there any way to "automatically" run finalization / destructor code as soon as a variable loses scope in a .Net language? It appears to me that since the garbage collector runs at an indeterminate time, the destructor code is not run as soon as…
Matt Hamsmith
  • 3,955
  • 1
  • 27
  • 42
5
votes
3 answers

Finalizer not called before second object is created except when using weakref

I was playing around with ruby finalizers and noticed some behaviour that is very strange to me. I could reduce the triggering code to the following: require "weakref" class Foo def initialize ObjectSpace.define_finalizer(self,…
Simon Kohlmeyer
  • 157
  • 1
  • 8
5
votes
4 answers

Store 'this' at finalization

How could be defined a code that store 'this' during class finalization? How the garbage collector should behave (if defined somewhere)? In my mind the GC should finalize multiple times the class instance, and the following test application shall…
Luca
  • 11,646
  • 11
  • 70
  • 125
5
votes
3 answers

What makes Finalizers so costly?

From Effective Java: Oh, and one more thing: there is a severe performance penalty for using finalizers. On my machine, the time to create and destroy a simple object is about 5.6 ns. Adding a finalizer increases the time to 2,400 ns. In other…
Geek
  • 26,489
  • 43
  • 149
  • 227
5
votes
4 answers

GC.Collect() not collecting immediately?

In the course of a discussion in chat, I wrote this console application. Code: using System; class Program { static void Main(string[] args) { CreateClass(); Console.Write("Collecting... "); GC.Collect(); …
Kendall Frey
  • 43,130
  • 20
  • 110
  • 148
5
votes
3 answers

Proper way to call glDeleteTextures in a .net object's finalizer

I'm about to implement a managed wrapper class around an OpenGL texture and I want the object finalizer to call glDeleteTextures. So, the thread calling the finalizer (GC thread?) must be bound to the OpenGL rendering context in which the texture…
Nicolas Repiquet
  • 9,097
  • 2
  • 31
  • 53
5
votes
8 answers

IDisposable, Finalizers and the definition of an unmanaged resource

I'm trying to make sure that my understanding of IDisposable is correct and there's something I'm still not quite sure on. IDisposable seems to serve two purpose. To provide a convention to "shut down" a managed object on demand. To provide a…
xyz
  • 27,223
  • 29
  • 105
  • 125