What is the use of Garbage Collection in Java?

Asked By 200 points N/A Posted on -
qa-featured

Hi Experts of techyv

How does the garbage collection work in Java?

Thanks in advance

 

SHARE
Best Answer by chetan
Answered By 0 points N/A #86787

What is the use of Garbage Collection in Java?

qa-featured

There are two stages to garbage collection.

1. Marking: The garbage collector traverses the application graph, starting from the root objects. Each object that the garbage collector encounters is marked as used and will not be deleted in the next stage.

2. Sweeping: The objects that were marked in the Marking stage are deleted here. There are multiple algorithms for deleting depending on the size and fragmentation of the memory.

In any application some objects are short-lived and some are long-lived. Garbage collection can take advantage of this information and split the memory space in to two sections: young generation and old generation. The objects that are short-lived are place in young generation and long-lived once are placed in old generation. This approach is called Generational garbage collection.

 

Answered By 200 points N/A #86789

What is the use of Garbage Collection in Java?

qa-featured

Thanks for that information. I also wanted to know what are the various jvm arguments available for garbage collection?

Best Answer
Best Answer
Answered By 0 points N/A #86790

What is the use of Garbage Collection in Java?

qa-featured

Here are some of the most commonly used GC params to the JVM:

-XX:-DisableExplicitGC       Disable calls to System.gc(), JVM still performs garbage collection when necessary.

-XX:-UseParallelGC           Use parallel garbage collection for scavenges.

-XX:-UseSerialGC              Use serial garbage collection. (Introduced in 5.0).

-XX:-PrintGC                      Print messages at garbage collection.

-XX:-ParallelGCThreads    Sets the number of garbage collection of threads in the young and old parallel garbage collectors. The default value varies with the platform on which the JVM is running.

Related Questions