I was told that we should not implement the finalize cleanup process of Java, but why is that so?
Also, is it possible to do something similar without using finalize?
It was written very well in the comments, so I will summarize it myself.
You don't know when finalize will be called, or you may not be called.Because it also depends on the implementation of the JVM, the portability of the code is lost.Also, the finalize is executed on the finalize thread, but an exception here does not result in an error, and the object may remain in the memory in a half-finished state.
Therefore, you should not write about any process that relies on such unstable processing.
There are various blogs and articles, but the most detailed one is this page of JPCert.
https://www.jpcert.or.jp/java-rules/met12-j.html
What I'm particularly concerned about is
…and many other shortcomings, strongly stating that it should not be implemented independently.
I can't say it's an alternative to finalyze, but do you want to open up the resources you've acquired (don't leave them alone, don't forget to handle exceptions)?
547 PHP ssh2_scp_send fails to send files as intended
546 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
529 Uncaught (inpromise) Error on Electron: An object could not be cloned
546 Understanding How to Configure Google API Key
689 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
© 2024 OneMinuteCode. All rights reserved.