Solved

garbage collector in java and finalize method

Posted on 2014-03-25
2
1,217 Views
Last Modified: 2014-04-16
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.

I was reading as above about garbage collector and finalize method as above.

I am not clear on who has finalize method. Do we need to write it?

Also why sockets database connections are non memory resources. Please advise
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 39954719
I am not clear on who has finalize method. Do we need to write it?
If you are writing a class, then you can write a finalize method and the JVM/garbage collector will call that method on your class before the object gets cleaned up/released.

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.

Also why sockets database connections are non memory resources
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.
0
 
LVL 7

Author Comment

by:gudii9
ID: 39995074
plain Java objects which are "memory resouces" because all they require is a little bit of memory to store their variables contents.

How and where to find how much my plain java object consumed(hopefully this is not permanent consumption right..when the memory is released?..if i run a tomcat server to deploy my webapplication war file with bunch of java objects does server memory is consumed or my laptop 4gb) out of 4GB of RAM. please advise
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Select statement in @Query (JPA Repository) - SpringMVC 2 52
learn programming 8 92
What is the use of Forwarding Class in java 1 51
JDBC performance 8 33
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question