Thursday, July 4, 2013

Garbage collection




Garbage Collection

1) objects are created on heap in Java  irrespective of there scope e.g. local or member variable. while its worth noting that class variables or static members are created in method area of Java memory space and both heap and method area is shared between different thread.
2) Garbage collection is a mechanism provided by Java Virtual Machine to reclaim heap space from objects which are eligible for Garbage collection.
3) Garbage collection relieves java programmer from memory management which is essential part of C++ programming and gives more time to focus on business logic.
4) Garbage Collection in Java is carried by a daemon thread called Garbage Collector.
5) Before removing an object from memory Garbage collection thread invokes finalize () method of that object and gives an opportunity to perform any sort of cleanup required.
6) You as Java programmer can not force Garbage collection in Java; it will only trigger if JVM thinks it needs a garbage collection based on Java heap size.
7) There are methods like System.gc () and Runtime.gc () which is used to send request of Garbage collection to JVM but it’s not guaranteed that garbage collection will happen.
8) If there is no memory space for creating new object in Heap Java Virtual Machine throws OutOfMemoryError or java.lang.OutOfMemoryError heap space
9) J2SE 5(Java 2 Standard Edition) adds a new feature called Ergonomics goal of ergonomics is to provide good performance from the JVM with minimum of command line tuning.



When an Object becomes Eligible for Garbage Collection

An Object becomes eligible for Garbage collection or GC if its not reachable from any live threads or any static refrences in other words you can say that an object becomes eligible for garbage collection if its all references are null. Cyclic dependencies are not counted as reference so if Object A has reference of object B and object B has reference of Object A and they don't have any other live reference then both Objects A and B will be eligible for Garbage collection.
Generally an object becomes eligible for garbage collection in Java on following cases:
1) All references of that object explicitly set to null e.g. object = null
2) Object is created inside a block and reference goes out scope once control exit that block.
3) Parent object set to null, if an object holds reference of another object and when you set container object's reference null, child or contained object automatically becomes eligible for garbage collection.
4) If an object has only live references via WeakHashMap it will be eligible for garbage collection. To learn more about HashMap see here How HashMap works in Java.

Heap Generations for Garbage Collection in Java

Java objects are created in Heap and Heap is divided into three parts or generations for sake of garbage collection in Java, these are called as Young generation, Tenured or Old Generation and Perm Area of heap.
New Generation is further divided into three parts known as Eden space, Survivor 1 and Survivor 2 space. When an object first created in heap its gets created in new generation inside Eden space and after subsequent Minor Garbage collection if object survives its gets moved to survivor 1 and then Survivor 2 before Major Garbage collection moved that object to Old or tenured generation.

Permanent generation of Heap or Perm Area of Heap is somewhat special and it is used to store Meta data related to classes and method in JVM, it also hosts String pool provided by JVM as discussed in my string tutorial why String is immutable in Java. There are many opinions around whether garbage collection in Java happens in perm area of java heap or not, as per my knowledge this is something which is JVM dependent and happens at least in Sun's implementation of JVM. You can also try this by just creating millions of String and watching for Garbage collection or OutOfMemoryError.

Types of Garbage Collector in Java

Java Runtime (J2SE 5) 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. Each is generational garbage collector which has been implemented to increase throughput of the application or to reduce garbage collection pause times.

1) Throughput Garbage Collector: This garbage collector in Java uses a parallel version of the young generation collector. It is used if the -XX:+UseParallelGC option is passed to the JVM via command line options . The tenured generation collector is same as the serial collector.

2) Concurrent low pause Collector: This Collector is used if the -Xingc or -XX:+UseConcMarkSweepGC is passed on the command line. This is also referred as Concurrent Mark Sweep Garbage collector. The concurrent collector 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. 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 (Sometimes called train) low pause collector: This collector is used only if -XX:+UseTrainGC is passed on the command line. 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.
Important point to not is that -XX:+UseParallelGC should not be used with -XX:+UseConcMarkSweepGC. The argument passing in the J2SE platform starting with version 1.4.2 should only allow legal combination of command line options for garbage collector but earlier releases may not find or detect all illegal combination and the results for illegal combination are unpredictable. It’s not recommended to use this garbage collector in java.

JVM Parameters for garbage collection in Java

Garbage collection tuning is a long exercise and requires lot of profiling of application and patience to get it right. While working with High volume low latency Electronic trading system I have worked with some of the project where we need to increase the performance of Java application by profiling and finding what causing full GC and I found that Garbage collection tuning largely depends on application profile, what kind of object application has and what are there average lifetime etc. for example if an application has too many short lived object then making Eden space wide enough or larger will reduces number of minor collections. you can also control size of both young and Tenured generation using JVM parameters for example setting -XX:NewRatio=3 means that the ratio among the young and tenured generation is 1:3 , you got to be careful on sizing these generation. As making young generation larger will reduce size of tenured generation which will force Major collection to occur more frequently which pauses application thread during that duration results in degraded or reduced throughput. The parameters NewSize and MaxNewSize are used to specify the young generation size from below and above. Setting these equal to one another fixes the young generation. In my opinion before doing garbage collection tuning detailed understanding of garbage collection in java is must and I would recommend reading Garbage collection document provided by Sun Microsystems for detail knowledge of garbage collection in Java. Also to get a full list of JVM parameters for a particular Java Virtual machine please refer official documents on garbage collection in Java. I found this link quite helpful though http://www.oracle.com/technetwork/java/gc-tuning-5-138395.html

Full GC and Concurrent Garbage Collection in Java

Concurrent garbage collector in java uses a single garbage collector thread that runs concurrently with the application threads with the goal of completing the collection of the tenured generation before it becomes full. In normal operation, the concurrent garbage collector is able to do most of its work with the application threads still running, so only brief pauses are seen by the application threads. As a fall back, if the concurrent garbage collector is unable to finish before the tenured generation fill up, the application is paused and the collection is completed with all the application threads stopped. Such Collections with the application stopped are referred as full garbage collections or full GC and are a sign that some adjustments need to be made to the concurrent collection parameters. Always try to avoid or minimize full garbage collection or Full GC because it affects performance of Java application. When you work in finance domain for electronic trading platform and with high volume low latency systems performance of java application becomes extremely critical an you definitely like to avoid full GC during trading period.

Summary on Garbage collection in Java

1) Java Heap is divided into three generation for sake of garbage collection. These are young generation, tenured or old generation and Perm area.
2) New objects are created into young generation and subsequently moved to old generation.
3) String pool is created in Perm area of Heap, garbage collection can occur in perm space but depends upon JVM to JVM.
4) Minor garbage collection is used to move object from Eden space to Survivor 1 and Survivor 2 space and Major collection is used to move object from young to tenured generation.
5) Whenever Major garbage collection occurs application threads stops during that period which will reduce application’s performance and throughput.
6) There are few performance improvement has been applied in garbage collection in java 6 and we usually use JRE 1.6.20 for running our application.
7) JVM command line options –Xmx and -Xms is used to setup starting and max size for Java Heap. Ideal ratio of this parameter is either 1:1 or 1:1.5 based upon my experience for example you can have either both –Xmx and –Xms as 1GB or –Xms 1.2 GB and 1.8 GB.
8) There is no manual way of doing garbage collection in Java.

Difference between ClassNotFoundException vs NoClassDefFoundError in Java

What is ClassNotFoundException and NoClassDefFoundError

From last few weeks I have been facing a cluster of ClassNotFoundException and NoClassDefFoundError while setting up a new project in Java. This new java project has lots of dependency on various jars and some of the jar even contains the same name of file which makes my problem even more problematic. While working with NoClassDefFoundError and ClassNotFoundException I thought to document my experience and I have already shared some on 3 ways to resolve NoClassDefFoundError in java and how to resolve ClassNotFoundException in java. in this article though focus will be on similarity and  differences between java.lang.ClassNotFoundException and java.lang.NoClassDefFoundError in Java.






NoClassDefFoundError vs ClassNotFoundException

NoClassDefFoundError vs ClassNotFoundException, difference between ClassNotFoundException vs NoClassDefFoundErrorBefore seeing the differences between ClassNotFoundException and NoClassDefFoundError let's see some similarities which are main reason of confusion between these two errors:

1) Both NoClassDefFoundError and ClassNotFoundException are related to unavailability of a class at run-time.
2) Both ClassNotFoundException and NoClassDefFoundError are related to java classpath.

Now let's see the difference between NoClassDefFoundError and ClassNotFoundException:

1) ClassNotFoundException comes in java if we try to load a class at run-time using with Class.forName() or ClassLoader.loadClass() or ClassLoader.findSystemClass() method and requested class is not available in Java. the most of the time it looks like that we have the class in classpath but eventually it turns out to be issue related to classpath and application may not be using classpath what we think it was using e.g. classpath defined in jar's manifest file will take precedence over CLASSPATH or -cp option, for more details see How classpath works in java. On the other hand NoClassDefFoundError is little different than ClassNotFoundException, in this case culprit class was present during compile time and let's application to compile successfully and linked successfully but not available during run-time due to various reason.

2) ClassNotFoundException is a checked Exception derived directly from java.lang.Exception class and you need to provide explicit handling for it while NoClassDefFoundError is an Error derived from LinkageError.

3) If you are using classloaders in Java and have two classloaders then if a classloader tries to access a class which is loaded by another classloader will result in ClassNoFoundException.

4) ClassNotFoundException comes up when there is an explicit loading of class is involved by providing name of class at runtime using ClassLoader.loadClass, Class.forName while NoClassDefFoundError is a result of implicit loading of class because of a method call from that class or any variable access.

Please let us know if you are aware of any other difference between NoClassdefFoundError and ClassNotFoundException in Java , I would be happy to incorporate those. 



Thursday, June 13, 2013

How to take Thread Dumps from a JVM

How to take Thread Dumps from a JVM

 

Question

A thread dump is a list of all the Java threads that are currently active in a Java Virtual Machine (JVM).
How can I take thread dumps from a JVM on Unix or Windows?

Answer, Resolution

There are several ways to take thread dumps from a JVM. It is highly recommended to take more than 1 thread dump. A good practice is to take 10 thread dumps at a regular interval (eg. 1 thread dump every 10 seconds).

Step 1: Get the PID of your java process

The first piece of information you will need to be able to obtain a thread dump is your java process's PID.
The java JDK ships with the jps command which lists all java process ids. You can run this command like this: jps -l
70660 sun.tools.jps.Jps
70305
Note: In Linux and UNIX, you may have to run this command as sudo -u user jps -l, where "user" is the username of the user that the java process is running as.If this doesn't work or you still cannot find your java process, (path not set, JDK not installed, or older Java version), use
  • UNIX, Linux and Mac OSX:
    ps -el | grep java
  • Windows:
    Press Ctrl+Shift+Esc to open the task manager and find the PID of the java process

Step 2: Request a Thread Dump from the JVM

jstack

If installed/available, we recommend using the jstack tool. It prints thread dumps to the command line console.
To obtain a thread dump using jstack, run the following command:
jstack <pid>
You can output consecutive thread dumps to a file by using the console output redirect/append directive:
jstack <pid> >> threaddumps.log
Notes:
  • The jstack tool is available since JDK 1.5 (for JVM on Windows it's available in some versions of JDK 1.5 and JDK 1.6 only).
  • jstack works even if the -Xrs jvm parameter is enabled
  • It's not possible to use the jstack tool from JDK 1.6 to take threaddumps from a process running on JDK 1.5.
  • In Linux and UNIX, you may need to run this command as sudo -u user jstack <pid> >> threaddumps.log, where "user" is the user that the java process is running as.
  • In Windows, if you run jstack and get the error "Not enough storage is available to process this command" then you must run jstack as the windows SYSTEM user.  You can do this by using psexec which you can download here. Then you can run jstack like this:
    psexec -s jstack <pid> >> threaddumps.log

jstack script

Here's a script, taken from eclipse.org that will take a series of thread dumps using jstack.
#!/bin/bash
if [ $# -eq 0 ]; then
    echo >&2 "Usage: jstackSeries <pid> <run_user> [ <count> [ <delay> ] ]"
    echo >&2 "    Defaults: count = 10, delay = 0.5 (seconds)"
    exit 1
fi
pid=$1          # required
user=$2         # required
count=${3:-10}  # defaults to 10 times
delay=${4:-0.5} # defaults to 0.5 seconds
while [ $count -gt 0 ]
do
    sudo -u $user jstack -l $pid >jstack.$pid.$(date +%H%M%S.%N)
    sleep $delay
    let count--
    echo -n "."
done
Just run it like this:
sh jstackSeries.sh [pid] [cq5serveruser] [count] [delay]
For example:
sh jstackSeries.sh 1234 cq5serveruser 10 3
  • 1234 is the pid of the java process
  • cq5serveruser is the Linux or Unix user that the java process runs as
  • 10 is how many thread dumps to take
  • 3 is the delay between each dump):

Alternative Ways to Obtain a Thread Dump

If the jstack tool is not available to you then you can take thread dumps as follows:

Note: Some tools cannot take thread dumps from the JVM if the commandline parameter -Xrs is enabled. If you are having trouble taking thread dumps then please see if this option is enabled.
Unix, Mac OSX and Linux (JDK 1.4 or lesser version)
On Unix, Mac OSX and Linux, you can send a QUIT signal to the java process to tell it to output a thread dump to standard output.
  1. Run this command to do this:
    kill -QUIT <pid>
    You may need to run this command as sudo -u user kill -QUIT <pid> where "user" is the user that the java process is running as.
  2. If you are starting CQSE using the crx-quickstart/server/start script then your thread dumps will be output to crx-quickstart/server/logs/startup.log. If you are using a 3rd party application server such as JBoss, WebSphere, Tomcat, or other then please see the server's documentation to find out which file the standard output is directed to.
Windows:
JDK 1.X
  1. Download javadump.exe (Attached below)
  2. Start the JVM with these 3 arguments. They must be in the right order.
    -XX:+UnlockDiagnosticVMOptions -XX:+LogVMOutput -XX:LogFile=C:\tmp\jvmoutput.log
  3. Press Ctrl+Shift+Esc to open the task manager
  4. Find the PID of the java process
  5. From the command line run
    javadump.exe [pid]
  6. The thread dump will appear in the jvmoutput.log file mentioned in step 2.
JDK 1.6
Get a thread dump from jconsole tool, by using a plugin : [0]
Here's how you can request a thread dump:
  1. Add the following parameter to the jvm running Communique : -Dcom.sun.management.jmxremote
  2. Download and install JDK 1.6 (if not done yet)
  3. Download and extract the Thread Dump Analyzer utility [1]
  4. Run jconsole.exe of JDK 1.6
    jconsole.exe -pluginpath /path/to/file/tda.jar
  5. Click on the Thread dumps tab
  6. Click on the Request Thread Dump ... link

Thursday, February 21, 2013

Is Java Pass by Reference or Pass by Value?

The Java Spec says that everything in Java is pass-by-value. There is no such thing as "pass-by-reference" in Java. The difficult thing can be to understand that Java passes "objects as references" passed by value. This can certainly get confusing and I would recommend reading this article from an expert:

Wednesday, February 20, 2013

Top 10 Tricky Java interview questions and Answers

Tricky Java interview questions are those question which has some surprise element on it and if you answer tricky Java question with common sense, you are likely to fail because they are tricky, they require something special to know. Most of the tricky Java questions comes from method overloading and overriding, Checked and Unchecked Exception and subtle Java programming details like Integer overflow. Most important thing to answer tricky Java question is attitude and analytical thinking , which helps even if you don't know the answer. Anyway in this Java article we will see 10 Java questions which is real tricky and requires more than average knowledge of Java programming language to answer. As per my experience there are always one or two tricky or tough Java interview question on any core Java or J2EE interviews, so its good to prepare tricky questions from Java in advance. 10 Tricky Java interview question - Answered Tricky Java interview question and answer for experienced programmerHere is my list of 10 tricky Java interview questions, Though I have prepared and shared lot of difficult core java interview question and answers, But I have chosen them as Top 10 tricky questions because you can not guess answers of this tricky java questions easily, you need some subtle details of Java programming language to answer these questions.  




What will happen if you call return statement or System.exit on try or catch block ? will finally block execute? This is a very popular tricky Java question and its tricky because many programmer think that finally block always executed. This question challenge that concept by putting return statement in try or catch block or calling System.exit from try or catch block. Answer of this tricky question in Java is that finally block will execute even if you put return statement in try block or catch block but finally block won't run if you call System.exit form try or catch.

 Can you override private or static method in Java ? Another popular Java tricky question, As I said method overriding is a good topic to ask trick questions in Java. Anyway, you can not override private or static method in Java, if you create similar method with same return type and same method arguments that's called method hiding. See Can you override private method in Java or more details.


 Does Java support multiple inheritance ? This is the trickiest question in Java, if C++ can support direct multiple inheritance than why not Java is the argument Interviewer often give. See Why multiple inheritance is not supported in Java to answer this tricky Java question.  


What will happen if we put a key object in a HashMap which is already there ? This tricky Java questions is part of How HashMap works in Java, which is also a popular topic to create confusing and tricky question in Java. well if you put the same key again than it will replace the old mapping because HashMap doesn't allow duplicate keys. See How HashMap works in Java for more tricky Java questions from HashMap.

 If a method throws NullPointerException in super class, can we override it with a method which throws RuntimeException? One more tricky Java questions from overloading and overriding concept. Answer is you can very well throw super class of RuntimeException in overridden method but you can not do same if its checked Exception. See Rules of method overriding in Java for more details.  



What is the issue with following implementation of compareTo() method in Java public int compareTo(Object o){ Employee emp = (Employee) emp; return this.id - o.id; } where id is an integer number ? Well three is nothing wrong in this Java question until you guarantee that id is always positive. This Java question becomes tricky when you can not guaranteed id is positive or negative. If id is negative than subtraction may overflow and produce incorrect result. See How to override compareTo method in Java for complete answer of this Java tricky question for experienced programmer.


How do you ensure that N thread can access N resources without deadlock If you are not well versed in writing multi-threading code then this is real tricky question for you. This Java question can be tricky even for experienced and senior programmer, who are not really exposed to deadlock and race conditions. Key point here is order, if you acquire resources in a particular order and release resources in reverse order you can prevent deadlock. See how to avoid deadlock in Java for a sample code example.  


What is difference between CyclicBarrier and CountDownLatch in Java Relatively newer Java tricky question, only been introduced form Java 5. Main difference between both of them is that you can reuse CyclicBarrier even if Barrier is broken but you can not reuse CountDownLatch in Java. See CyclicBarrier vs CountDownLatch in Java for more differences.



What is difference between StringBuffer and StringBuilder in Java ? Classic Java questions which some people thing tricky and some consider very easy. StringBuilder in Java is introduced in Java 5 and only difference between both of them is that Stringbuffer methods are synchronized while StringBuilder is non synchronized. See StringBuilder vs StringBuffer for more differences.





 Can you access non static variable in static context? Another tricky Java question from Java fundamentals. No you can not access static variable in non static context in Java. Read why you can not access non-static variable from static method to learn more about this tricky Java questions. This was my list of 10 most common tricky question in Java . It's not a bad idea to prepare tricky Java question before appearing for any core Java or J2EE interview. One or two open ended or tricky question is quite common in Java interviews.

Why String is immutable or final in Java

This is one of the most popular interview question on String in Java which starts with discussion of What is immutable object in Java , what are the benefits of immutable object , why do you use it and which scenarios do you use it. This is some time also asked as "Why String is final in Java" . It can also come once interviewee answers some preliminarily strings questions e.g. What is String pool in Java , What is the difference between String and StringBuffer , What does intern method do in String, What is special about String Class in Java, Why String is popular HashMap key in Java, Does String is thread-safe in Java, How to compare two String in Java , How SubString works in Java and What is the difference between StringBuffer vs StringBuilder Why char array is preferred over String for passwords Though there could be many possible answer for this question and only designer of String class can answer this , I think below two does make sense 1)Imagine StringPool facility without making string immutable , its not possible at all because in case of string pool one string object/literal e.g. "Test" has referenced by many reference variables , so if any one of them change the value others will be automatically gets affected i.e. lets say String A = "Test" String B = "Test" Now String B called "Test".toUpperCase() which change the same object into "TEST" , so A will also be "TEST" which is not desirable. 2)String has been widely used as parameter for many java classes e.g. for opening network connection you can pass hostname and port number as string , you can pass database URL as string for opening database connection, you can open any file in Java by passing name of file as argument to File I/O classes. In case if String is not immutable , this would lead serious security threat , I mean some one can access to any file for which he has authorization and then can change the file name either deliberately or accidentally and gain access of those file. This is some time asked as Why Char array is better than String for Storing password in Java in interviews as well. 3)Since String is immutable it can safely shared between many threads ,which is very important for multi threaded programming and to avoid any synchronization issues in Java, Immutability also makes String instance thread-safe in Java, means you don't need to synchronize String operation externally. Another important point to note about String is memory leak caused by SubString, which is not a thread related issues but something to be aware of. 4) Another reason of Why String is immutable in Java is to allow String to cache its hashcode , being immutable String in Java caches its hashcode and do not calculate every time we call hashcode method of String, which makes it very fast as hashmap key to be used in hashmap in Java. This one is also suggested by Jaroslav Sedlacek in comments below. In short because String is immutable, no one can change its contents once created which guarantees hashCode of String to be same on multiple invocation. 5) Another good reason of Why String is immutable in Java suggested by Dan Bergh Johnsson on comments is: The absolutely most important reason that String is immutable is that it is used by the class loading mechanism, and thus have profound and fundamental security aspects. Had String been mutable, a request to load "java.io.Writer" could have been changed to load "mil.vogoon.DiskErasingWriter" I believe there could be some more very convincing reasons also , Please post those reasons as comments and I will include those on this post. I think above reason holds good for another java interview questions "Why String is final in Java" also to be immutable you have to be final so that your subclass doesn't break immutability. what do you guys think ? Read more: http://javarevisited.blogspot.com/2010/10/why-string-is-immutable-in-java.html#ixzz2LVtUBqxt

Difference between JIT and JVM in Java - Interview Question

Main difference between JIT and JVM is that, JIT is part of JVM itself and used to improve performance of JVM. JIT stands for Just In time compilation and JVM stands for Java Virtual Machine. JVM is a virtual machine used in Java programming platform to execute or run Java programs. Main advantage of JVM is that, it makes Java platform independent by executing byte codes. Java source code is compiled into class files, which contains byte code. These byte codes are then executed by JVM. Now here comes JIT. Since execution of byte code is slower than execution of machine language code, because JVM first needs to translate byte code into machine language code. JIT helps JVM here by compiling currently executing byte code into machine language. JIT also offers caching of compiled code which result in improved performance of JVM. by the way difference between JVM and JIT is also a good Java interview question to ask. Well, this is just a simple explanation, JIT is lot more complex than this. There are sophisticated algorithm which helps JIT to pick most executed code for compiling into machine code. Earlier we have seen difference between JRE and JDK and in this post we will understand difference between JVM and JIT. Let's see next section for more difference between JIT vs JVM. JVM vs JIT Difference between JVM and JIT in Java programming Here are couple of more differences between JVM and JIT in Java programing platform : 1) Main difference between JVM and JIT is there purpose, main goal of JVM is to provide platform independence while objective of JIT is to improve performance of JVM, by compiling more code into machine language. Just keep in mind that this compilation also takes time, so translating all code into native code is not worth doing. That's why JIT mostly compile frequently used code into native code. 3) Another difference between JIT and JVM is that, JIT is part of JVM. One example of JIT is Oracle's Hotspot JIT which comes with Hotspot JVM. 2) At last, JVM is older concept than JIT. JIT actually get invented to improve performance of JVM. That's all on difference between JVM and JIT in Java. As I said, JIT is part of JVM and used to improve JVM performance by dynamically compiling or translating Java byte codes into native machine language code during execution time.