Questions tagged [finalize]
A finalize() is a special method in an object-oriented language that is executed when an object is garbage collected.
228
questions
0
votes
2
answers
99
views
junit.framework.AssertionFailedError: Expectation failure on verify: finalize(): expected: 1, actual: 0
I have an old test in code which uses EasyMock instead of Mockito.
When I run test locally then it works, however when it runs in gitlab pipeline then I get:
junit.framework.AssertionFailedError:
...
0
votes
1
answer
70
views
Why is subclass's finalize method, which overrides java.lang.Object's finalize method, called?
Here is the code block
public class Person {
int age;
String name;
//
@Override
public void finalize() throws Throwable{
System.out.println("Garbage collected --->...
0
votes
0
answers
52
views
Garbage Collection and Finalize()
I am trying to write a program that moves an elevator randomly through the floors of different buildings with different stories. I need to use the finalize() method to clean up after every building. ...
-2
votes
1
answer
135
views
Java Object.finalize() vs. C# IDisposable
I have extensive Java experience and know why finalize() is scheduled for a removal soon. On the other hand, my knowledge about C# is skin-deep - I am more or less aware about what features it offers ...
0
votes
0
answers
84
views
Java reference-aware cache
I have memory-expensive objects, that sometimes come with identical content. I would like to cache them as long as they're referenced at least once, and evict from the cache if all referenced are ...
0
votes
2
answers
1k
views
RxJS finalize operator vs tap({ finalize: () => {} })
Is there any difference between A and B? Are there any cases where one would behave differently than the other?
A)
observableHere
.pipe(
finalize(() => {
// Do stuff here
...
0
votes
1
answer
86
views
java System.gc() only works if I set obj=null implicitly?
I'm using Java 8 on windows 10 and I have this code snippet to test how System.gc() works:
public class testGc{
static class MyObject{
@Override
protected void finalize() throws ...
0
votes
0
answers
686
views
Many objects with finalize() in heap, i.e. PgConnection
When analyzing a heap dump of my application I noticed 1478 instances of java.lang.red.Finalizer class. 501 of them was referencing org.postgresql.jdbc.PgConnection class and another 501 sun.security....
2
votes
2
answers
2k
views
RxJS finalize(): pass the last emitted value to the callback
In my Angular service, I've got a loading prop which I set true as a first thing done in the method fetching data. I want to set loading to false after the data is downloaded. I was doing it in ...
0
votes
0
answers
364
views
Problem with MPI_Finalize at the end of a code
I have a Fortran 90 code which uses the MPI library. At the end of the code, I have the following statements to finish the program:
call MPI_Finalize(ierr)
write(6,*) ierr
stop
The code to ...
1
vote
0
answers
519
views
AlreadyFinalized("Context was already finalized.")
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import padding
import os
...
0
votes
2
answers
481
views
calling finalize() of super class in Kotlin
I have the following Java code I wanted to convert into Kotlin:
@Override
protected void finalize() throws Throwable {
try {
release();
} finally {
super.finalize();
}
}
...
1
vote
0
answers
197
views
Fortran Final Procedures Not Being Called in the Expected Manner [duplicate]
I have a container type that has an allocatable array of another type. When the final procedure is called on the container type and the allocatable array is deallocated, it doesn't trigger the final ...
-1
votes
1
answer
148
views
C# Will the typical IDisposable pattern not cause resource leak in some condition
Below is a typical IDisposable implementation for C# class which has managed and unmanaged resources both. My question is, Can there be a situation where ~DisposableObject() destructor method gets ...
0
votes
0
answers
180
views
Unload a dll loaded by a classloader in Java11
I had a scenario where I need to perform database connectivity using Java11 twice using Windows Authentication on SQL server.
Initially, the sqljdbc_auth.dll is loaded for the first call and the ...
2
votes
1
answer
389
views
Alternative to Java finalizer
I am implementing a locking service in a distributed system using Mysql GET_LOCK. Upon calling my getLock() method, if a lock is obtained by a client, I make an entry to DB and delete the entry when ...
0
votes
1
answer
353
views
Trying to decrement my static count variable in java jdk 1.8.0
Using finalize function, when I point an object to null it’s not decrementing although it is incrementing properly. I also mentioned garbage collector which will listen to its own self, I know but why ...
1
vote
1
answer
435
views
Prevent object garbage collection before other objects
I have a weakly-referenced array of weak references to further objects like so:
public class Foo{
WeakReference<WeakReference<Bar>[]> cache;
}
public class Bar{
private final ...
2
votes
1
answer
263
views
Java: impact of throwing an exception in finalize()
I am confused about this statement in Java Doc of Object#finalize():
Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored.
Does ...
5
votes
2
answers
189
views
If Java's generational garbage collectors traverse the graph of live objects, how do they know which objects to call finalize() on? [duplicate]
My understanding is that GCs like ParallelGC and G1 are "generational" collectors. Garbage Collection almost happens as a byproduct, since you move all live objects to a new heap region and anything ...
0
votes
1
answer
206
views
How do i override the finalize() method of the class object itself?
My problem is straightforward. I have a class that has a static ThreadPoolExecutor object. This threadpoolexecutor object is thus shared by all the instances of the class. If i were to shut the entire ...
0
votes
0
answers
84
views
Java testing: How to run custom code when instances become eligible for GC?
I want to know if there is a way to detect when a CompletableFuture instance becomes eligible for garbage collection before anyone ever called .get() on it. I want to do this detection during Java ...
1
vote
1
answer
1k
views
what to use instead of finalize() in java
Let's consider following code:
class Table {
private static int number_of_Tables=0;
public Table(){
++number_of_Tables;
}
public void finalize(){
--number_of_Tables;
}
public static int ...
3
votes
3
answers
212
views
Java 8 is this assertion about finalize method correct? [duplicate]
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 ...
2
votes
2
answers
345
views
Can one native method be called from another native method?
I have a class in Java which contains certain native method declarations. It contains a call to detroy() within finalize method which is now deprecated. As an alternative to finalize, I arrived at ...
5
votes
1
answer
2k
views
How to replace the deprecated finalize() method from a Java 11 project with inter - dependencies among classes
I have a Java 11 project involving multiple classes. In the present scenario, 2 of my classes - A and B - implement the java finalize() method, which is now deprecated for good. I understand the ...
1
vote
1
answer
2k
views
Is there any destroy method in JavaFX we must call before leaving the page/controller
In my JavaFX application, at a specific page I have to initialize (when loading the page) some devices such as iris scanner, fingerprint scanner, cameras etc. Before leaving the page/controller it is ...
3
votes
1
answer
396
views
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 ...
1
vote
1
answer
1k
views
Why is the retained heap size of the FinalizerReference class so large in the (memory) Profiler of Android Studio?
I have read this question about Finalizer's lion share of the heap. It dates from 2011 when the tools were different and the Java class still had a different name (Finalizer vs FinalizerReference). So ...
3
votes
0
answers
158
views
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() ...
31
votes
1
answer
25k
views
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 ...
0
votes
0
answers
350
views
How to write code coverage for subscribe method with finalize keyword in Angular Jamsine?
Angular version: "@angular/core": "6.1.7"
Jamsine version: "jasmine-core": "~2.8.0"
File name: app.component.ts (Method inside the component file)
testFunction() {
this.testClassService....
3
votes
1
answer
2k
views
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)
....
1
vote
1
answer
3k
views
Memory leak of java.util.ref.Finalizer while Finalizer thread is waiting
Analysing a heap dump I look for instances of java.lang.ref.Finalizer class. java.lang.ref.Finalizer has 'next' and 'prev' member fields for maintaining linked list. I always get FileInputStream as a ...
0
votes
0
answers
49
views
Why can't javac compile class with finalize
I have read similar questions on the stackoverflow,but they answer what to do about it (compile it with -Xlint:deprecation) and not why the error occurs in the first place.I want to know why the error ...
29
votes
5
answers
9k
views
In Java, how to check that AutoCloseable.close() has been called?
I am authoring a java library. Some of the classes that are meant to be used by library users, hold native system resources (over JNI). I'd like to ensure that the user "disposes" these objects, as ...
0
votes
1
answer
495
views
Why does finalize() execute only after new object is created, but not after gc() is invoked?
Shouldn't finalize() execute immediately when gc() is called? The order of output result is a little unconvincing.
class Test
{
int x = 100;
int y = 115;
protected void finalize()
{...
1
vote
1
answer
593
views
When is finalize() invoked during garbage collection?
From :
Q11 of https://www.baeldung.com/java-memory-management-interview-questions
When an object becomes eligible for GC, the garbage collector has to run the finalize() on it; this method is ...
1
vote
2
answers
81
views
Why finalize is required when GC can free all connections
I have an object. It opened a FileConnection.
Now, I made the reference of object null.
Then GC [Garbage-Collecter] runs.
So, GC will free the object and also close the FileConnection held by Object.
...
3
votes
2
answers
678
views
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 ...
2
votes
1
answer
139
views
Finalize method causing performance overhead [duplicate]
Why is it that implementing finalize method causes performance overhead in Java?
0
votes
1
answer
603
views
Problems with finalize method in Java [duplicate]
I've been having problems with finalize method in my last exercises. Seems like I have a deprecated method and I can't seem to find the newest version of finalize. I have two files and I'm required ...
24
votes
4
answers
6k
views
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 ...
2
votes
1
answer
1k
views
Is 'finalize()' method always called by garbage collector before destroying an 'Unreachable' object?
I red from the following link:garbage collection from geeksforgeeks
and here it is said that:
The finalize() method is never invoked more than once for any given object.
So, here it is said "more ...
0
votes
1
answer
384
views
How to Properly Handle Class Variables with Dispose/Finalize Methods
I am at a loss on how to handle a class that contains variables with Dispose/Finalize methods. I wish for this class to contain its own Dispose/Finalize methods that call upon the Dispose for each ...
1
vote
1
answer
129
views
Java Finalize method not invoking [duplicate]
This is the piece of code. The finalize() method should invoke after the System.gc() command but its not.
Any suggestions?
class test123{
test123(){
System.out.println("Inside the ...
6
votes
1
answer
1k
views
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();
}
...
2
votes
2
answers
1k
views
ExecutorService shutdown in finalize()
I've got a class which manages a cache of objects.
The cache is cleaned once an hour via ScheduledExecutorService.
Is it ok to shutdown the ExecutorService via the class finalize() method?
@Override
...
2
votes
1
answer
3k
views
Redisson: Closing client in finalize method
I'm writing an adapter for redission client to use in our application, I'm not sure if it is a good design to close the client in the finalize block. Below is the code. Please let me know
private ...
0
votes
3
answers
205
views
Why finalize is not giving null pointer exception in the below code?
Why the below code is not giving null pointer exception in the finalize method when the objects are made null??
class Person{
public int a;
public void finalize(){
//System.out.println("...