JVM Management Interview Questions Part I

Different types of memory used by JVM ?
  • Class.

  • Heap.

  • Stack.

  • Register.

  • Native Method Stack.

When are static variables loaded in memory ?

They are loaded at runtime when the respective Class is loaded.

Does garbage collection guarantee that a program will not run out of memory?

Garbage collection does not guarantee that a program will not run out of memory. It is possible for programs to use up memory resources faster than they are garbage collected. It is also possible for programs to create objects that are not subject to garbage collection.

What is a String Pool ?

String pool (String intern pool) is a special storage area in Java heap. When a string is created and if the string already exists in the pool, the reference of the existing string will be returned, instead of creating a new object and returning its reference.

Learn in Detail

Which are the different segments of memory ?
  • Stack Segment -It contains local variables and Reference variables(variables that hold the address of an object in the heap).

  • Heap Segment -It contains all created objects in runtime, objects only plus their object attributes (instance variables).

  • Code Segment - The segment where the actual compiled Java bytecodes resides when loaded.

Describe what happens when an object is created in Java ?
  1. Memory is allocated from heap to hold all instance variables and implementation-specific data of the object and its superclasses. implementation-specific data includes pointers to class and method data.

  2. The instance variables of the objects are initialized to their default values.

  3. The constructor for the most derived class is invoked. The first thing a constructor does is call the constructor for its superclasses. This process continues until the constructor for java.lang.Object is called, as java.lang.Object is the base class for all objects in java.

  4. Before the body of the constructor is executed, all instance variable initializers and initialization blocks are executed. Then the body of the constructor is executed. Thus, the constructor for the base class completes first and constructor for the most derived class completes last.

How many objects are created with this code :- String s = new String("abc");

Two objects will be created here. One object creates memory in heap with new operator and second in stack constant pool with "abc".

Describe, in general, how java's garbage collector works ?
  • The Java runtime environment deletes objects when it determines that they are no longer being used. This process is known as garbage collection.

  • The Java runtime environment supports a garbage collector that periodically frees the memory used by objects that are no longer needed.

  • The Java garbage collector is a mark-sweep garbage collector that scans Java's dynamic memory areas for objects, marking those that are referenced. After all possible paths to objects are investigated, those objects that are not marked (i.e. are not referenced) are known to be garbage and are collected.

Can I import same package/class twice? Will the JVM load the package twice at runtime?

One can import the same package or same class multiple times. Neither compiler nor JVM complains wil complain about it. And the JVM will internally load the class only once no matter how many times you import the same class.

What is a class loader ? What are the different class loaders used by JVM ?

Class loader is part of JVM which is used to load classes and interfaces.

Class loaders used by JVM are:

  • Bootstrap

  • Extension

  • System

Explain java.lang.OutOfMemoryError ?

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.

Should we override finalize method ?

Finalize is used by Java for Garbage collection. It should not be done as we should leave the Garbage Collection to Java itself.

Difference between loadClass and Class.forName ?

loadClass only loads the class but doesn't initialize the object whereas Class.forName initialize the object after loading it.

Which kind of memory is used for storing object member variables and function local variables ?

Local variables are stored in stack whereas object variables are stored in heap.

Why do member variables have default values whereas local variables don't have any default value ?

Member variable are loaded into heap, so they are initialized with default values when an instance of a class is created. In case of local variables, they are stored in stack until they are being used.

What are various types of Class loaders used by JVM ?
  • Bootstrap - Loads JDK internal classes, java.* packages.

  • Extensions - Loads jar files from JDK extensions directory - usually lib/ext directory of the JRE

  • System - Loads classes from system classpath.

How are classes loaded by JVM ?

Class loaders are hierarchical. The very first class is specially loaded with the help of static main() method declared in your class. All the subsequently loaded classes are loaded by the classes, which are already loaded and running.

Which memory areas does instance and static variables use ?

Instance variables are stored on stack whereas static variables are stored on heap.

What is PermGen or Permanent Generation ?

The memory pool containing all the reflective data of the java virtual machine itself, such as class and method objects. With Java VMs that use class data sharing, this generation is divided into read-only and read-write areas.

The Permanent generation contains metadata required by the JVM to describe the classes and methods used in the application. The permanent generation is populated by the JVM at runtime based on classes in use by the application. In addition, Java SE library classes and methods may be stored here.

What is metaspace ?

The Permanent Generation (PermGen) space has completely been removed and is kind of replaced by a new space called Metaspace.

The consequences of the PermGen removal is that obviously the PermSize and MaxPermSize JVM arguments are ignored and you will never get a java.lang.OutOfMemoryError: PermGen error.

Why Java don't use pointers ?

Pointers are vulnerable and slight carelessness in their use may result in memory problems and hence Java intrinsically manage their use.

Learn about Java in Detail

Difference between static vs dynamic class loading?

Static loading - Classes are statically loaded with Java’s “new” operator.

Dynamic class loading - Dynamic loading is a technique for programmatically invoking the functions of a class loader at run time.

Class.forName (Test className);
What are the disadvantages of using arrays ?
  • Arrays are of fixed size and have to reserve memory prior to use. Hence if we don't know size in advance arrays are not recommended to use.

  • Arrays can store only homogeneous elements.

  • Arrays store its values in contentious memory location. Not suitable if the content is too large and needs to be distributed in memory.

  • There is no underlying data structure for arrays and no ready made method support for arrays, for every requirement we need to code explicitly.

Can we call the garbage collector explicitly ?

Yes, We can call garbage collector of JVM to delete any unused variables and unreferenced objects from memory using gc( ) method.

This gc( ) method appears in both Runtime and System classes of java.lang package.

What are different ways to create String Object? Explain.

When we create a String using double quotes, JVM looks in the String pool to find if any other String is stored with same value. If found, it just returns the reference to that String object else it creates a new String object with given value and stores it in the String pool.

When we use new operator, JVM creates the String object but don’t store it into the String Pool. We can use intern() method to store the String object into String pool or return the reference if there is already a String with equal value present in the pool.

How substring() method of String class create memory leaks?

Substring method would build a new String object keeping a reference to the whole char array, to avoid copying it. Hence you can inadvertently keep a reference to a very big character array with just a one character string.

What are strong, soft, weak and phantom references in Java ?
  • Garbage Collector won’t remove a strong reference.

  • A soft reference will only get removed if memory is low.

  • A weak reference will get removed on the next garbage collection cycle.

  • A phantom reference will be finalized but the memory will not be reclaimed. Can be useful when you want to be notified that an object is about to be collected.