Page 3 of 15. public class Main{. public static void main(String[] args). {. Student s; //local variable - reference. s
Theory into Practice
class Student { int age; String name;
//instance variable //instance variable
public Student() { this.age = 0; name = "Anonymous"; } public Student(int Age, String Name) { this.age = Age; setName(Name); } public void setName(String Name) { this.name = Name; } }
public class Main{ public static void main(String[] args) { Student s; //local variable - reference s = new Student(23,"Jonh"); int noStudents = 1; //local variable } }
JAVA Main Hasta la vista Baby!
What will happen inside the JVM ???
Let’s start from beginning…. Loading
The initial attempt to execute the method main of class Main discovers that the class Main is not loaded
The virtual machine does not currently contain a binary representation for this class (class file format).
After Main is loaded, it must be initialized before main can be invoked, and a type (class or interface) must always be linked before it is initialized.
Linking is the process of taking a binary form of a class or interface type and combining it into the runtime state of the Java virtual machine, so that it can be executed. A class or interface type is always loaded before it is linked.
A runtime constant pool is a per-class or per-interface runtime representation of the constant_pool table in a class file. Each runtime constant pool is allocated from the Java virtual machine's method area.
String s1 = “jim”; String s2 = “jim”;
System.out.println(s1==s2); // true.
String s1 = “jim”; String s2 = new String(“jim”);
System.out.println(s1==s2); //false. It will allocate new Object instead getting from String constant pool.
The runtime constant pool for a class or interface is constructed when the class or interface is created When creating a class or interface, if the construction of the runtime constant pool requires more memory than can be made available in the method area of the Java virtual machine, the Java virtual machine throws an OutOfMemoryError
.
The information regarding the type is loaded from the class files when the JVM loads a class file. This information is stored in the method area. Method Area is shared among all Java virtual machine threads.
•
•
•
Before a class or interface is initialized, its direct superclass must be initialized, but interfaces implemented by the class need not be initialized. Similarly, the super interfaces of an interface need not be initialized before the interface is initialized.
A class or interface type Student will be initialized immediately before one of the following occurs: Student is a class and an instance of Student is created. Student is a class and a static method of Student is invoked. A non-constant static field of Student is used or assigned
What are the reasons for below Exceptions?
java.lang.OutOfMemoryError: PermGen space
java.lang.OutOfMemoryError: Java heap space
IOException: Too many open files
java.lang.StackOverflowError
Java runs as a single process Does not share memory with other processes Each process allocates memory We call this, process heap
1.
2.
JVM creates a Java Heap Part of the process heap Configured through –Xmx and –Xms settings Also referred to as Java Heap Often confused with JVM process heap Stores Java Objects instances of classes and the data the objects contain Primitives References
-Xmx, -Xms
Only controls the Java Object Heap
Often misunderstood to control the process heap
Confusion leads to incorrect tuning
And in some cases, the situation worsens
80-98% of newly allocated are extremely short lived (few million instructions) 80-98% die before another megabyte has been allocated
All new objects are created here Only moved to Old Gen if they survive one or more minor GC
Survivor Spaces 2, used during the GC algorithm (minor collections)
A good size for the YG is 33% of the total heap
Multithreaded apps create new objects at the same time New objects are always created in the EDEN space
During object creation, memory is locked On a multi CPU machine (threads run concurrently) there can be contention
Allow each thread to have a private piece of the EDEN space Thread Local Allocation Buffer -XX:+UseTLAB -XX:TLABSize= -XX:+ResizeTLAB (On by default on multi CPU machines and newer JDK)
Major GC happens in here.
One survivor space is always empty Serves as destination for minor collections
Objects get copied to the tenured space when the 2nd survivor space fills up
Major collections occur when the tenured space fills up
This is known as a stop-the-world collection, because basically the JVM pauses everything else until the collection is completed. Major collections free up Eden and both survivor spaces
Concurrent/Parallel Garbage Collection
-XX:+UseParNewGC Parallel GC in the New(Young) Generation
-XX:+UseConcMarkSweepGC Concurrent in the Old generation Use these two combined Multi CPU box can take advantage of this
Stores classes, methods and other meta data -XX:PermSize= (initial) -XX:MaxPermSize= (max)
Permanent Space Memory Errors Too many classes loaded, Classes are not being GC:ed Unaffected by –Xmx flag Identified by java.lang.OutOfMemoryError: PermGen space
Many situations, increasing max perm size will help i.e., no leak, but just not enough memory Others will require to fix the leak