{"id":225,"date":"2003-01-19T19:03:50","date_gmt":"2003-01-19T23:03:50","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=225"},"modified":"2003-01-19T19:03:50","modified_gmt":"2003-01-19T23:03:50","slug":"object-finalization-and-cleanup","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2003\/01\/19\/object-finalization-and-cleanup\/","title":{"rendered":"Object finalization and cleanup"},"content":{"rendered":"<p><a href=\"http:\/\/www.javaworld.com\/javaworld\/jw-06-1998\/jw-06-techniques_p.html\">Object finalization and cleanup<\/a>: How to design classes for proper object cleanup<\/p>\n<p>&#8220;&#8230;By now you may be getting the feeling that you don&#8217;t have much use for finalizers. While it is likely that most of the classes you design won&#8217;t include a finalizer, there are some reasons to use finalizers. <\/p>\n<p>One reasonable, though rare, application for a finalizer is to free memory allocated by native methods. If an object invokes a native method that allocates memory (perhaps a C function that calls malloc()), that object&#8217;s finalizer could invoke a native method that frees that memory (calls free()). In this situation, you would be using the finalizer to free up memory allocated on behalf of an object &#8212; memory that will not be automatically reclaimed by the garbage collector. <\/p>\n<p>Another, more common, use of finalizers is to provide a fallback mechanism for releasing non-memory finite resources such as file handles or sockets. As mentioned previously, you shouldn&#8217;t rely on finalizers for releasing finite non-memory resources. Instead, you should provide a method that will release the resource. But you may also wish to include a finalizer that checks to make sure the resource has already been released, and if it hasn&#8217;t, that goes ahead and releases it. Such a finalizer guards against (and hopefully will not encourage) sloppy use of your class. If a client programmer forgets to invoke the method you provided to release the resource, the finalizer will release the resource if the object is ever garbage collected.&#8221;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Object finalization and cleanup: How to design classes for proper object cleanup &#8220;&#8230;By now you may be getting the feeling that you don&#8217;t have much use for finalizers. While it is likely that most of the classes you design won&#8217;t include a finalizer, there are some reasons to use finalizers. One reasonable, though rare, application &hellip; <a href=\"https:\/\/cephas.net\/blog\/2003\/01\/19\/object-finalization-and-cleanup\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Object finalization and cleanup<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/225"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=225"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/225\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}