Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why Donot finalize method run?

 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In above code the finalize method donot run
i am not able to understand why.

Thanks
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have implemented finalize in your Test class. You do not create an instance of your Test class. So no instance of the Test class needs to be garbage collected (or rather, is eligible for garbage collection)
 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But there are Integer objects inside for loop which should be garbage collected.
 
Awais Sheikh
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Finalize method is called when object of a class is created and completes its work, but remember finalize method should in the class whose garbage it has to be collect.
In your code you implemented in the test class, but no where object of Test class is created so this finalize method will not called by gc. new Interger is calling finalize method of Integer class not Test class. Try the code below hope it will clear you.



Thanks
 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Awais Sheikh
Now i understood it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic