Reputation: 1898
Why this should not be done? As finalizers will be called when garbage collection is executed, why can't we add persistent related code here?
Upvotes: 0
Views: 63
Reputation: 2111
As the book explains
Not only does the JLS provide no guarantee that finalizers will get executed promptly, it provides no guarantee that they'll get executed at all. It is entirely possible, even likely, that a program terminates without executing finalizers on some objects that are no longer reachable.
Upvotes: 5
Reputation: 15175
It says critical(!) persistent data. It is ok to have finalizers who will write away persistent data but it must be ok if this data is not written.
Since it is not ok to be missing critical data, it should not go into a finalizer.
But how can a finalizer not be called when the GC has to collect everything? Well for once the application could just be terminated forcefully, thus never leaving a chance for the GC to run.
Upvotes: 2