What is garbage collection? Explain how Garbage Collection is done in Java. Differentiate method overloading and method overriding.
Garbage collection in java is defined as the process, by which Java programs execute involuntary memory regulation. It automatically clears out unutilized objects and pointers in the memory, permitting the resources to be used again.
How garbage collection is done in Java?
Java garbage collection is an involuntary process where the programmer does not need to indicate objects to be deleted. However, it is important for programmers who desire to develop their skills to learn how Java garbage collection operates and how it can be configured.
Garbage collection in Java is a fully automated procedure. Objects that are to be discarded do not need to be marked directly by the programmer. The JVM contains the garbage collection code. Garbage collection can be implemented differently by each JVM. Each JVM is free to enforce garbage collection in whatever way it sees fit, as long as it adheres to the JVM specification. It can observe with the standard JVM definition of dealing with objects in mass memory, naming or defining unreachable objects, and so on.
Although there are numerous JVMs, Oracle’s Hotspot is by far the most common.
Java Garbage Collection best practices
Besides the fundamental mechanisms of it is, one of the most critical things to remember about garbage collection in Java is that it is non-deterministic it simply means that there is no way to determine when garbage collection will occur at run time. The System, gc () and Runtime. Gc () methods may be used to provide a hint in the code to operate the garbage collector, but they don’t ensure that the garbage collector would run.
Setting flags on the JVM is the easiest way to fine-tune Java garbage collection. The garbage collector to be used (e.g. Serial, G1, etc.), the heap’s original and limit sizes, the size of the heap parts (e.g. Young Generation, Old Generation), and more can all be changed using flags.
The essence of the program being tuned or adjusted is a good place to start when figuring out what settings to use. The Parallel garbage collector, for example, is quick but sometimes causes “stop the world” situations, so it’s best suited to runtime processing where lengthy garbage collection delays are appropriate.
Difference between method overloading and method overriding
|Method Overloading is an attribute that permits a category to have more than one method having an identical name if their argument lists are distinct.
|Method overriding is a language attribute that permits a subcategory or child category to come up with a particular execution of a method that is provided by one of its superclass or parent class previously.
|The execution is done within the class.
|The execution has occurred in two or multiple classes that have an inheritance.
|Method overloading is an example of compile-time polymorphism.
|Method overriding is an example of run time polymorphism.
|Parameters must be distinct in case of overloading.
|Parameters must be alike in case of overriding.
|In method overloading, the return type can be distinct but the user has to switch the parameters as well.
|In method overriding the return type must be alike.