Questions tagged [finalization]

Finalization is intended for questions regarding programmatically releasing memory in conjuction with garbage collection, external resources and weak references.

References

57 questions
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
41
votes
6 answers

Should Java finalizer really be avoided also for native peer objects lifecycle management?

In my experience as a C++/Java/Android developer, I have come to learn that finalizers are almost always a bad idea, the only exception being the management of a "native peer" object needed by the java one to call C/C++ code through JNI. I am aware…
athos
  • 1,281
  • 3
  • 14
  • 24
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
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
18
votes
1 answer

Do C# try-finally CERs break in iterators?

Apparently, Constrained Execution Region guarantees do not apply to iterators (probably because of how they are implemented and all), but is this a bug or by design? [See the example below.] i.e. What are the rules on CERs being used with…
user541686
  • 205,094
  • 128
  • 528
  • 886
17
votes
3 answers

Do I need to finalize array of records in Delphi?

In my application I have the following record: TTransaction = record Alias: string Description: string Creation: TDateTime Count: Integer end; and I'm using this record in this array: Transactions = array of TTransaction; I'm keeping the…
EProgrammerNotFound
  • 2,403
  • 4
  • 28
  • 59
13
votes
1 answer

C# - What does "destructors are not inherited" actually mean?

Section 10.13, Destructors, of the C# Language Specification 3.0 states the following: Destructors are not inherited. Thus, a class has no destructors other than the one which may be declared in that class. The Destructors section of the C#…
Secret Squirrel
  • 251
  • 3
  • 10
12
votes
5 answers

What is the purpose of finalization in Java?

My understanding of finalization is this: To clean up or reclaim the memory that an object occupies, the Garbage collector comes into action. (automatically is invoked?) The garbage collector then dereferences the object. Sometimes, there is no way…
user244333
9
votes
2 answers

Finalizer statistics

Is there a way to obtain the total number of finalizers registered using runtime.SetFinalizer and which have not yet run? We are considering adding a struct with a registered finalizer to some of our products to release memory allocated using…
Florian Weimer
  • 32,022
  • 3
  • 48
  • 92
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
1 answer

What is the correct way to free an interface behind an OleVariant?

I am trying to find a safe/deterministic way to release an interface which is encapsulated in an OleVariant. AFAICS Delphi releases interface references at the end of a procedure, but in my case I have to do it earlier, because I have to shut down…
Jens Mühlenhoff
  • 14,565
  • 6
  • 56
  • 113
7
votes
1 answer

Does calling `gc()` manually, result in all `finalizers` being executed immediately?

I have some code that I suspect is leaking memory. As the code uses ccall and maintains significant information held inside pointers, which are supposed to be free'd by code that is ccalled during finalizers. In my debugging I am calling gc(). And I…
Frames Catherine White
  • 27,368
  • 21
  • 87
  • 137
6
votes
1 answer

finalization handle remains in the memory. how to remove this reference?

I need to optimize my application in memory usage. so I used .net performance profiler... but some references in my application are still alive and are never collected by GC even if I force it to collect. The reference that is alive is a…
6
votes
4 answers

Unit finalization order for application, compiled with run-time packages?

I need to execute my code after finalization of SysUtils unit. I've placed my code in separate unit and included it first in uses clause of dpr-file, like this: project Project1; uses MyUnit, // <- my separate unit SysUtils, Classes, …
Alex
  • 5,477
  • 2
  • 36
  • 56
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
1
2 3 4