You are on page 1of 12

Garbage Collection

I have read many articles on Garbage Collection in Java, some of them are too complex to understand and some of them dont contain enough information required to understand garbage collection in Java. Then I decided to write my own experience as an article or you call tutorial about How Garbage Collection works in Java or what is Garbage collection in Java in simple word which would be easy to understand and have sufficient information to understand how garbage collection works in Java. This article is in continuation of my previous articles How Classpath works in Java and How to write Equals method in java and before moving ahead let's recall few important points about garbage collection in java: 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 its 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. Its 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 applications 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.

How HashMap works in Java


How HashMap works in Java How HashMap works in Java or sometime how get method work in HashMap is common interview questions now days. Almost everybody who worked in Java knows what hashMap is, where to use hashMap or difference between hashtable and HashMap then why this interview question becomes so special? Because of the breadth and depth this question offers. It has become very popular java interview question in almost any senior or mid-senior level java interviews. Questions start with simple statement "Have you used HashMap before" or "What is HashMap? Why do we use it Almost everybody answers this with yes and then interviewee keep talking about common facts about hashMap like hashMap accpt null while hashtable doesn't, HashMap is not synchronized, hashMap is fast and so on along with basics like its stores key and value pairs etc. This shows that person has used hashMap and quite familiar with the functionality HashMap offers but interview takes a sharp turn from here and next set of follow up questions gets more detailed about fundamentals involved in hashmap. Interview here you and come back with questions like "Do you Know how hashMap works in Java or "How does get () method of HashMap works in Java" And then you get answers like I don't bother its standard Java API, you better look code on java; I can find it out in Google at any time etc. But some interviewee definitely answer this and will say "HashMap works on principle of hashing, we have put () and get () method for storing and retrieving data from hashMap. When we pass an object to put () method to store it on hashMap, hashMap implementation calls hashcode() method hashMap key object and by applying that hashcode on its own hashing funtion it identifies a bucket location for storing value object , important part here is HashMap stores both key+value in bucket which is essential to understand the retrieving logic. if people fails to recognize this and say it only stores Value in the bucket they will fail to explain the retrieving logic of any object stored in HashMap . This answer is very much acceptable and does make sense that interviewee has fair bit of knowledge how hashing works and how HashMap works in Java. But this is just start of story and going forward when depth increases a little bit and when you put interviewee on scenarios every java developers faced day by day basis. So next question would be more likely about collision detection and collision resolution in Java HashMap e.g "What will happen if two different objects have same hashcode? Now from here confusion starts some time interviewer will say that since Hashcode is equal objects are equal and HashMap will throw exception or not store it again etc. then you might want to remind them about equals and hashCode() contract that two unequal object in Java very much can have equal hashcode. Some will give up at this point and some will move ahead and say "Since hashcode () is same, bucket location would be same

and collision occurs in hashMap, Since HashMap use a linked list to store in bucket, value object will be stored in next node of linked list." great this answer make sense to me though there could be some other collision resolution methods available this is simplest and HashMap does follow this. But story does not end here and final questions interviewer ask like "How will you retreive if two different objects have same hashcode? Hmmmmmmmmmmmmm Interviewee will say we will call get() method and then HashMap uses keys hashcode to find out bucket location and retrieves object but then you need to remind him that there are two objects are stored in same bucket , so they will say about traversal in linked list until we find the value object , then you ask how do you identify value object because you don't value object to compare ,So until they know that HashMap stores both Key and Value in linked list node they won't be able to resolve this issue and will try and fail. But those bunch of people who remember this key information will say that after finding bucket location , we will call keys.equals() method to identify correct node in linked list and return associated value object for that key in Java HashMap. Perfect this is the correct answer. In many cases interviewee fails at this stage because they get confused between hashcode () and equals () and keys and values object in hashMap which is pretty obvious because they are dealing with the hashcode () in all previous questions and equals () come in picture only in case of retrieving value object from HashMap. Some good developer point out here that using immutable, final object with proper equals () and hashcode () implementation would act as perfect Java HashMap keys and improve performance of Java hashMap by reducing collision. Immutability also allows caching there hashcode of different keys which makes overall retrieval process very fast and suggest that String and various wrapper classes e.g Integer provided by Java Collection API are very good HashMap keys. Now if you clear all this java hashmap interview question you will be surprised by this very interesting question "What happens On HashMap in Java if the size of the Hashmap exceeds a given threshold defined by load factor ?". Until you know how hashmap works exactly you won't be able to answer this question. if the size of the map exceeds a given threshold defined by load-factor e.g. if load factor is .75 it will act to resize the map once it filled 75%. Java Hashmap does that by creating another new bucket array of size twice of previous size of hashmap, and then start putting every old element into that new bucket array and this process is called rehashing because it also applies hash function to find new bucket location. If you manage to answer this question on hashmap in java you will be greeted by "do you see any problem with resizing of hashmap in Java" , you might not be able to pick the context and then he will try to give you hint about multiple thread accessing the java hashmap and potentially looking for race condition on HashMap in Java. So the answer is Yes there is potential race condition exists while resizing hashmap in Java, if two thread at the same time found that now Java Hashmap needs resizing and they both try to resizing. on the process of resizing of hashmap in Java , the element in bucket which is stored in linked list get reversed in order during there migration to new bucket because java hashmap doesn't append the new element at tail instead it append new element at head to avoid tail traversing. if race condition happens then you will end up with an infinite loop. though this point you can potentially argue that what the hell makes you think to use HashMap in multi-threaded environment to interviewer :) I like this question because of its depth and number of concept it touches indirectly, if you look at questions asked during interview this HashMap questions has verified Concept of hashing Collision resolution in HashMap Use of equals () and hashCode () method and there importance? Benefit of immutable object? race condition on hashmap in Java Resizing of Java HashMap Just to summarize here are the answers which does makes sense for above questions How HashMAp works in Java

HashMap works on principle of hashing, we have put () and get () method for storing and retrieving object form hashMap.When we pass an both key and value to put() method to store on HashMap, it uses key object hashcode() method to calculate hashcode and they by applying hashing on that hashcode it identifies bucket location for storing value object. While retrieving it uses key object equals method to find out correct key value pair and return value object associated with that key. HashMap uses linked list in case of collision and object will be stored in next node of linked list. Also hashMap stores both key+value tuple in every node of linked list. What will happen if two different HashMap key objects have same hashcode? They will be stored in same bucket but no next node of linked list. And keys equals () method will be used to identify correct key value pair in HashMap. In terms of usage HashMap is very versatile and I have mostly used hashMap as cache in electronic trading application I have worked . Since finance domain used Java heavily and due to performance reason we need caching a lot HashMap comes as very handy there.

How to override equals method in Java


When I started Java I heard that Java is truly object oriented language and everything in Java is an object, later I found that though its not completely true but yes most of things are in Java is represented via OOPS concept. One of them is Introduction of java.lang.Object class and every class in Java extends this class by default. So when I started my blog and writing about my Java experience as Core Java Tutorials I thought lets write something which helps beginners in Java.

Object class holds some very interesting method which is applicable at object level and one of them is equals () which we are going to discuss in this article. equals () method is used to compare the equality of two object. default implementation of equals() class provided by Object class compares memory location and only return true if two reference variable are pointing to same memory location i.e. essentially they are same object. JAVA recommends to override equals method if equality is going to be define by logical way or via some business logic and some classes in Java standard library does override it e.g. String class whose implementation of equals() method return true if content of two strings are exactly same. This article is in continuation of my previous articles How Classpath works in Java , difference between HashMap and Hashtable in Java , How Garbage collection works in Java and How Synchronization works in Java if you havent read already you may find some useful information based on my experience in Java . Since HashMap and hashtable in Java relies on equals () and hashcode () method for comparing keys and values, java has provided a contract to follow while overriding equals () method which state that as per Java your equals() method should adhere following rule: 1) Reflexive: Object must be equal to itself. 2) Symmetric: if a.equals(b) is true then b.equals(a) must be true. 3) Transitive: if a.equals(b) is true and b.equals(c) is true then c.equals(ad) must be true. 4) Consistent: multiple invocation of equals() method must result same value until any of properties are modified. So if two objects are equals in Java they will remain equals until any of there property is modified. 5) Null comparison: comparing any object to null must be false and should not result in NullPointerException. For example a.equals(null) must be false. And equals method in Java must follow its commitment with hashcode as stated below. 1) If two objects are equal by equals() method then there hashcode must be same. 2) If two objects are unequal by equals() method then there hashcode could be same or different. So this was the basic theory about equals () method in Java now we are going to discuss the approach on how to override equals () method, yes I know you all know this stuff :) but I have seen some of equals () code which can really be improved by following correct. For illustration purpose we will see an example of Person class and discuss How to write equals () method for that class.

Overriding equals method in Java


Here is my approach for overriding equals () method in Java. This is based on standard approach most of java programmer follows while writing equals method in Java. 1) Do this check -- if yes then return true. 2) Do null check -- if yes then return false. 3) Do the instanceof check if instanceof return false than return false , after some research I found that instead of instanceof we can use getClass() method for type identification because instanceof check returns true for subclass also, so its not strictly equals comparison until required by business logic. But instanceof check is fine if your class is immutable and no one is going to sub class it. For example we can replace instanceof check by below code

if((obj == null) || (obj.getClass() != this.getClass())) return false;


4) Type cast the object; note the sequence instanceof check must be prior to casting object. 5) Compare individual attribute start with numeric attribute because comparing numeric attribute is fast and use short circuit operator for combining checks. So if first field does not match, don't try to match rest of attribute and return false. Its also worth to remember doing null check on individual attribute before calling equals() method on them recursively to avoid NullPointerException during equals check in Java.

Code Example of overriding equals method in Java


Lets see a code example based on my approach of overriding equals method in Java as discussed in above paragraph: class Person{ private int id; private String firstName; private String lastName; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public boolean equals(Object obj){ if(obj == this){ return true; } if(obj == null || obj.getClass() != this.getClass()){ return false }

Person guest = (Person) obj; return id == guest.id && (firstName == guest.firstName || (firstName != null && firstName.equals(guest.getFirstName()) && (lastName == guest.lastName || (lastName != null && lastName.equals(guest.getLastName()); }

If you look above method we are first checking for "this" check which is fastest available check for equals method then we are verifying whether object is null or not and object is of same type or not. only after verifying type of object we are casting it into desired object to avoid any ClassCastException in Java. Also while comparing individual attribute we are comparing numeric attribute first ,using short circuit operator to avoid further calculation if its already unequal and doing null check on member attribute to avoid NullPointerException.

Common Errors while overriding equals in Java


Though equals and hashcode method are defined in Object class and one of fundamental part of Java programming I have seen many programmers making mistake while writing equals() method in Java. I recommend all Java programmer who has just started programming to write couple of equals and hashcode method for there domain or value object to get feel of it. Here I am listing some of common mistakes I have observed on various equals method in Java. 1) Instead of overriding equals() method programmer overloaded it. This is the most common error I have seen while overriding equals method in Java. Syntax of equals method defined in Object class is public boolean equals(Object obj) but many people unintentionally overload equals method in Java by writing public boolean equals(Person obj), instead of using Object as argument they use there class name. This error is very hard to detect because of static binding. So if you call this method in your class object it will not only compile but also execute correctly but if you try to put your object in collection e.g. ArrayList and call contains() method which is based on equals() method in Java it will not able to detect your object. So beware of it.

2) Second mistake I have seen while overriding equals() method is not doing null check for member variables which ultimately results in NullPointerException during equals() invocation. For example in above code correct way of calling equals() method of member variable is after doing null check as shown below: firstname == guest.firstname || (firstname != null && firstname.equals(guest.firstname)));

3) Third mistake I have seen is not overriding hashcode method in Java and only overriding equals() method. You must have to override both equals() and hashcode() method in Java , otherwise your value object will not be able to use as key object in HashMap because hashmap in based on equals() and hashcode to read more see , How HashMap works in Java

Writing JUnit tests for equals method in Java


Its good practice to write JUnit test cases to test your equals method. Here is my approach for writing JUnit test case for equals method in Java. I will write test cases to check equals behavior on different circumstances: testReflexive() this method will test reflexive nature of equals() method in Java. testSymmeteric() this method will verify symmetric nature of equals() method in Java. testNull() this method will verify null comparison and will pass if equals method returns false. testConsistent() will verify consistent nature of equals method in Java. testNotEquals() will verify if two object which are not supposed to equals is actually not equal testHashcode() will verify that if two objects are equal by equals() method in Java then there hashcode must be same.

5 Tips on Equals method in Java

Tip: most of the IDE provides support to generate equals() and hashcode() method In Eclipse do the right click-> source -> generate hashCode() and equals(). Tip: if your domain class has any unique business key then just comparing that field in equals method would be enough instead of comparing all the fields e.g. in case of our example if "id" is unique for every person then by just comparing id we will be able to identify whether two persons are equal or not. Tip: while overriding hashcode method in Java makes sure you use all fields which have been used while writing equals method in Java. Tip: String and Wrapper classes in java override equals method but StringBuffer doesnt override it. Tip: Whenever possible try to make your fields immutable, equals method based on immutable fields are much secure than equals method based on mutable fields.

1. What is immutable object? Can you write immutable object?

You need to make class final and all its member final so that once objects gets crated no one can modify its state. You can achieve same functionality by making member as non final but private and not modifying them except in constructor. 2. Does all property of immutable object needs to be final? Not necessary as stated above you can achieve same functionality by making member as non final but private and not modifying them except in constructor. 3. What is the difference between creating String as new () and literal? When we create string with new () its created in heap and not added into string pool while String created using literal are created in String pool itself which exists in Perm area of heap.

String s = new String("Test"); will put the object in String pool , it it does then why do one need String.intern() method which is used to put Strings into String pool explicitly. its only when you create String object as String literal e.g. String s = "Test" it put the String in pool.
4. How does substring () inside String works? Another good question, I think answer is not sufficient but here it is Substring creates new object out of string by taking a portion of original string.

suggested by Anand and Anonymous

The substring() method is used to return a part (or substring) of the String used to invoke the method. The first argument represents the starting location (zero-based) of the substring. If the call has only one argument, the substring returned will include the characters to the end of the original String. If the call has two arguments, the substring returned will end with the character located in the nth position of the original String where n is the second argument. Unfortunately, the ending argument is not zero-based, so if the second argument is 7, the last character in the returned String will be in the original Strings 7 position, which is index 6. Lets look at an example: String x = "0123456789"; System.out.println( x.substring(5) ); // output is "56789" System.out.println( x.substring(5, 8)); // output is "567"

The first example should be easy: start at index 5 and return the rest of the String. The second example should be read as follows: start at index 5 and return the characters up to and including the 8th position (index 7).

and @Anonymous pointed out some interesting fact: omething important about String.substring() method, its implementation calls the following String(...) constructor : // Package private constructor which shares value array for speed. String(int offset, int count, char value[]) { this.value = value; this.offset = offset; this.count = count; } That means the new String() object returned by substring(...) shares the same backing array (this.value) as the original string object. Thus if your original string object is 1GB long, the substring object will always be 1GB long too! You will probably want to trim the new array size to the substring range, this should do the job: String veryLongString = readFromFile(file); String tinyString = new String(veryLongString.substring(1,10)); The String(String) constructor is implemented that way: public String(String original) { ... if (originalValue.length > size) { // The array representing the String is bigger than the new // String itself. Perhaps this constructor is being called // in order to trim the baggage, so make a copy of the array. int off = original.offset; v = Arrays.copyOfRange(originalValue, off, off+size); } ... }

5. Which two method you need to implement for key in hashMap ? (equals and hashcode) read How HashMap works in Java for detailed explanation. 6. Where does these two method comes in picture during get operation? See here How HashMap works in Java for detailed explanation. 7. How do you handle error condition while writing stored procedure or accessing stored procedure from java? Open for all, my friend didn't know the answer so he didn't mind telling me. 8. What is difference between Executor.submit() and Executer.execute() method ? (Former returns an object of Future which can be used to find result from worker thread)

@vinit Saini suggesed a very good point related to this core java interview question

There is a difference when looking at exception handling. If your tasks throws an exception and if it

was submitted with execute this exception will go to the uncaught exception handler (when you don't have provided one explicitly, the default one will just print the stack trace to System.err). If you submitted the task with submit any thrown exception, checked or not, is then part of the task's return status. For a task that was submitted with submit and that terminates with an exception, the Future.get will rethrow this exception, wrapped in an ExecutionException.
9. What is the difference between factory and abstract factory pattern? Open for all, he explains about factory pattern and how factory pattern saves maintenance time by encapsulating logic of object creation but didn't know exact answer

@Raj suggested

Abstract Factory provides one more level of abstraction. Consider different factories each extended from an Abstract Factory and responsible for creation of different hierarchies of objects based on the type of factory. E.g. AbstractFactory extended by AutomobileFactory, UserFactory, RoleFactory etc. Each individual factory would be responsible for creation of objects in that genre.
10. What is Singleton? is it better to make whole method synchronized or only critical section synchronized ? see my article 10 Interview questions on Singleton Pattern in Java 11. Can you write Critical section code for singleton? check here 10 Interview questions on Singleton Pattern in Java 12. Can you write code for iterating over hashmap in Java 4 and Java 5 ? Tricky one but he managed to write using while and for loop. 13. When do you override hashcode and equals() ? Whenever necessary especially if you want to do equality check or want to use your object as key in HashMap. check this for writing equals method correctly 5 tips on equals in Java 14. What will be the problem if you don't override hashcode() method ? You will not be able to recover your object from hash Map if that is used as key in HashMap. See here How HashMap works in Java for detailed explanation. 15. Is it better to synchronize critical section of getInstance() method or whole getInstance() method ? Answer is critical section because if we lock whole method than every time some one call this method will have to wait even though we are not creating any object) 16. What is the difference when String is gets created using literal or new() operator ? When we create string with new() its created in heap and not added into string pool while String created using literal are created in String pool itself which exists in Perm area of heap. 17. Does not overriding hashcode() method has any performance implication ? This is a good question and open to all , as per my knowledge a poor hashcode function will result in frequent collision in HashMap which eventually increase time for adding an object into Hash Map. 18. Whats wrong using HashMap in multithreaded environment? When get() method go to infinite loop ?
Another good question. His answer was during concurrent access and re-sizing.

Give a simplest way to find out the time a method takes for execution without using any profiling tool? this questions is suggested by @Mohit Read the system time just before the method is invoked and immediately after method returns.
19.

Take the time difference, which will give you the time taken by a method for execution. To put it in code long start = System.currentTimeMillis (); method (); long end = System.currentTimeMillis (); System.out.println (Time taken for execution is + (end start)); Remember that if the time taken for execution is too small, it might show that it is taking zero milliseconds for execution. Try it on a method which is big enough, in the sense the one which is doing considerable amout of processing

What is the difference between Synchronized Collection classes and Concurrent Collection Classes ? When to use what ?
Collections classes are heart of java API though I feel using them judiuously is an art.its my personal experience where I have improved performance by using ArrayList where legacy codes are unnecesarily used Vector etc. JDK 1.5 introduce some good concurrent collections which is highly efficient for high volume , low latency system. The synchronized collections classes, Hashtable and Vector, and the synchronized wrapper classes, Collections.synchronizedMap and Collections.synchronizedList, provide a basic conditionally thread-safe implementation of Map and List. However, several factors make them unsuitable for use in highly concurrent applications -- their single collection-wide lock is an impediment to scalability and it often becomes necessary to lock a collection for a considerable time during iteration to prevent ConcurrentModificationExceptions. The ConcurrentHashMap and CopyOnWriteArrayList implementations provide much higher concurrency while preserving thread safety, with some minor compromises in their promises to callers. ConcurrentHashMap and CopyOnWriteArrayList are not necessarily useful everywhere you might use HashMap or ArrayList, but are designed to optimize specific common situations. Many concurrent applications will benefit from their use. So what is the difference between hashtable and ConcurrentHashMap , both can be used in multithreaded environment but once the size of hashtable becomes considerable large performance degrade because for iteration it has to be locked for longer duration.

Since ConcurrentHashMap indroduced concept of segmentation , how large it becomes only certain part of it get locked to provide thread safety so many other readers can still access map without waiting for iteration to complete. In Summary ConcurrentHashMap only locked certain portion of Map while Hashtable lock full map while doing iteration. As always comments , suggestions are welcome.

You might also like