Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

finally Vs finalize  RSS feed

 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks both does releasing memory/non-memory resources. Which is the right option for releasing memory intensive resources ?
 
Stephan van Hulst
Saloon Keeper
Posts: 7808
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally and finalize are two completely different mechanisms.

The latter is intended as a safety net to release resources held by the object, if they weren't explicitly released by the client. You should never call finalize(); and you rarely have a good reason to override it.

Finally on the other hand, can be used to release resources held locally by a method. Always use finally to release resources.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!