clean-up resources

Posted on 2000-05-11
Last Modified: 2010-04-09
What is the best way to clean-up after an object that uses ResultSet,Connection and Statement objects?
I wrote close() method which closes these objects (by using their close method:Connection.close(),ResultSet.close() etc,
but I have to put all in a try block so what happens if an exception is thrown?
Another issue - what if  the class which uses those objects is the main class
and no one calls its close() method?
Question by:twg
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
  • 3
  • 3
LVL 22

Expert Comment

ID: 2799859
You use ASP, right, then you should set your objects to nothing after you've closed them.always nothing your objects, that's the way to clean up memory

Conn = nothing

Author Comment

ID: 2799879
thanks CJ S but I don't use ASP.
Anyway, the problems still stay open(if an exception is thrown, and how can I garantee clean-up for the main class
LVL 22

Expert Comment

ID: 2799907
then what do you use, VBscript? That's also nothing

javascript is null

object = null
object = nothing
asp = vbscript:
object = nothing

that'll ganarantue the clean up.
Technology Partners: 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!


Author Comment

ID: 2799966
I wrote my application in Java and it uses the java.sql package. I'm sorry that I didn't write my question in the right section (java programming) so can you pass my question to the relevant people?
LVL 22

Accepted Solution

CJ_S earned 40 total points
ID: 2800022
totally depends on how you have set your program up. One should use as little gloabl variables as you can. This means that there are no variables outside your functions. If the variables are all inside your functions, they'll get deleted right away when they loose their scope.
If you used the new operator, then you should use the free-method.
Java usesit's own handles, everything is inherited from it's parent, there's no need for you to free them. Just closing _should_ be enough for normal stuff.

Author Comment

ID: 2800114
I wrote my application in Java and it uses the java.sql package. I'm sorry that I didn't write my question in the right section (java programming) so can you pass my question to the relevant people?

Expert Comment

ID: 2800121

Use the finally clause....

catch(Exeption e){
   // handle exception
  // can clean up stuff here

If an exception occurs in the try block, and there is an associated catch block to handle the exception , control transfers to the catch block and then to the finally block

If control reaches the end of the try , it then proceeds to the finally block.

You should really make sure that the close is called. But you can use the finalize()method. This method is called by the garbage collector calls before it collects the objects in memory. But there is a problem, you have no gaurantee that any objects will be garbage collected...

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

717 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