• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Reg finalize() method

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please could you tell me how many times finalize() method would be called of this class :



1.  Exactly one time
2.  One or two times
3.  Exactly Two times
4.  Can't say

When I run this code, it calls finalize() exactly one time as it gets called from the constructor (although it's incorrect to do so). However, can this method be called again before the garbage collection happens (in this case)? If so, what would be the correct answer. Thanks in advance.
 
Marshal
Posts: 69806
277
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instance methods are never called on a class; they are called on objects. There is no guarantee that the GC process will run at all; if it does run it may not release a particular object, so the number of times finalize() runs is unpredictable. You would not have finalize() run 3× or more.
Where did you get that code from? It is the sort of thing that, if written at work, would guarantee you are looking for a new job 
 
Saloon Keeper
Posts: 12141
258
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is 2. "One or two times", though it almost always will be run just once. It will be run once definitely because you call it from the constructor. It may be called a second time because the object becomes unreachable in line 10, and therefore eligible for garbage collection. A hypothetical JVM that has a very aggressive garbage collector could run the finalizer as soon as the object becomes eligible.
 
Campbell Ritchie
Marshal
Posts: 69806
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can persuade the finalize() method to run by forcing garbage collection, as I did with your code by filling up the available space.

[critchie@...]$ java Fizz
def child con
in finalize
in finalize

I added lines 11‑14, which makes your code even worse than it was before
 
Neha Agnihotri
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Instance methods are never called on a class; they are called on objects. There is no guarantee that the GC process will run at all; if it does run it may not release a particular object, so the number of times finalize() runs is unpredictable. You would not have finalize() run 3× or more.
Where did you get that code from? It is the sort of thing that, if written at work, would guarantee you are looking for a new job 



Thanks for the explanation. I'm these days randomly doing such stuff and in turn trying to get a better understanding how things work in Java. Don't worry, it's not written at work as I'm a new mom and on a break doing weird stuff at home.
 
Neha Agnihotri
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You can persuade the finalize() method to run by forcing garbage collection. . . .



This was interesting :-)
 
Destroy anything that stands in your way. Except this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic