-Xss
Showing posts with label JVM. Show all posts
Showing posts with label JVM. Show all posts
Monday, 2 May 2016
Which JVM parameter is used to control stack size of thread?
-Xss
Can we execute the java program after calling System.exit(0) ?
Yes, By adding shutdown hook
void addShutdownHook(Thread hook) registers a new JVM shutdown hook.
When JVM shuts down ?
1. Normal exit
* When the last non-daemon thread exits
* When System.exit() invoked
2. Interrupted by User
* When pressing ^C
* System-wide event like, Shutdown, Reboot, Log off
Why Shutdown hook is useful ?
Shutdown hook can be used to :
- Perform resource cleanup like, closing log file, sending some alerts
- Save the state when JVM shuts down
Implementation of Shutdown hook
A shutdown hook is an initialized but un-started thread.
Once the shutdown sequence has begun,
- It can be stopped only by invoking the halt method, which forcibly terminates the virtual machine.
- It is impossible to register a new shutdown hook or de-register a previously-registered hook, Otherwise it will cause an IllegalStateException.
Example
Defining Shutdown hook as a thread
class ShutDownHook extends Thread {
public void run(){
System.out.println("Shutdown hook tasks - Cleanup, Save state...");
}
}
Inside main method, Add the shutdown hook / thread
Runtime runtime = Runtime.getRuntime();
runtime.addShutdownHook(new ShutDownHook());
Example (same using anonymous class)
Runtime runtime = Runtime.getRuntime();
runtime.addShutdownHook(new Thread() {
public void run(){
System.out.println("Shutdown hook tasks - Cleanup, Save state...");
}
});
Monday, 25 April 2016
How you can force the garbage collection ?
Garbage collection automatic process and can't be forced.
However, you could request it by calling System.gc()
JVM does not guarantee that GC will be started immediately.
Garbage collection is also called automatic memory management as JVM automatically removes the unused variables/objects (value is null) from the memory. User program can't directly free the object from memory, instead it is the job of the garbage collector to automatically free the objects that are no longer referenced by a program.
Every class inherits finalize() method from java.lang.Object, the finalize() method is called by garbage collector when it determines no more references to the object exists.
In Java, it is good idea to explicitly assign null into a variable when no more in use.
On calling System.gc() and Runtime.gc(), JVM tries to recycle the unused objects, but there is no guarantee when all the objects will garbage collected.
Heap vs. Stack memory
Heap vs. Stack memory
CONTENTS
- Objects are always stored in Heap
- Reference variables to objects and primitive variables are stored in Stack memory
USABILITY
- Heap memory is used globally by all components of the application
- Stack memory is used only by one thread of execution.
LIFE
- Heap memory lives from the start till the end of application execution.
- Stack memory is short-lived.
SIZE
- Stack memory size is very less when compared to Heap memory.
ERRORS
- When stack memory is full, JRE throws java.lang.StackOverFlowError
- If heap memory is full, it throws java.lang.OutOfMemoryError
SPEED
- Stack memory is very fast when compared to heap memory due to simplicity in memory allocation (LIFO).
JVM OPTIONS
- Use -Xms and -Xmx JVM option to define the startup size and maximum size of heap memory.
- Use -Xss to define the stack memory size.
MEMORY MANAGEMENT
- Heap memory is divided into Young-Generation, Old-Generation etc.
- Memory management in stack is done in LIFO manner
Can we execute some code just before JVM shutdown ?
Yes, By adding shutdown hook
void addShutdownHook(Thread hook) registers a new JVM shutdown hook.
When JVM shuts down ?
1. Normal exit
* When the last non-daemon thread exits
* When System.exit() invoked
2. Interrupted by User
* When pressing ^C
* System-wide event like, Shutdown, Reboot, Log off
Why Shutdown hook is useful ?
Shutdown hook can be used to :
- Perform resource cleanup like, closing log file, sending some alerts
- Save the state when JVM shuts down
Implementation of Shutdown hook
A shutdown hook is an initialized but un-started thread.
Once the shutdown sequence has begun,
- It can be stopped only by invoking the halt method, which forcibly terminates the virtual machine.
- It is impossible to register a new shutdown hook or de-register a previously-registered hook, Otherwise it will cause an IllegalStateException.
Example
Defining Shutdown hook as a thread
class ShutDownHook extends Thread {
public void run(){
System.out.println("Shutdown hook tasks - Cleanup, Save state...");
}
}
Inside main method, Add the shutdown hook / thread
Runtime runtime = Runtime.getRuntime();
runtime.addShutdownHook(new ShutDownHook());
Example (same using anonymous class)
Runtime runtime = Runtime.getRuntime();
runtime.addShutdownHook(new Thread() {
public void run(){
System.out.println("Shutdown hook tasks - Cleanup, Save state...");
}
});
System.gc() vs. Runtime.gc()
System.gc() vs. Runtime.gc()
There is no difference !
System.gc() internally calls Runtime.gc()
public static void gc() {
Runtime.getRuntime().gc();
}
Runtime.getRuntime() provides singleton instance and then it calls its native method gc()
These methods sends request of Garbage collection to JVM but it’s not guaranteed that garbage collection will happen.
When finalized() method is called ?
Before removing an object from memory, Garbage collection thread invokes finalize() method of that object and gives an opportunity to perform any kind of cleanup required.
Can a programmer force the Garbage collection ?
Programmer cannot force garbage collection in Java.
It will only trigger automatically, if JVM thinks it needs a garbage collection based on Java heap size.
What are the side effects of caling System.gc() ?
Calling System.gc() triggers a full GC which takes significantly longer than a GC of the 'new space'.
By forcing the GC, you are causing the JVM to use extra CPU cycles, etc. which may potentially interfere with other things that the user is doing on his machine.
JDK vs. JRE vs. JVM
JDK
JDK (Java Development Kit) is a set of a Java compiler, a Java interpreter, Applet viewer, developer tools, Java API libraries, documentation which can be used by Java developers to develop Java-based applications.
Compiler converts java code into byte code. Java application launcher opens a JRE, loads the class, and invokes its main method.
JDK is mainly targeted for java development.
JDK includes JRE as as subset. JDK contains more then one JRE and developmental sub program like debugger, libraries etc.
JRE
JRE (Java Runtime Environment) is a minimum set that includes a Java interpreter, Java API libraries, Java browser plug-in, which make up the minimum environment to execute Java-based applications.
It includes the JVM, as the JRE provides some standard libraries and the JVM which can be used to execute a Java program.
JRE is targeted for execution of Java files.
JRE contains JVM, class libraries, and other supporting files.
It does not contain any development tools such as compiler, debugger, etc. Actually JVM runs the program, and it uses the class libraries, and other supporting files provided in JRE. If you want to run any java program, you need to have JRE installed in the system.
i.e. JRE = JVM + Java Packages Classes(like util, math, lang, awt,swing etc) + runtime libraries.
JVM
JVM (Java Virtual Machine) is the core of the Java platform and is a part of both the JDK and JRE that translates Java bytecodes and executes them as native code on the client machine.
JVM loads the appropriate class files for executing a Java Program, and then executes it.
JVM provides a platform-independent way of executing code; programmers can concentrate on writing software, without having to be concerned with how or where it will run.
JVM interprets the byte code into the machine code depending upon the underlying operating system and hardware combination. It is responsible for all the things like garbage collection, array bounds checking, etc.
JVM is platform dependent.
The JVM is called "virtual" because it provides a machine interface that does not depend on the underlying operating system and machine hardware architecture. This independence from hardware and operating system is a cornerstone of the write-once run-anywhere value of Java programs.
Sunday, 24 April 2016
When JVM starts up, which thread will be started up first ?
The thread executing main method is started.
Saturday, 23 April 2016
What are different types of Garbage collector in Java ?
Types of Garbage Collector in Java
Java Runtime provides various types of Garbage collection in Java which you can choose based upon your application's performance requirement.
Java 5 adds three additional garbage collectors except serial garbage collector.
1) Throughput Garbage Collector
It uses a parallel version of the young generation collector.
JVM argument to use -XX:+UseParallelGC
The tenured generation collector is same as the serial collector.
2) Concurrent low pause Collector
This is also referred as Concurrent Mark Sweep Garbage collector.
It is used to collect the tenured generation and does most of the collection concurrently with the execution of the application. The application is paused for short periods during the collection. A parallel version of the young generation copying collector is sued with the concurrent collector.
JVM arguments to use -Xingc or -XX:+UseConcMarkSweepGC
Concurrent Mark Sweep Garbage collector is most widely used garbage collector in java and it uses algorithm to first mark object which needs to collected when garbage collection triggers.
3) The Incremental (train) low pause collector
This garbage collector has not changed since the java 1.4.2 and is currently not under active development. It will not be supported in future releases so avoid using this and please see 1.4.2 GC Tuning document for information on this collector.
JVM argumets to use -XX:+UseTrainGC
Note : -XX:+UseParallelGC should not be used with -XX:+UseConcMarkSweepGC.
Subscribe to:
Posts (Atom)