Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


public class Garbage
{
int a=0;
public void add()
{
int c=10+20;
System.out.println(c);
System.out.println(a);
}
public static void main(String args[])
{
Garbage obj=new Garbage();
System.gc();
System.out.println("Garbage Collected");
obj.add();
}
}
Above is a code in java used for garbage collection. object
obj has been created for the class Garbage and system.gc
method is called. Then using that object add method is
called.System.gc method if called the obj should be garbage
collected?

Answers were Sorted based on User's Feedback



public class Garbage { int a=0; public void add() { int c=10+20; System.out.prin..

Answer / namita

First We cannot force the garbage collection to garbage the
object. Garbage collection can never be forced.

So by calling System.gc() will not ensure you that the
object will be garbage collected.

Is This Answer Correct ?    2 Yes 0 No

public class Garbage { int a=0; public void add() { int c=10+20; System.out.prin..

Answer / madhu

Garbage Collector is called by the JVM. and it will collect
the objects which has no reference, in the above case obj
has reachability,so JVM won't force the garbage collector to
collect the obj object.

Is This Answer Correct ?    2 Yes 0 No

public class Garbage { int a=0; public void add() { int c=10+20; System.out.prin..

Answer / namita

Maverickhari,

Garbage collector is system created thread which runs
automatically.

We are not sure when the garbage collection is going to
happen. this totally depends upon the JVM. Like connection
pool all the the objects are created in pool JVM will check
if there is no free memory in pool then it searches for the
objects which are no longer in use and will garbage collect
that and allocate to some other object.

hope this will clear

Is This Answer Correct ?    1 Yes 0 No

public class Garbage { int a=0; public void add() { int c=10+20; System.out.prin..

Answer / maverickhari

say me when this eill actually happen.

Is This Answer Correct ?    0 Yes 0 No

public class Garbage { int a=0; public void add() { int c=10+20; System.out.prin..

Answer / maverickhari

Thanks Namita

Is This Answer Correct ?    0 Yes 0 No

public class Garbage { int a=0; public void add() { int c=10+20; System.out.prin..

Answer / bhudeep

Java provides us one method System.gc()to call garbage
collection forcefully.But by calling System.gc() will not
ensure you that the object will be garbage collected.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Core Java Interview Questions

What is used of static keyword in java?

0 Answers   Cyient,


Which list does not allow duplicates in java?

0 Answers  


Can I import same package/class twice?

0 Answers  


How to use string tokenizer class.

0 Answers  


What is the generic function?

0 Answers  


What is tostring () method?

0 Answers  


What is meant by oops concept in java?

0 Answers  


What is hash method?

1 Answers  


WHAT IS MARKER INTERFACE,AND NO METHODS IS NOT THERE ON THAT INTERFACE THEN WHY WE USED IN JAVA?

3 Answers   L&T,


What is arraylist class in java?

0 Answers  


what is difference between perfom() & excute() ?

2 Answers   IBM,


why string is not taking as primitive datatypes but we r taking it as a class? why explain it?

2 Answers   HP, SparkTG,


Categories