Just go through the video, I am pretty much sure you will learn a lot.
Through this blog, I share my experiences, Problems that me and my team faced and how we resolved them.
Apart from that Technical tips, tricks, Java Basics and also questions that are asked in interviews related to Java.
Now blog can be accessed from a direct URL www.java-espresso.in
Friday, May 20, 2011
Monday, May 16, 2011
Heap structure in JVM
Following are the parts in JVM heap:
JVM Heap details (Image from Oracle(SUN) itself):
Closer Look:
Young Space: Young space holds the newly created objects and can be tuned with the parameter -XX:NewRatio=<value>, for example, you set -XX:NewRatio=3 [ which means ratio of young:old generation=1:3 and since total heap=young+old=4. So you have set the young space as one fourth of the total Heap] . You can even decide the initial and max sizes of the young space with parameters NewSize and MaxNewSize
Eden Space: All the objects either created inside the methods or as class member variables are created in the Eden space. Space allocated to objects created inside the methods is reclaimed after the method execution is over. Some objects might live longer as reference pointing to them are class member variables. If the Eden space starts filling up then minor garbage collector runs and shifts the old objects to the Tenured Generation space so that space is available for creation of new objects in Eden space. Objects that might never move to tenured generation are
Survivor Ratio: it is the ratio of the Eden space to Survivor space in the Young space. This parameter can be controlled by property -XX:SurvivorRatio Suppose if the -XXSurvivorRatio=6, it means each survivor space is one eigth of the total Young space[total young space=2*1 (as there are 2 survivor spacee)+6]. If survivor space is too small to copy collections then survivor space will extend in the tenured area also.
Virtual Space-1: It is the difference between the parameters of young space i.e, MaxNewSize-NewSize (Young Space Max Size - Young space initially size)
Tenured Generation: It contains the objects which have survived minor collections and are aged enough to be in tenured area. Major GC runs in the tenured area [See Collection algos of GC for collection algorithms of GC].
Virtual Space-2: It is the difference between MaxHeapSize and IntialHeapSize(Virtual Space-2 = MaxHeapSize-IntialHeapSize)
Other Areas:
PermGen(Permanent Generation) Area: It is the area where class loading and unloading happens. it stores metadata about classes , methods,etc. Please find the metadata term elaborated below:
Other Areas:
Native Area:
Native area is for the internal operations of the JVM. JVM uses this area to load libraries and for intermediate code generation. We cannot size the native area but other areas can be sized to control it. We can calculate the native area as below:
ProcessSize(Total memory allocated to Heap and other areas)-HeapSize-MaxPermSize
Collection Algorithms of GC (as per JVM 1.3, there are new additions as well):
Other Related articles on this blog:
JVM Memory Structure
References (Only which are worth reading :) ):
- Young Generation
- Eden Space
- Survivor space
- Virtual space
- Tenured (Old) generation:
- Virtual Space
- Other Areas: Not a part of heap:
- PermGen Area
- Native Area
JVM Heap details (Image from Oracle(SUN) itself):
Closer Look:
Young Space: Young space holds the newly created objects and can be tuned with the parameter -XX:NewRatio=<value>, for example, you set -XX:NewRatio=3 [ which means ratio of young:old generation=1:3 and since total heap=young+old=4. So you have set the young space as one fourth of the total Heap] . You can even decide the initial and max sizes of the young space with parameters NewSize and MaxNewSize
Eden Space: All the objects either created inside the methods or as class member variables are created in the Eden space. Space allocated to objects created inside the methods is reclaimed after the method execution is over. Some objects might live longer as reference pointing to them are class member variables. If the Eden space starts filling up then minor garbage collector runs and shifts the old objects to the Tenured Generation space so that space is available for creation of new objects in Eden space. Objects that might never move to tenured generation are
- Objects created inside the loops
- Objects created inside the methods
Survivor Ratio: it is the ratio of the Eden space to Survivor space in the Young space. This parameter can be controlled by property -XX:SurvivorRatio Suppose if the -XXSurvivorRatio=6, it means each survivor space is one eigth of the total Young space[total young space=2*1 (as there are 2 survivor spacee)+6]. If survivor space is too small to copy collections then survivor space will extend in the tenured area also.
Virtual Space-1: It is the difference between the parameters of young space i.e, MaxNewSize-NewSize (Young Space Max Size - Young space initially size)
Tenured Generation: It contains the objects which have survived minor collections and are aged enough to be in tenured area. Major GC runs in the tenured area [See Collection algos of GC for collection algorithms of GC].
Virtual Space-2: It is the difference between MaxHeapSize and IntialHeapSize(Virtual Space-2 = MaxHeapSize-IntialHeapSize)
Other Areas:
PermGen(Permanent Generation) Area: It is the area where class loading and unloading happens. it stores metadata about classes , methods,etc. Please find the metadata term elaborated below:
- Constant pool information
- Array having references to methods
- Internal Objects (for exceptions, etc.)
- Information for compilers for optimization
- Methods of class
Other Areas:
Native Area:
Native area is for the internal operations of the JVM. JVM uses this area to load libraries and for intermediate code generation. We cannot size the native area but other areas can be sized to control it. We can calculate the native area as below:
ProcessSize(Total memory allocated to Heap and other areas)-HeapSize-MaxPermSize
Collection Algorithms of GC (as per JVM 1.3, there are new additions as well):
- Copying or Scavange : It is very efficient and used only for minor collections in Eden space as it requires significant footprint.
- Mark-compact : it is slower than above technique. It does not require extra memory. It is used in major collections.
- Incremental or train : this collector is used only if -Xincgc is passed on the command line. This is significantly slower than Mark-Compact
Other Related articles on this blog:
JVM Memory Structure
References (Only which are worth reading :) ):
Tuesday, May 10, 2011
Email validation with regular expression
Please find the code listing below:
import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class EmailTest { public static void main(String[] args) { ListvalidEmails = new ArrayList (); // valid emails validEmails.add("dharam@gmail.com"); validEmails.add("dharam.singh@gmail.com"); validEmails.add("dharam.sing_chahar@gmail.com"); validEmails.add("1985dharam@gmail.com"); validEmails.add("1985dharam@gmail.com"); validEmails.add("dharam@gmail-youtube.co.in"); for (String string : validEmails) { System.out.println("Email: "+string+" is "+isEmail(string)); } System.out.println(); // invalid emails List inValidEmails = new ArrayList (); inValidEmails.add("@dharam@gmail.com"); inValidEmails.add("dharam.singh@gmail."); inValidEmails.add("dharam.sing_chahar@gmail"); inValidEmails.add("1985dharam-gmail.com"); inValidEmails.add("#####@gmail.com"); inValidEmails.add("dharam@gmail-youtube.co.in_uk"); for (String string : inValidEmails) { System.out.println("Email: "+string+" is "+isEmail(string)); } } public static String isEmail(String email) { // Set the email pattern string /* * Email must start with either A-Z or a-z or a number Before @ sign any * number of dots can come */ Pattern p = Pattern .compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[-A-Za-z0-9]+(\\.[A-Za-z]{1,})+$"); // Match the given string with the pattern Matcher m = p.matcher(email); // check whether match is found return m.matches() ? "valid" : "invalid"; } }
Friday, May 6, 2011
Finding the position in an array where right side sum is equal to left side sum
Code is pretty much simple.
So here is the code for the problem:
So here is the code for the problem:
/** * @author Dharmvir Singh * This program finds the position in an array where sum of left * side is equal to sum of right side excluding the position. If * position doesn't exist return -1. */ public class FindPosition { public static void main(String[] args) { // int a[] = { 1, 2, 3, 2, 1 }; int a[] = { -1, 2, -3, 6, -5 }; int sumLeft = a[0], flag = 0, sumRight = 0; int arrayLength = a.length; // Assume that 1st position is the desired position and so finding the // right side sum for (int i = 2; i < arrayLength; i++) { sumRight += a[i]; } /* * Now we will check if our assumption is correct that if a[1] position * and if not assume that a[2] is the correct one and so on.. last * position to check will be (a.length-1) */ int i; for (i = 1; i < (arrayLength - 1); i++) { if (sumLeft == sumRight) { break; } sumLeft += a[i]; sumRight -= a[i + 1]; } if (i < arrayLength - 1) { System.out.println("Pos is " + i + " sum is: " + sumLeft); } else { System.out.println("No post"); } } }Related Posts
- Tricky Java question: finding the missing number
- Checking if i=4 or 5 or 6 in single if condition without using logical operators
Happy Coding.
Subscribe to:
Posts (Atom)