What is garbage collection in Java, and how can it be used ?

Answer Posted / surinder mehra

Garbage Collection for an object occurs in following cases:

1) All references of that object explicitly set to null
e.g. object = null
2) Object is created inside a block and reference goes out
scope once control exit that block.
3) Parent object set to null, if an object holds reference
of another object and when you set container object's
reference null, child or contained object automatically
becomes eligible for garbage collection.

Garbage Collectors are implicitly invoked by JVMs when
needed(when there is no enough space for coming objects to
store). or jvm can call garbage collectors whenever it
finds that object is not being used for long time. It is
marked as garbage and later on collected

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What does i ++ mean in Java?

503


How to declare an arraylist in java?

482


What is static block?

593


How many ways can you break a singleton class in java?

490


Howto get an object that will perform date & time calculations then format it for output in some different locales with different date style.can ne1 tel me the answer of this question.pls

1449






What is linked hashmap and its features?

548


What is an example of a keyword?

546


Does every java program need a main?

538


What is protected access modifier?

579


Why vector is used in java?

562


How do you sort an array in java?

543


What is java oops?

558


Can you create an object of an abstract class?

555


Explain thread in java?

664


Why are the objects immutable in java?

548