Garbage Collection Java Interview Questions – Set 01

What is the difference between final, finally and finalize

  • final” is the keyword to declare a constant AND prevents a class from producing subclasses.
  • finally” is a block of code that always executes when the try block is finished, unless System.exit() was called.
  • finalize()” is an method that is invoked before an object is discarded by the garbage collector.

What is final, finalize() and finally?-

final : final keyword can be used for class, method and variables. A final class cannot be subclassed and it prevents other programmers from subclassing a secure class to invoke insecure methods. A final method can’t be overridden. A final variable can’t change from its initialized value. finalize() : finalize() method is used just before an object is destroyed and can be called just prior to garbage collection. finally : finally, a key word used in exception handling, creates a block of code that will be executed after a try/catch block has completed and before the code following the try/catch block. The finally block will execute whether or not an exception is thrown. For example, if a method opens a file upon exit, then you will not want the code that closes the file to be bypassed by the exception-handling mechanism. This finally keyword is designed to address this contingency.

What is OutOfMemoryError in java? How to deal with java.lang.OutOfMemeryError error?

This Error is thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector. Note: Its an Error (extends java.lang.Error) not Exception. Two important types of OutOfMemoryError are often encountered

  • lang.OutOfMemoryError: Java heap space
    • The quick solution is to add these flags to JVM command line when Java runtime is started:
      • -Xms1024m -Xmx1024m
    • lang.OutOfMemoryError: PermGen space
      • The solution is to add these flags to JVM command line when Java runtime is started:
        • -XX:+CMSClassUnloadingEnabled
        • -XX:+CMSPermGenSweepingEnabled

Long Term Solution: Increasing the Start/Max Heap size or changing Garbage Collection options may not always be a long term solution for your Out Of Memory Error problem. Best approach is to understand the memory needs of your program and ensure it uses memory wisely and does not have leaks. You can use a Java memory profiler to determine what methods in your program are allocating large number of objects and then determine if there is a way to make sure they are no longer referenced, or to not allocate them in the first place.

Why will you use Comparator and Comparable interfaces?

java.util.Comparator
java.util.Comparator compares some other class’s instances.

java.lang.Comparable
java.lang.Comparable compares another object with itself.

Differentiate between final, finally and finalize.

The keyword is final. It is used for declaring a constant It prevents a class from producing subclasses.

finally is a code.It always executes when the try block is finished, Unless System.exit() has been called.finalize() is a method, Before discarding by the garbage collector it is invoked.

Differentiate JAR and WAR files

JAR files:

  • JAR files is the acronym stands for Java ARchive fles.
  • JAR files allow aggregating many files into one,
  • JAR is usually used to hold Java classes in a library.

WAR files:

  • WAR files is the acronym stands for Web ARchive fles.
  • WAR stores XML, java classes, and JavaServer pages
  • WAR is mainly used for Web Application purposes.

In a Java , how can you send program messages on the system console, but error messages, to a file?

The class System has a variable out that denotes the standard output.

The standard error device represents the variable err .

Naturally, they both point at the system console.

In this way, the standard output can be sent to the file:

Stream x = new Stream(new FileOutputStream(“error.txt”));

System.setErr(x);

System.Out(x);

When is an object subject to garbage collection

An object is subject to garbage collection when it becomes unreachable to the program in which it is used.

Under what conditions is an object’s finalize() method invoked by the garbage collector

The garbage collector invokes an object’s finalize() method when it detects that the object has become unreachable.

What is the purpose of finalization

The purpose of finalization is to give an unreachable object the opportunity to perform any cleanup processing before the object is garbage collected.

What is daemon thread and which method is used to create the daemon thread

Daemon thread is a low priority thread which runs intermittently in the back ground doing the garbage collection operation for the java runtime system. setDaemon method is used to create a daemon thread

What is garbage collection

The runtime system keeps track of the memory that is allocated and is able to

determine whether that memory is still useable. This work is usually done in

background by a low-priority thread that is referred to as garbage collector. When the

gc finds memory that is no longer accessible from any live thread it takes steps to

release it back to the heap for reuse

Can an object be garbage collected while it is still reachable

A reachable object cannot be garbage collected. Only unreachable objects may be garbage collected..