File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Performance and the fly likes Garbage collection doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "Garbage collection doubt" Watch "Garbage collection doubt" New topic

Garbage collection doubt

vijaya raj

Joined: Dec 26, 2010
Posts: 7
Dear All,
I have created many objects in my program.

My doubt is, when will the GC occur? Is it during program execution?
What happens to the objects if i close the application forcefully( Not a graceful death)?

Please clear my doubts.....

William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13027
1, GC will occur whenever the JVM decides to run it. Generally speaking you don't need to worry about it.

2. Forcing a shutdown will of course destroy all the objects - depending on the resources being used this could create some problems. Thats why orderly shutdown is preferred.

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11923

GC recovers memory to the JVM. it will run when the JVM thinks it needs to. if your program dies, all that memory is restored to the operating system.

You can think of memory as an office building. The OS owns the whole thing. The JVM is a tenant, who may rent a couple of floors. The OS doesn't care how the tenant uses the memory, it just knows that nobody else can have floor 3-5.

Then, the JVM decides how to divide up the floors it owns. as people start working there (i.e. object created), the tenant/jvm decides what cube they should go in. Some people will quit the company. eventually, they may need to re-arrange everyone on the floors. None of this matter to the OS.

When the JVM 'breaks its lease', all the memory is returned to the OS/landlord, who can then rent it to someone else.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
vijaya raj

Joined: Dec 26, 2010
Posts: 7
Thanks a lot for the clear and precise explanation.
That clears all my doubts

Good Day.
I agree. Here's the link:
subject: Garbage collection doubt
It's not a secret anymore!