Java does not support destructors, but adds a finalize( ) method which are invoked by the garbage collector prior to reclaiming the memory hold by the object which has the finalize( ) method. you do not know when the objects are going to be finalized. Try Avoiding use of finalize( ) method to release non-memory resources like file handles, sockets, database connections because the platform has only a finite number of these resources, and you do not know when the garbage collection is going to invoked in to release these resources through the finalize( ) method.
C++ requires explicit memory management while Java has automatic garbage collection.
plain Java objects which are "memory resouces" because all they require is a little bit of memory to store their variables contents.
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
From novice to tech pro — start learning today.
You don't NEED to write this method, but you can if you need to perform some cleanup before the object gets garbage collected. But as the quote that you posted above alludes to, you generally don't need to write a finalize method, because a) resources such as sockets, files, database connections, etc should be released by your code well before a finalize() method would ever get called, and then b) the automatic garbage collector in Java automatically handles the cleanup of everything else. So there is generally nothing to do in a finalize() method.
Those are considered as "non-memory resources" because they are things that are managed by the operating system, rather than say plain Java objects which are "memory resouces" because all they require is a little bit of memory to store their variables contents.
The distinction between "non-memory" and "memory" resources is made here because generally the limits on the maximum numbers of these are vastly different. For example, if we are talking about database connections, these are generally managed by a connection pool and the maximum number of connections that are available for use might be set at say 300, whereas you might have 4GB of ram in your computer. Therefore, if you don't manage your resources properly, you are more likely to hit the limit for maximum database connections before you might hit the limit of maximum memory used.