JVM Internals Flashcards

1
Q

What is JRE composed of?

A

Java API and JVM

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is role of JVM?

A

To read the Java application through the class loader and execute it along with the Java API

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Virtual Machine (VM) in JVM?

A

A virual machine is a software implementation of a machine that executes program like a physical machine. Basically an abstraction on top of machine.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Is JVM hardware dependent?

A

Yes, JVM is hardware dependent portion. It executes the Java bytecode spit out by compiler. Java execution code does not need to change with changing hardware.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

JVM is register based or stack based?

A

Even though most popular computer architectures like x86 or ARM run on register based implementation, JVM runs based on stack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Platform independence and primitive data type

A

C/C++ have different int type size based on platform. JVM clearly defines primitives to maintain compatibility & guarantee platform independence.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Is JVM little endian or big endian?

A

Java class file uses network byte order which is big endian. This is done to maintain platform independence between Intel x86 uses little and RISC uses big endian.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Are there multiple JVM implementations?

A

Yes, any vendor can develop and provide JVM. JRockit, Hotspot, Azul, IBM, OpenJDK etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is Java Bytecode?

A

Middle language between Java (user language) and the machine language. This is what helps in achieving WORA.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is javap?

A

javap is disassembler. The result of javap is called Java assembly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Which is the opcode to invoke a method in java?

A

invokevirtual is the OpCode used to run method in Java bytecode. 4 OpCodes are invokeinterface, invokespecial, invokestatic, invokevirtual.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

4 OpCodes used to invoke method

A
invokeinterface - invokes an interface method
invokespecial - invokes an initializer, private method or superclass method
invokestatic - invokes static methods
invokevirutal - invokes instance methods
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Show sample bytecode

A
public void add(java.lang.String);
Code:
 0: aload_0
 1: getfield #15;
 4: aload_1
 5: invokevirtual #23;
 8: pop
 9: return
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does number in front of the code mean? Ex

0: aload_0
1: getfield #15;
4: aload_1

A

It is the byte number. This is the reason why code executed by JVM is called “Byte code”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How many bytes is OpCode size?

A

1 byte. Therefore maximum number of Java Bytecode instruction OpCodes is 256.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the maximum size of java method?

A

65536 bytes. It is because the branch/jump instructions used in Byte code take 2 byte argument. So maximum value that can be expressed is 65535.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What happens if you run code compiled with higher version compiler and executed in lower version runtime?

A

You get runtime exception called java.lang.UnsupportedClassVersionError

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What happens if you run code compiled in lower version compiler and run in higher version runtime?

A

It just runs! Java maintains backward compatibility.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

JVM structure?

A

Java code (.java) is compiled using javac and the output is (.class). Then at execution time using java command, Classloader loads the classes in the Runtime data areas and executes the code using the execution engine.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Which classloader is parent of all class loaders?

A

Bootstrap class loader is parent of all classloaders

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How classloading delegation works?

A

During class loading, first parent class loader is checked. If parent loads the class, it is used and if not the current classloader loads the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Visibility limit of class in classloader?

A

A child class loader can find the class in parent class loader but parent cannot find class in child classloader.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Can class be unloaded once loaded?

A

No. Instead the classloader can be deleted and a new one can be created.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Can two .class files of same java code be different?

A

Yes. If they are loaded with two different classloaders. Classloaders has its namespace that stores the loaded classes.

25
Q

Show hierarchy of classloaders

A
Bootstrap classloader
   ^
   |
Extension classloader
   ^
   |
System classloader
   ^
   |
------------
User defined classloader
   ^
   |
User defined classloader
26
Q

What is Bootstrap classloader?

A

This is created when running the JVM. It loads java api, including Object classes. It is implemented in native code instead of Java.

27
Q

What is Extension class loader?

A

It loads the extension classes excluding basic java apis. It also loads various security extension functions.

28
Q

What is system class loader?

A

It loads application classes. It loads class in $CLASSPATH specified by user.

29
Q

What is User defined classloader?

A

Classloader that a user creates using code is User defined classloader.

30
Q

What are the steps in classloading?

A
1 - Loading
2 - Verifying
3 - Preparing
4 - Resolving
5 - Initializing
31
Q

Define Loading stage of classloading?

A

In this stage, the class is read from the file and loaded into JVM memory.

32
Q

Define Verification stage of classloading?

A

In this stage, checks whether or not the class is configured as per JLS and JVM specifications. This is the most complicated test process of class loading process.

33
Q

Define prepare stage of classloading?

A

Prepare data structure that assigns the memory required by classes and indicates the fields, methods and interfaces defined in the class.

34
Q

Define resolving stage of classloading?

A

Change all symbolic references in the constant pool of the class to direct references.

35
Q

Define Initializing stage of classloading?

A

Initialize class variables to proper values. Execute static initializers and initialize static fields to configured values.

36
Q

Which three stages combined are called Linking?

A

Verification, Preparing and Resolving. Combined together are called Linking.

37
Q

Which are thread specific memory that JVM allocates?

A

PC register, Call Stack, Native method stack.

38
Q

Which memory areas are shared across all threads?

A

Heap, Method area, Constant pool

39
Q

What does method area contain?

A

Shared area, which stores runtime constant pool, field and method information, static variables and method bytecode for each of the classes and interfaces read by the JVM.

40
Q

What is method area called in Hotspot JVM?

A

Method area is called PermGen (Permanent Generation) in Hotspot JVM. The garbage collection is optional for JVM vendor.

41
Q

What is Runtime constant pool?

A

It plays important role in JVM operation. It is part of method area. When a method or field is referred to, JVM searches the actual address of method or field on memory by using the runtime constant pool.

42
Q

How execution engine runs bytecode?

A

Engine needs to convert bytecode into instructions that machine can understand. There are two ways - 1) Interpreter 2) JIT

43
Q

How interpreter executes bytecode?

A

Interpreter reads each instruction line one by one. Interpreter can quicky interpret one bytecode but slowly executes the interpreted result. That is disadvantage of interpreted language.

44
Q

How JIT compiler helps?

A

Compensate for disadvantages of Interpreter. After a method is used many times, JIT compiles the code to native code. So it eliminates the need to interpret. But it is slow process so only hotspot methods are compiled down to native code.

45
Q

Is compiled JITed native code also rolled back?

A

Yes, if a method is no longer a hotspot it is removed from cache and again interpreted.

46
Q

What does -server flag mean when starting JVM?

A

There are two flavours of JVM, Server VM and Client VM. The optimization level in both is different and both use different JIT compilers.

47
Q

What was the biggest change made in Java SE 7 in JVM?

A

A new instruction OpCode was added called “invokedynamic” to support new dynamically typed languages.

48
Q
public class A {
  public void doSomething() {
  B b = new B();
  b.doSomethingElse();
}
}
What is the line B b = new B() semantically equivalent to in terms of classloading?
A
It is equivalent to
B b = A.class.getClassLoader().loadClass("B").newInstance();
49
Q

Which is the default classloader of all classloaders?

A

It is System ClassLoader, the one which loads class from $CLASSPATH.

50
Q

Which classloader has no parent?

A

Its Bootstrap classloader. It is parent of all classloaders and does not have a parent. It is written in native code.

51
Q

Do web applications J2EE applications, follow same ordering in classloading?

A

It is not defnied. They also follow reversed order of classloading. Different containers can have different behaviors.

52
Q

What is NoClassDefFoundError?

A

NoClassDefFoundError is thrown if class loader tries to load the definition of class and none is found. Which means that the definition existed when the class was compiled but could not be found at runtime. So we cannot fully rely on IDE telling us problem.

53
Q

When does classloading happen?

A

Classloading happens at runtime and not compile time.

54
Q

Which are ways to see classpath of running application?

A

1 - Print it with code by casting classloader to URLClassLoader and use #getURLs() method and print it.
2 - Use JConsole and view the classpath MBean property.

55
Q

When does NoSuchMethodError occur?

A

The class that is being referred is present but incorrect verision is loaded at runtime, hence the required method is not found.

56
Q
class A {
 public void test() { ((Util)Factory.getUtil()).sayHello();
}
}
class Factory { 
  public static Object getUtil() {
    return new Util();
  } 
}
When can this code throw ClassCastException?
A

This can happen if two different Util class were loaded from different class contexts. Solution is to figure out the class path problem.

57
Q

How is a class identified in context of classloading?

A

A class is identified by combination of classloader and FQDN. It means same class loaded from two different classloaders will not be equal.

58
Q

Which is the reason why classcastexception occurs for same class?

A

It happens when different versions of the same library is bundled in different places. Typically happens with de facto libraries like log4j, hibernate etc.