Saturday, September 27, 2014

Java Memory Overview - Java Heap Size, Perm Gen Size and Java Stack Size.

             There are 2 quite common ‘Out of Memory’ issues. The 1st one is ‘Heap Size’ and the 2nd one is ‘PermGen Space’.For Java developer, knowledge of Heap in Java, setting size of java heap /PermGen space , dealing with java heap space OutOfMemoryError, analyzing heap dumps is very important.

Heap Space :

When a Java program starts, Java Virtual Machine gets some memory from Operating System. Java Virtual Machine or JVM uses this memory for all its need and part of this memory is call java heap memory. Heap in Java generally located at bottom of address space and move upwards. whenever we create object using new operator or by any another means object is allocated memory from Heap and When object dies or garbage collected ,memory goes back to Heap space in Java

For a heavy Java process, insufficient Heap size will cause the popular java.lang.OutOfMemoryError: Java heap space.

-Xms<size> - Set initial Java heap size
-Xmx<size> - Set maximum Java heap size
 
java -Xms512m -Xmx1024m HelloProgram

Perm Gen Space :

PermGen Space  is used to store classes and Meta data about classes in Java. When a class is loaded by a classloader it get stored in PermGen space, it gets unloaded only when the classloader which loaded this class got garbage collected. If any object retains reference of classloader than its not garbage collected and Perm Gen Space is not freed up. This causes memory leak in PermGen Space and eventually cause java.lang.OutOfMemoryError: PermGen space. Another important point is that when you deploy your web application a new Clasloader gets created and it loads the classes used by web application. So if Classloader doesn't get garbage collected when your web application stops you will have memory leak in tomcat.

As a performance optimization the permanent generation was created and classes were put into it.Classes are part of our JVM implementation and we should not fill up the Java heap with our data structures. Permanent Generation is allocated outside the heap size. The permanent Generation contains the following class information:

    Methods of a class.
    Names of the classes.
    Constants pool information.
    Object arrays and type arrays associated with a class.
    Internal objects used by JVM.
    Information used for optimization by the compilers.

If a large code-base project is loaded, the insufficient Perm Gen size will cause the popular Java.Lang.OutOfMemoryError: PermGen.

‘Java.Lang.OutOfMemoryError: PermGen Space’ occurs when JVM needs to load the definition of a new class and there is no enough space in PermGen. The default PermGen Space allocated is 64 MB for server mode and 32 MB for client mode. There could be 2 reasons why PermGen Space issue occurs.

The 1st reason could be your application or your server has too many classes and the existing PermGen Space is not able to accommodate all the classes.


-XX:PermSize<size> - Set initial PermGen Size.
-XX:MaxPermSize<size> - Set the maximum PermGen Size.

java -XX:PermSize=64m -XX:MaxPermSize=128m HelloProgram

And the 2nd reason could be memory leak. How the class definitions that are loaded could can become unused.

Normally in Java, classes are forever. So once the classes are loaded, they stay in memory even if that application is stopped on the server. Dynamic class generation libraries like cglib use lot of PermGen Space since they create a lot of classes dynamically. Heavy use of Proxy classes, which are created synthetically during runtime. It’s easy to create new Proxy classes when a single class definition could be reused for multiple instances.

Spring and Hibernate often makes proxies of certain classes. Such proxy classes are loaded by a classloader. The generated class definitions are never discarded causing the permanent heap space to fill up fast.

Use Profiler and Heap dump Analyzer tool to understand Java Heap space and how much memory is allocated to each object. Java Heap dump is a snapshot of Java Heap Memory at a particular time. This is very useful to analyze or troubleshoot any memory leak in Java or any Java.lang.OutOfMemoryError. You can take heap dump using tool like VisualVm mentioned at the end of the post.

For PermGen space issues, you will need identify the cause of leak and fix it. Increasing the PermGen space will not help, it will only delay the issue, since at some point the PermGen space will still be filled up.

If you are using Tomcat and are haunted by memory leaks, the latest versions of Tomcat have capability to fix the some of the memory leak issues.


Java Stack Size
:

Each thread has its own stack,. primitive types and references live on stack. No object can live on stack. If a project has a lot of threads processing, try reduce this stack size to avoid running out of memory.

-Xss = set java thread stack size

java -Xss512k HelloProgram


JAVA_OPTS To Set vm Variables:

set JAVA_OPTS = -Xss512k -Xms512m -Xmx1024m -XX:PermSize=512m -XX:MaxPermSize=1024m

 
Tool for Troubleshooting :

VisualVm :

VisualVM is a visual tool integrating several command line JDK tools(like stat, jinfo, jstack, and jmap etc) and lightweight profiling capabilities. Designed for both production and development time use, it further enhances the capability of monitoring and performance analysis for the Java SE platform.

Download Link :
http://visualvm.java.net/download.html


Sunday, September 21, 2014

Difference between List and Set in Java Collection

         Both List and Set are two of most important Collection classes Java Program use along with various Map implementation. Basic feature of List and Set are abstracted in List and Set interface in Java and then various implementation of List and Set adds specific feature on top of that e.g. ArrayList in Java is a List implementation backed by Array while LinkedList is another List implementation which works like linked list data-structure.

List vs Set in Java

  1. Fundamental difference between List and Set in Java is allowing duplicate elements. List in Java allows duplicates while Set doesn't allow any duplicate. If you insert duplicate in Set it will replace the older value. Any implementation of Set in Java will only contains unique elements.

  2. List allows null elements and you can have many null objects in a List, because it also allowed duplicates. Set just allow one null element as there is no duplicate permitted.

  3.  Another significant difference between List and Set in Java is order. List is an Ordered Collection while Set is an unordered Collection. List maintains insertion order of elements, means any element which is inserted before will go on lower index than any element which is inserted after. Set in Java doesn't  maintain any order. Though Set provide another alternative called SortedSet which can store Set elements in specific Sorting order defined by Comparable and Comparator methods of Objects stored in Set.Set uses equals() method to check uniqueness of elements stored in Set, while SortedSet uses compareTo() method to implement natural sorting order of elements. In order for an element to behave properly in Set and SortedSet, equals and compareTo must be consistent to each other.
  4. Popular implementation of List interface in Java includes ArrayList, Vector and LinkedList. While popular implementation of Set interface includes HashSet, TreeSet and LinkedHashSet.

When to use List and Set in Java

Another good follow-up question is "when do you use List and Set in Java" , which can also be answered based on properties of List and Set we have learn here.These difference between Set and List also teaches us when to use Set and when to prefer List. its pretty clear that if you need to maintain insertion order or object and you collection can contain duplicates than List is a way to go. On the other hand if your requirement is to maintain unique collection without any duplicates than Set is the way to go.
Important point to note is that both List and Set are derived from Collection Interface. In short main difference between List and Set in Java is that List is an ordered collection which allows duplicates while Set is an unordered collection which doesn't allow duplicates.

Monday, September 15, 2014

Releasing a Project Using Maven Release Plugin

Maven Release Plugin

  • This plugin is used to release a project with Maven, saving a lot of repetitive, manual work. Releasing a project is made in two steps: prepare and perform.

Requriements:

1)  Current version specified in your projects pom.xml should be a SNAPSHOT version
 eg. 1.1-SNAPSHOT

2) your projects pom.xml should contain the scm connection settings specified inside scm tag (maven-scm-plugin)
eg.
<scm>
        <connection>scm:svn:https://your.projects.svn.path</connection>
        <developerConnection>scm:svn:https://
your.projects.svn.path</developerConnection>
    </scm>

"https://your.projects.svn.paththis is the path where maven checkins the next developemnt version
3) your pom.xml must contain repositories configuration under distributionManagement tag.
eg.
<distributionManagement>
          <repository>
            <id>releases</id>
            <name>Releases</name>
            <url>http://your.server.path/nexus/content/repositories/releases/</url>
            <layout>default</layout>
          </repository>
          <snapshotRepository>
            <id>snapshots</id>
            <name>Snapshots</name>
            <url>http://your.server.path/nexus/content/repositories/snapshots/</url>
            <layout>default</layout>
          </snapshotRepository>
    </distributionManagement>

4) your maven's settings.xml must contain the required server configuration
eg.

<servers>
       <server>
            <id>releases</id>
            <username>yourusername</username>
            <password>yourpassword</password>
        </server>
        <server>
            <id>snapshots</id>
            <username>yourusername</username>
            <password>yourpassword</password>
        </server>
        </servers>

Steps to follow :

  1. mvn release:prepare -DdryRun=true -DautoVersionSubmodules=true -P "production" -DtagBase=https://your.svn.tagbase.path

    "https://your.svn.tagbase.path" this is the path where branch/tag is created for the current release, which is than checkout in target folder and the same is uploaded on nexus during perform step.
  2. mvn release:perform -DdryRun=true -P "production"
  3. mvn release:clean
  4. mvn release:prepare -DautoVersionSubmodules=true -P "production" -DtagBase=https://your.svn.tagbase.path
  5. mvn release:perform -P "production"
  6. Dependent project POM files should refer new SNAPSHOT version.
  7. If problem occurred while performing common release than execute the below commands for rollback.

    a) mvn release:rollback

    To rollback a release, the following requirement must be met:
    • You haven't run release:clean on the project. This means that the backup files and the release descriptor from the previous release command still exists.
    When a release is rolled back, the following release phases are executed:
    • All project POMs are reverted back to their pre-release state locally, and also in the SCM if the previous release command was able to successfully make changes in the SCM to the POMs. This is done by using the backup files created during release:prepare.
    Currently we have to manually delete the branch/tag from scm.

    b) mvn release:clean
    
    Cleaning a release goes through the following release phases:
    • Delete the release descriptor (release.properties)
    • Delete any backup POM files

 

Reference : http://maven.apache.org/maven-release/maven-release-plugin/index.html

Saturday, August 23, 2014

Strategy Design Pattern

1) Create interface

interface SectionRemovalRule {

        void runRule(long userId) ;
    }

2) Create hashmap

private static Map<String, SectionRemovalRule> sectionRemovalMethodMap = new HashMap<String, SectionRemovalRule>();

3) Initialize map

 @PostConstruct
    void initSectionRemovalMethodMap() {

        sectionRemovalMethodMap.put("section1",
                new SectionRemovalRule() {
                    @Override
                    public void runRule(long sectionId)  {
                        section1Dao
                                .removeSection(sectionId);
                    }
                });

        sectionRemovalMethodMap.put("section2",
                new SectionRemovalRule() {
                 @Override
                    public void runRule(long sectionId)  {
                        section2Dao
                                .removeSection(sectionId);
                    }
                });
       
    }

4) Calling the method 

sectionRemovalMethodMap.get('yourSectionName').runRule(sectionId);

5) Benefits :

* It helps to avoid if else
* it makes code simple.
* increases performance

Spring Login Authentication

1) Configure password encoder bean 

<bean id="passwordEncoder" class="org.springframework.security.authentication.encoding.Md5PasswordEncoder"/>

2) configure salt source bean (optional)

  <beans:bean id="saltSource" class="org.springframework.security.authentication.dao.ReflectionSaltSource"
        p:userPropertyToUse="username"/>

3) Configure rememberme bean

<beans:bean id="rememberMeAuthenticationProvider"
            class="org.springframework.security.authentication.RememberMeAuthenticationProvider">
    <beans:constructor-arg value="e37f4b31-0c45-11dd-bd0b-0800200c9a66"/>
</beans:bean>


4) Configure authentication providers(list)

 <authentication-manager alias="authenticationManager">
    <authentication-provider ref="rememberMeAuthenticationProvider">
        </authentication-provider>
        <authentication-provider user-service-ref="userDao">
            <password-encoder ref="passwordEncoder" >
                <salt-source ref="saltSource" />
            </password-encoder>
        </authentication-provider>
        <authentication-provider ref="customAuthenticationProvider" />
    </authentication-manager>

5) Create class that implements userDao inteface and implement loadUserByUsername method

@Repository("userDao")
public class UserDaoHibernate extends GenericDaoHibernate<User, Long> implements
        UserDao, org.springframework.security.core.userdetails.UserDetailsService.UserDetailsService {

  UserDetails loadUserByUsername(String username) throws UsernameNotFoundException
{}

}

6) Create custom authentication provider 

@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {

@Override
    public Authentication authenticate(Authentication authentication)
            throws AuthenticationException {

        String username = authentication.getName();
        String rawPassword = (String) authentication.getCredentials();

        User user = (User) userDao.loadUserByUsername(username);

        if (user == null) {
            throw new BadCredentialsException("Username not found.");
        }

        if (!passwordEncoder.isPasswordValid(encryptedPassword, rawPassword,
                saltField)) {
            throw new BadCredentialsException("Wrong password.");
        }

  
        Collection<? extends GrantedAuthority> authorities = user
                .getAuthorities();

        return new UsernamePasswordAuthenticationToken(user,
                user.getPassword(), authorities);
    }

  
    @Override
    public boolean supports(Class<?> arg0) {
            return true;
    }

}

7) Configure remember me filter for changing cookie name 

<beans:bean id="rememberMeServices"       class="org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices">
    <beans:constructor-arg value="e37f4b31-0c45-11dd-bd0b-0800200c9a66"/>
    <beans:constructor-arg ref="userDao"/>
    <beans:property name="cookieName" value="_SRM"/>
    </beans:bean>

<beans:bean id="rememberMeFilter"            class="org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter">
    <beans:constructor-arg ref="authenticationManager"/>
    <beans:constructor-arg ref="rememberMeServices"/>
</beans:bean>

Refer document for more information :  spring-security-document

Must read articles