Destroying Objects Explicitly

When an Object will get removed physically from memory ?

(a) Only When an application is closed

(b) or eventhough the application is closed, the object will get removed only after a period of time by Garbage Collector

Ex: Using Connection Object

Connection con = DriverManager.getConnection(...........)

....

......

con.close();

// Here i want to delete the Con Object...

// delete con.. (So that it gets removed from memory)

// I dont have a destructor

How to delete con object ? will get deleted automatically once application is closed ?

[641 byte] By [PrabhuSai] at [2007-9-30 11:08:17]
# 1
The OS will free any system resources held by an exiting process: memory, open files, sockets, locks, semaphores, etc.
BIJ001 at 2007-7-3 22:51:48 > top of Java-index,Administration Tools,Sun Connection...
# 2
Explicitly assign null to the object's reference when u want to delete it. This will improve the chances of object being garbage collected.
taruniiitb at 2007-7-3 22:51:48 > top of Java-index,Administration Tools,Sun Connection...
# 3

1) the object will get garbage collected if it is null and no other object is referencing this object.

2) when u close the application, the OS will free all the resources occupied by the JVM, and end the process. Thus there is no question of Garbage Collection at this time. Although the chance that the Garbage Collector will be called is rare, even if it is called simultaneously while closing the application, the process is ended, thus GC will also end.

Regards,

Rohan Kamat

Rohan__Kamat at 2007-7-3 22:51:48 > top of Java-index,Administration Tools,Sun Connection...
# 4
nice help! Thank you! dan http://www.jobmarktplatz.ch/index.htm
blandonnet at 2007-7-3 22:51:48 > top of Java-index,Administration Tools,Sun Connection...
# 5
You are saying the GC doesn't run on a set schedule but only executes if it is needed? If so, what is listening or checking if it is needed? Or, overall, what is the life cycle of the GC?Thanks,Brett
brettr15 at 2007-7-3 22:51:48 > top of Java-index,Administration Tools,Sun Connection...
# 6

> You are saying the GC doesn't run on a set schedule

> but only executes if it is needed?

It doesn't say that at all in any of the above.

There are a number of reasons why the gc runs. There is a number of different ways in which it runs.

> If so, what is

> listening or checking if it is needed? Or, overall,

> what is the life cycle of the GC?

The following link should provide some info on what it does....

http://java.sun.com/developer/technicalArticles/Networking/HotSpot/index.html

jschell at 2007-7-3 22:51:48 > top of Java-index,Administration Tools,Sun Connection...
# 7

As mentioned Earlier:

1> You should assign the object ref var to "null"

2> No other object or reference using that object that is the pre-requisite of the 1st one

then you can call

a> System.gc()

or

b> Runtime.gc()

by calling these methods even didn't run the garbage collector bocz it is a low priority thread means it will be executed if and only if all other high priority threads have done their execution.

malik_adnan at 2007-7-3 22:51:48 > top of Java-index,Administration Tools,Sun Connection...
# 8

> by calling these methods even didn't run the garbage collector bocz it is a low priority thread means it

> will be executed if and only if all other high priority threads have done their execution.

That's not the reason that it doesn't always run when you call System.gc(). The reason is does not always run is because that's how it's coded - this is an "advisory" call, and the actual garbage collector will only run if other memory pressure criteria are met (in which case it will run even if there are lots of other high-priority threads running).

shankar.unni at 2007-7-3 22:51:48 > top of Java-index,Administration Tools,Sun Connection...
# 9

I don't see where System.gc was mentioned previously but the previous two posts are incorrect.

Calling System.gc() does do something in the Sun VM. At least in versions 1.3 and 1.4.

It might not do what you expect but it does do something (and it doesn't do it on seperate thread.)

jschell at 2007-7-3 22:51:48 > top of Java-index,Administration Tools,Sun Connection...