Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage collection doubt

 
vijaya raj
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 13055
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Bill
 
fred rosenberger
lowercase baba
Bartender
Posts: 12085
29
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
vijaya raj
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the clear and precise explanation.
That clears all my doubts

Good Day.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic