Google

May 31, 2012

Top 5 Eclipse debugging tips tutorial style

Good debugging skills can not only make you more productive, but also can be used in job interviews to determine your experience. The remote debugging was already discussed using eclipse, which is very handy if you have a scenario where you can't reproduce locally what your client is experiencing. In this case, you can set up the remote debugger using eclipse. In this blog I will go through some handy debugging options using eclipse IDE. Similar, options are available in other popular IDEs as well.


Q1. How will you go about getting the debugger to stop on NullPointerException, which is thrown on line E in the code  below?


import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MyClassWithACollection {
 
 //map instance variable
 private Map<integer, Country> fromMap = 
          new ConcurrentHashMap<integer, Country>(10);
 //class variable 
 private static Map<integer, Country> toMap = 
           new ConcurrentHashMap<integer, Country>(10);
 

 public static void main(String[] args) {
  MyClassWithACollection mcc = new MyClassWithACollection(); //Line X
  mcc.fromMap.put(1, new Country("India"));                  //Line Y 
  mcc.fromMap.put(2, new Country("UK"));
  mcc.fromMap.put(3, new Country("USA"));
  System.out.println(mcc.fromMap);          //Line A
  
  //copy from fromMap to toMap
  mcc.toMap.putAll(mcc.fromMap);            //Line B
  System.out.println(mcc.toMap);            //Line C
  
  mcc.fromMap = null;                       //Line D 
  //throws a null pointer exception
  System.out.println(mcc.fromMap.values()); //Line E
 
 }
 
 //inner class
 public static class Country {
  String name;
  
  public Country(String name){
     this.name = name;
  }
 }

}
 
A1. You can get the debugger to stop at a particular exception as ashown below.

1. go to the debug window
2. click on j! icon that brings up the modal panel to search and select the exception to set the break point on



3. Select the exception type.
4. Click okay.
5. Select the "MyClassWithACollection" file and right click to get the contextual menu and and then select Debug As --> Java Application as shown below, which will stop the debugger on Line E, where the NullPointerException will be thrown as mcc.fromMap was set to null on Line D.


 Note: For all the Q&A below, you need to use the above Debug As --> Java Application.

Q2. If you want to inspect the contents of "mcc.fromMap" on Line A, how will you go about accomplishing it?
A2. In eclipse you could follow the following steps

1. Set a break point on "Line A" by double clicking on the left hand side of the eclipse editor as shown below to set the break point.



2. When you run the program in debug mode as explained  earlier, the debugger will stop on the break point. 3. Go to the variables tab, and then make sure the "Logical Structure" icon shown in the diagram below is turned on by clicking it as shown below.



4. When the logical structure is turned on, you can easily drill into the "mcc.fromMap" as shown above to inspect the values. This approach can be used for all collection types.

Q3. How will you inspect the contents of the static variable "mcc" on Line X oand contents of the static variable "mcc.toMap" on Line C?
A3. To inspect a local variable "mcc" on Line X

1. Put the break point on Line Y.
2. When it stops on Line Y, highlight mcc on Line X, and then right click to get the contextual menu as shown below.



3. You could then select "Inspect" to get a pop up that inspects the variable as shown below.



4. Alternatively, you could select watch from the contextual menu which will display the local and static variables. You could even add expressions to stop when a variable has a particular value like null.



You can even edit the watch with expressions as shown below.




5. As you step through one line at a time, you can see the watch values changing.

Q4. Since Line B is a method from the Java API, how will you get eclipse to not step into the break point on Line B even if you press F5 (step into), and go to the next break point?

A4. You can achieve this via setting up the "Step Filters" in eclipse preferences.

1. Go to Window --> Preferences and you will get a modal popup window.
2. Follow the rest of the steps as shown below.



3. If you try debugging now, the debugger will stop at Line B, but if you press step into or F5, the code will not step into the putAll method, and step over to the next break point instead.

The best way to learn is by trying the above code.

Related posts: 

Labels:

May 29, 2012

Why must you write a technical blog?

Do you have to be an expert in a technical area to write a technical blog? The answer is no. You write a technical blog for a number of reasons.

Firstly, for your own benefit

  • To capture your experience and learning so that it will be useful to you in your future endeavors. I use my blogs and books to jog my memory prior to my job interviews. I also use my blog as a reference guide in my regular job. These 650 job interview questions are something I collected over 12 years from my own and others' experience. When you create a blog post, you could keep it private for your own use, until you are ready to share it publicly. Information gathering takes time, so better start now.
  • Writing blog posts isn’t easy. Most posts require lots of thinking, researching, trying out the code, scratching your head when they don’t work right, etc. So, it not only widens your technical skills, but also improves your writing and communication skills. By blogging, you will learn what related topics that you need to learn and identify where your understanding is shallow. It is very handy when you are feeling stagnated to motivate you to self learn.
  • In addition to being the valuable part of your learning process, it helps you network, show off your skills and earn a very small passive income. Employers are increasingly inquisitive about your online presence and good blogs can open more doors in terms of better job opportunities and other collaborative endeavors. Blogging shows that you are passionate about your chosen field. An attribute which most employers value.
  • Venting your frustrations diplomatically and professionally through blogs as to what not to do or how not to do things as opposed to writing inappropriate comments like "this code is rubbish" in code base or sending emails like "ashamed to work with this code". I have seen very technically talented professionals being sacked or overlooked for potential promotions due to inappropriately venting their frustrations. Your blogs could also invite different perspectives and solutions to your frustration or problem from the fellow professionals and followers. For example, you could take the view that if you are paid $50K to do a job then you could think that you are paid 25K to add value and the remaining 25K to put up with things you have no control over and get things done to the best you can.


Your own learning process is a great justification in itself for writing a blog, but good writing and communication skills require you to target the intended audience. This helps you network with the fellow professionals and learn from their experience, comments and feedback. Blogging has helped me handle criticisms well. When you write technical blogs, people who are more nerdy than you are going to read it. Technology is a very vast area, and you cannot be good at everything. Here are a few tips as to what you can blog about
  • Write about the problems you faced about any one of the 16 key areas, technology, or framework for which you have not found good solution on the internet.
  • A step by step guide to getting a task done for which you have not found any simple and clear guide with examples on the internet.
  • If you are interested in writing about what you had learned last week or what you wished had learned a year ago, then think a little differently and explain it with a liitle bit of imagination to make it unique from 100 other articles or blogs that can be found on the internet. I learned JavaScript by blogging. Read around 20+ online articles and then blogged it in my own style.  I am still an amateur when it comes to JavaScript as I don't use it regularly at work, but when I want to brush up my I knowledge, I know where to look. 
  • Blog readers have too many other stuff to read, so your blog entries need to be precise and concise.
  • Take part in industry specific forums to help others. Others' problems and needs may give you your next topic to blog on.
  • Anything else that you find interesting and would like to share with others in your own style.
So, don't wait and get started. Write something you are passionate about. Write it because you feel it should be written as opposed to worrying about it will be read or criticized. So, blogging is not for the experts, but for everyone who is passionate about something. 


Think --> research --> Write --> Learn --> get feedback --> handle criticism --> grow --> succeed


Finally, to take what you need

If you had given what others need in the last step to retain followers and get enough hits, you will get what you need in terms of recognition to open more doors and earn additional income by promoting your own and others' books (both eBooks and printed), products, and services via your blogs and websites. There are so many tips out there on getting enough hits to your blog, the best 2 tips I can give you from my experience is that



Secondly, to give what your target audience need




Things to watch out
  • Blogging can be quite addictive, and it is imperative to get your priorities right.
  • It can be time consuming. So, it requires lots of patience. "A journey of a thousand miles begins with a single step" -- by Lao tzu
  • Do it professionally. Support and network with the other bloggers.
How to get started
  • Pick a blogger tool by researching online. The wordpress.com and blogger.com are worthy contenders. 
  • Register your blog with javablogs.com so that your blogs can be discovered by others.
  • Start writing quality blogs.
  • Promote your blogs via forums by providing the link to your blog as your signature, publishing articles via java.dzone.com, which lets you have your web address , and adding social media widgets to your blog for people to network with you.
  • You can add your blog url to your resume and relevant email correspondences.
  • Use the Google keyword tool to target your blog keywords.
  • Use Google  analytics to analyze your blog traffic. 
  • Once you are in it, you will find lots of other SEO tips on Google.

 Here are other similar posts that might interest you



Labels:

May 23, 2012

Spring and Hibernate simple JDBC example

Q. How would you go about using Spring and Hibernate frameworks to make JDBC calls?
A.

STEP 1: Firstly, define the environment specific data in a properties file named datastore.properties with datasource details

jdbc.driver=com.sybase.jdbc3.jdbc.SybDriver
jdbc.url=jdbc:sybase:Tds:serverName:2000/my_database
jdbc.username=user
jdbc.password=password

STEP 2: Define the spring context file (myAppContext.xml) to wire up the data source and data access objects

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"
 xmlns:batch="http://www.springframework.org/schema/batch"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
   http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
   http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.0.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

 

 <!-- STEP1: read configuration data  from .properties file           -->
 <context:property-placeholder location="classpath:datastore.properties" />
 
 <!-- STEP2: Data Source                     -->
 <bean id="dataSourceMyDs" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="${jdbc.driver}" />
  <property name="url" value="${jdbc.url}" />
  <property name="username" value="${jdbc.username}" />
  <property name="password" value="${jdbc.password}" />
 </bean>
 
 <!-- STEP3: Hibernate Properties like dialect, showSql, etc  -->
 <bean id="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  <property name="properties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.SybaseDialect</prop>
    <prop key="hibernate.generate_statistics">false</prop>
    <prop key="hibernate.hbm2ddl.auto">verify</prop>
    <prop key="hibernate.jdbc.batch_size">50</prop>
    <prop key="hibernate.show_sql">false</prop>
    <prop key="hibernate.format_sql">false</prop>
    <prop key="hibernate.cache.use_query_cache">false</prop>
    <prop key="hibernate.cache.use_second_level_cache">false</prop>
    <prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
   </props>
  </property>
  <property name="location">
   <value>classpath:/hibernate.properties</value>
  </property>
 </bean>
 
 <!-- Hibernate annotated classes for ORM -->
 <bean id="hibernateAnnotatedClasses" class="org.springframework.beans.factory.config.ListFactoryBean">
        <property name="sourceList">
            <list>
                <value>com.myapp.domain.model.Account</value>
                <
            </list>
        </property>
    </bean>
 
 <!-- STEP 4: define the hibernate session factory that makes use of the dataSourceMyDs & hibernateProperties defined in step 2 & 3 respectively-->
 <bean id="hibernateSessionFactoryMpApp"
  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  <property name="dataSource" ref="dataSourceMyDs" />
  <property name="hibernateProperties">
   <ref bean="hibernateProperties" />
  </property>
  <property name="annotatedClasses">
   <list>
   </list>
  </property>
  <property name="annotatedPackages">
   <list></list>
  </property>
 </bean>
 
 <!-- STEP 5:  DAO Template that uses the hibernateSessionFactoryMpApp for direct JDBC and ORM calls-->
 <bean id="daoTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
  <constructor-arg index="0" ref="hibernateSessionFactoryMpApp" />
  <constructor-arg index="1" value="true" />
 </bean>
 
 <!-- STEP 6:  DAO Template gets injected into a DAO class -->
    
    <bean id="accountDao" class="com.myapp.dao.impl.AccountDaoImpl">
        <property name="dataSource" ref="dataSourceMyDs" />
    </bean>
    
    <!--  If you want to use HibernateTemplate -->
    <bean id="accountHibDao" class="com.myapp.dao.impl.AccountHibDaoImpl">
        <constructor-arg ref="daoTemplate" />
    </bean>
 
</beans>


The above config shows HibernateTemplate configuration as well, but the ensuing steps focuses on using the data source and the JdbcTemplate.

STEP 3: Define the interface for AccountDaoImpl.

package com.myapp.dao.impl;

public interface AccountDao {

    Account getAccount(String accountCode);

}

STEP 4: Define the implementations. The value object (i.e. POJO - Plain Old Java Object) class to map the relational data.

public class Account {
    
 private String accountCode;
 private String accountName;

 //getters & setters
    ....
}

The row mapper class that maps relational data to the value object (i.e a POJO)

import java.sql.ResultSet;
import java.sql.SQLException;

public class AccountRowMapper implements RowMapper {

   public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
  Account account = new Account();
  account.setAccountCode(rs.getString("account_code"));
  account.setAccountName(rs.getString(account_name"));
  return account;
 }

}

STEP 5: The data access object implementation that makes use of the POJO and the row mapper classes.

package com.myapp.dao.impl;

public class AccountDaoImpl implements AccountDao {

    private JdbcTemplate jdbcTemplate;
    
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    } 
 
   
 
 public Account getAccount(String accountCode) {
 
     String sql =  "Select account_code, account_name from account where account_code = ?"
  Account account = jdbcTemplate.queryForObject(sql, new Object[]{accountCode}, new AccountRowMapper());
  return account;
 }

}

STEP 6: Finally, you can invoke the data access object from your service or processor classes. Here is a simple class
for demo.

public class MyService {

   public static void main(String[] args) throws ClassNotFoundException {
      ApplicationContext applicationContext = new ClassPathXmlApplicationContext("myAppContext.xml");
      AccountDao dao = applicationContext.getBean("accountDao", AccountDao.class);
      Account account = dao.getAccount("1234");
   //do something with the account
   }
}

The JdbcTemplate class comes with many useful overloaded query methods and options to map data.



Q. If you are running your application on an application server, how would you define your datasource properties?
A. The data source will be configured via the application server and the spring config file myAppContext.xml will look up via JNDI as shown below

  <bean id="dataSourceMyDs" class="org.springframework.jndi.JndiObjectFactoryBean" scope="singleton">
  <property name="jndiName">
   <value>jdbc.dataSource.my_app</value>
  </property>
  </bean>
  
Q. How will you go about defining the dependency jars?
A. You need the following framework libraries (Spring and Hibernate) in addition to Java, and the
dependency can be configured via maven pom.xml file.

 <!-- Spring framework -->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring</artifactId>
    <version>3.0.5.RELEASE</version>
    </dependency>
 
    <!-- Spring AOP dependency -->
    <dependency>
        <groupId>cglib</groupId>
  <artifactId>cglib</artifactId>
  <version>2.2</version>
 </dependency>
 
    <!-- Sybase database driver -->
 <dependency>
  <groupId>com.sybase</groupId>
  <artifactId>jconn3</artifactId>
  <version>6.0</version>
 </dependency>
 
 <!-- Hibernate framework -->
 <dependency>
  <groupId>hibernate</groupId>
  <artifactId>hibernate3</artifactId>
  <version>3.2.3.GA</version>
 </dependency>
 
 
 <!-- Hibernate library dependecy start -->
 <dependency>
  <groupId>dom4j</groupId>
  <artifactId>dom4j</artifactId>
  <version>1.6.1</version>
 </dependency>
 
 <dependency>
  <groupId>commons-logging</groupId>
  <artifactId>commons-logging</artifactId>
  <version>1.1.1</version>
 </dependency>
 
 <dependency>
  <groupId>commons-collections</groupId>
  <artifactId>commons-collections</artifactId>
  <version>3.2.1</version>
 </dependency>
 
 <dependency>
  <groupId>antlr</groupId>
  <artifactId>antlr</artifactId>
  <version>2.7.7</version>
 </dependency> 
 

JDBC, Spring, and Hibernate tutorials

Labels: ,

May 17, 2012

Apply the philosophy of give before you take for career success in software engineering




"Give what others need before you take what you need"

Many of you would have heard this philosophy before, but how many of you had consciously applied this? Whenever you get frustrated with questions like why am I not getting enough job interview calls?, why am I not getting any job offers?, why am I not getting promoted?, why is my blog not getting enough traction?, etc apply the above philosophy. To give you more examples,


At your job give your employers what they need in terms of getting things done, adding value to the business, providing solutions to business and technical problems, becoming a go to person, taking on more responsibilities, and staying visible as a great contributor before you take what you need in terms of promotions and salary increase.

In your  resume or CV give your prospective recruiters a quick snapshot of what they need --
  •     Quantified accomplishments
  •     Experience and capabilities in sought-after technologies, frameworks, and tools
  •     Well roundness to get the job done with good technical skills, soft skills and right attitude
before you be in a position to take multiple job interviews. Have customized resumes tailored to needs of individual employer.


When attending job interviews give your interviewers what they are looking for
  •    The basics (things you must know)
  •    Ability to get the job done
  •    Passion for the chosen field and real interest in the job and the organization
  •    Fit in well with the team
through good prior preparation before you take the enviable position to be able to choose from multiple job offers. Ask yourself a few questions like
  •   What can I give to the prospective employers that many others can't?
  •   What skills, experience, and capabilities can I marry up with their requirements?

When blogging, give your blog readers what they need in terms of
  •    steps required to getting a task done or solving a problem
  •    potential pitfalls to avoid
  •    information to improving the technical knowledge or awareness
  •    inspiration to progress in their careers
  
before you take what you need -- recognition, networking, and monetary benefits. If providing content that is readily available elsewhere, combine it with some creative ideas, so that it will be easy for your readers to grasp. Before you start blogging ask yourself the following questions
  •    What can you offer others that no one else can?
  •    Are you willing to invest time and commitment?

If you are building  an application for individuals (e.g. mobile apps, games, etc) or business users give what really your target audience need in terms of

  •    What problems does it solve?
  •    What benefits does it provide?
  •    Are there any similar applications and what am I competing against?
  
before you take what you need in terms of revenue, promotion, and recognition.


Very often people give what they need without the right skill or know how to put themselves in others' shoes. So, apply this philosophy in your future endeavors as you see fit, and avoid frustrations and open more doors for your success.

Labels:

May 16, 2012

Memory leak in Java



Q. How will you go about creating a memory leak in Java?
A. In Java, memory leaks are possible under a number of scenarios. Here is a typical example where hashCode( ) and equals( ) methods are not implemented for the Key class that is used to store key/value pairs in a HashMap. This will end up creating a large number of duplicate objects. All memory leaks in Java end up with java.lang.OutOfMemoryError, and it is a matter of time. The following code agressively creates the OutOfMemoryError via an endless loop for demonstration purpose.

If you are not familiar with the significance of equals( ) and hashCode ( ) methods in Java learn how to define proper key class in Java.

import java.util.HashMap;
import java.util.Map;

public class MemoryLeak {

 public static void main(String[] args) {
  Map<Key, String> map = new HashMap<Key, String>(1000);
  
  int counter = 0;
  while (true) {
       // creates duplicate objects due to bad Key class
   map.put(new Key("dummyKey"), "value");
   counter++;
   if (counter % 1000 == 0) {
    System.out.println("map size: " + map.size());
    System.out.println("Free memory after count " + counter
      + " is " + getFreeMemory() + "MB");
      
    sleep(1000);
   }
   
   
  }
 }

 // inner class key without hashcode() or equals() -- bad implementation
 static class Key {
  private String key;

  public Key(String key) {
   this.key = key;
  }

 }

 //delay for a given period in milli seconds
 public static void sleep(long sleepFor) {
  try {
   Thread.sleep(sleepFor);
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
 }

 //get available memory in MB
 public static long getFreeMemory() {
  return Runtime.getRuntime().freeMemory() / (1024 * 1024);
 }

}

If you run the above code, you will get the ouput as shown below

map size: 1000
Free memory after count 1000 is 4MB
map size: 2000
Free memory after count 2000 is 4MB
map size: 1396000
Free memory after count 1396000 is 2MB
map size: 1397000
Free memory after count 1397000 is 2MB
map size: 1398000
Free memory after count 1398000 is 2MB
map size: 1399000
Free memory after count 1399000 is 1MB
map size: 1400000
Free memory after count 1400000 is 1MB
map size: 1401000
Free memory after count 1401000 is 1MB
.....
.....
map size: 1452000
Free memory after count 1452000 is 0MB
map size: 1453000
Free memory after count 1453000 is 0MB
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
 at java.util.HashMap.addEntry(HashMap.java:753)
 at java.util.HashMap.put(HashMap.java:385)
 at MemoryLeak.main(MemoryLeak.java:10)


As you could see, the size of the map keeps growing with the same objects and the available memory keeps coming down from 4MB to 0MB. At the end, the program dies with an OutOfMemoryError.




Q. How will you fix the above memory leak?
A. By providing proper implentation for the key class as shown below with the equals() and hashCode() methods.

    .....
 static class Key {
  private String key;

  public Key(String key) {
   this.key = key;
  }

 
  @Override
  public boolean equals(Object obj) {

   if (obj instanceof Key)
    return key.equals(((Key) obj).key);
   else
    return false;

  }

  @Override
  public int hashCode() {
   return key.hashCode();
  }
 }
 .....
 
If you rerun it after making the fix shown above to the MemoryLeak class, you will get an output as shown below. The program runs endlessly, and creates only one object in the HashMap.

map size: 1
Free memory after count 1000 is 4MB
map size: 1
Free memory after count 2000 is 4MB
map size: 1
Free memory after count 3000 is 4MB
map size: 1
Free memory after count 4000 is 4MB
...
Free memory after count 73000 is 4MB
map size: 1
Free memory after count 74000 is 4MB
map size: 1
Free memory after count 75000 is 4MB

Q. In real applications, how do you know that you have a memory leak?
A. If you profile your application, you can notice a graph like a saw tooth. Here is how you can determine this with the help of jconsole for the above bad key class example. All you have to to do is while your MemoryLeak is running, get the Java process id by typing

C:\>jps
5808 Jps
4568 MemoryLeak
3860 Main

Now, open up the jconsole as shown below on a command line
 
C:\>jconsole 4568

If try this for both good key class and the bad key class you will get the memory graphs as shown below.

No memory Leak: 



With Memory Leak (saw tooth graph):



You can learn more about profiling




Labels:

May 15, 2012

Java memory profiling questions and asnwers

The questions and answers on Java performance and CPU profiling was discussed in a separate blog. This blog covers memory profiling. Try this blog entry if you want to know how to create memory leak in Java.

Q. How will you go about profiling your Java application for memory usage?
A. There are number of tools both commercial and open-source. There are command line tools that get shipped with Java like hprof, jconsole, jhat, and jmap. Here is an example with hprof.


java -agentlib:hprof=heap=all,thread=y,format=b test.ProfilingTest

When you run the above command in binary format (i.e. format=b), it produces a heap dump file called “java.hprof” when the program exits or a control character (Ctrl-\ or Ctrl-Break on WIN32 and QUIT signal is received kill -QUIT  on Unix machines) is pressed, which can be opened in eclipse memory analysis tool (MAT) for further analysis and leak detection.




A Java heap dump is an image of the complete Java object graph at a certain point in time. It includes all objects, fields, primitive types and object references. It is possible to instruct the JVM to create a heap dump automatically in case of a OutOfMemoryError with the JVM option -XX:+HeapDumpOnOutOfMemoryError. You could also dump the heap on ctrl+brk key stroke by setting the JVM argument -XX:+HeapDumpOnCtrlBreak.


The above heap dump can also be analyzed instantly with the jhat tool that is shipped with your java.

jhat java.hprof


The above command starts a web server as shown below
Reading from java.hprof...
Dump file created Mon Nov 21 12:52:22 EST 2011
Snapshot read, resolving...
Resolving 5426 objects...
Chasing references, expect 1 dots.
Eliminating duplicate references.
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.


The report can be viewed via an internet browser using http://localhost:7000





 
The “jmap” and jconsole are handy tools for analyzing heaps and garbage collection respectively. If you start your application with the following command line options, you will have a number of options to gather profiling data in Java.

  java -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9000 \
       -Dcom.sun.management.jmxremote.authenticate=false \
       -Dcom.sun.management.jmxremote.ssl=false \
       -agentlib:hprof=heap=dump, format=b,depth=10 test.ProfilingTest


If you type jconsole on a command line, you will get the following console.


The “4564” is the process id of the Java process. You can also find out the process id by typing “jps” on a command line.


 
Click on the connect button to view the different profiles as shown below. The jconsole has a button to invoke the garbage collection at your will along with other useful profiling information.


The “jmap” is a handy utility that will cause the heap dump of a running Java application. Find out the process id <pid> with the following command

jps 

and then run the jmap command as shown below with the relevant Java process id (e.g. 4564).


jmap -dump:format=b,file=hprof.bin 4564

 The heap dump file hprof.bin can be opened with jhat and eclipse memory analysis tool for further analysis.

There are commercial tools that can be used in production environment like YourKit for Java & .Net, JProfiler for Java, etc to tools for larger distributed and clustered systems with large number of nodes like CA Wiley Introscope for Java and .Net, SiteScope from HP and ClearStone for Java.

Labels:

May 8, 2012

Java performance questions and answers: overview and profiling cpu

Q. When somebody says an application is running "very slow, what does he/she mean?
A. He/she is generally referring to one of two performance attributes –  latency or scalability. Latency describes how long it takes for a given task to complete, whereas scalability describes how a program's performance gets impacted under increasing load. Building high performance applications require 

  • Low latency –  for example,  low page loading times.
  • High scalability – for example, serving increasing number of users without adversely impacting performance. 
  • High availability – for example, staying up 24x7, without going down due to memory leak or running out of connections to the database or LDAP server.

As described in the above diagram, performance issues can occur due to a variety of reasons.  It can vary from a bad application, database, or infrastructure design that does not scale well to poorly tuned load balancer, virtual machine, application server, and from badly constructed SQL statements (e.g. a Cartesian join) and frequently back tracking regular expressions to thread contention issues caused by multiple threads simultaneously waiting for a long running locked method, block of code, or  database records. Memory and non-memory (e.g. sockets, connections, etc) leaks can also degrade performance by depriving your application of these scarce resources by either consuming more CPU cycles due to over working the garbage collection or by running out of memory or connections. 


Q. What tools do you need to profile a Java application?
A. For troubleshooting Java applications, there are basic tools like vmstat, hprof, JConsole, JAMon, PerfAnal, etc to more feature packed profiling tools like VisualVM, Netbeans profiler, eclipse TPTP (i.e. Test & Performance Tools Platform), etc to tools that can be used in production environment like YourKit for Java, JProfiler for Java, etc to tools for larger distributed and clustered systems with large number of nodes like CA Wiley Introscope for Java, HP Sitescope and ClearStone for Java.

Q. How would you go about profiling a Java application?
A. The following example uses an hprof tool that comes with Java to determine the cpu times. The profiles are dumped to “java.hprof.txt” file when the program exits or a control character Ctrl-\ or Ctrl-Break (WIN32) depending on platform is pressed. On Solaris OS and Linux a profile is also generated when a QUIT signal is received (kill -QUIT ). The process id can be determined with the “jps” command. The command shown below executes the "ProfilingTest" class with the hprof agent for measuring cpu times.

java -agentlib:hprof=cpu=times test.ProfilingTest 

The profiled results will be dumped to a file named “java.hprof.txt”.

CPU TIME (ms) BEGIN (total = 1953) Thu Sep 15 12:26:50 2011

rank  self    accum      count   trace      method
   1   51.20%  51.20%       1   301025   ProfilingTest.invokeMethod3
   2   31.23%  82.44%       1   301015   ProfilingTest.invokeMethod2
   3   15.21%  97.64%       1   301005   ProfilingTest.invokeMethod1
   4   0.82%   98.46%       1   300396   java.net.URLClassLoader$1.run
   5   0.77%   99.23%       2   300707   java.io.Win32FileSystem.normalize
   6   0.77%   100.00%    268   300309   java.lang.StringBuilder.append
CPU TIME (ms) END
 
PerfAnal is a GUI based analysis tool to analyze the above results.

java -jar PerfAnal.jar  java.hprof.txt


Similar approach can be used for profiling for memory usage.


Learn more -- Java Interview Questions and Answers - performance testing your Java application

Labels:

May 2, 2012

5 handy eclipse tips you must know



It is imperative to be able to quickly navigate through your code as in commercial projects there will be thousands of artifacts. You will find more debugging tips using eclipse and other tools. The following tips are must know, and if you don't know any of this already, apply these tips and see how productive you become. Especially the control clicking shown here will help you fly through your code.


Tip #1 : Navigating between web resource files and Java classes

<div>
    <s:link rendered="#{!myAccounts.viewAccounts}" reRender="myAccounts" styleClass="link-excel" view="/view/accounts/myaccounts.excel.xhtml" >    Export </s:link>
 ......
</div>

you will often want to navigate from one xhtml or JSP page to another. In the above example to navigate to myaccounts.excel.xhtml page from your current page, highlight the text "myaccounts.excel.xhtml" in the above code and then press "ctrl+shift+r" short-cut to select the file and go to that file directly. In the above code, "myAccounts" within #{!myAccounts.viewAccounts} is an annotation attribute in the Java based backing bean. To get quickly to the backing Java bean, you will need to highlight the text "myAccounts" in the above code and the press "ctr+h" short-cut to bring up the search dialog to search for that text. The Java snippet will look like this
    
    @In(value = "myAccounts")
    private MyAccounts myAccountsController;
 
Once you have found the Java source file that has the text "myAccounts" annotated attribute, you can go directly to the method with the context helper short cut "ctrl+o" and then typing the first few charaters of the method "viewAccounts" to go to that method. This tip can be applied for other Web frameworks as well if you want to navigate between Web resources and Java source files.

The "highlighting + ctrl+h" is also handy for navigating to relevant Java or web resource file from a spring config file or any other configuration file like web.xml.

Tip #2: If you are navigating between Java source files then the code snippet below
   
public void init(Long accountId) {
        logger.debug("Initialising accounts  .... ");
        this.selectedAccount = this.serviceFacade.getAccount(accountId, user);
        this.authoriser.checkPermission(new AccountsPermission(this.user, this.selectedAccount));
        ...
}

may require you to navigate to "AccountsPermission" class. Highlight the "AccountsPermission" and press "F3" function key to go to that class. If you want to see the type hirarchy (i.e. its interfaces, super class, sub class, etc) press "F4". You can also highlight a varible like "selectedAccount" or a method like "checkPermission" and then press "F3" to go to that variable or method declaration.


What is even more efficient is "ctrl + clicking". All you have to do is press the ctrl key and then click on a variable or class to name to navigate to it. If a particular method has both interface and implementation, you will get a contextual option list to choose either the interface or the implementation with this control clicking.

So, both "ctrl + clicking" and "highlight  + (ctrl+shift+r or ctrl+h) are very handy commands for navigating around.


Tip 3#: If eclipse is slow in opening your files or starting up, etc it is likely that some plugins might be making it slow.

Recently I had this issue, and after removing a number of plugins, for example Atlasian Maven/Bamboo build plugins and some JBoss plugins for JSF and Seam UI development, my eclipse is much faster and responsive. 


Tip #4: If you are working on a large project with lots of modules or projects

 it is worth creating "working sets" to group relevant projects together. This will also enable you to open and close projects or modules based on "working sets". You can also search for resources based on a "working set". Google for "How to create a working set in eclipse".


Tip #5:Code assist to type less

No body wants to type a lot of code. While working on your code, you can use "ctrl + space" to auto complete some variable names, methods, class names, etc.



The above short-cut keys will help you navigate quickly through your code. Feel free to comment with other useful tips. There are more tips on

Labels:

May 1, 2012

Core Java Interview Questions and Answers: String concatenation

It is a very common beginner level question on String concatenation. Its imperative to understand that NOT all String concatenations are  bad. It depends on how you are using it. You need to have the basic understanding that in Java, String is an immutable object and StringBuilder (not thread-safe) and StringBuffer (thread-safe) are mutable. In, Java you can concatenate strings a number of ways with the "+" operator, using the append( ) in StringBuilder and StringBuffer classes, and the other methods like concat( ) in String class.

Q1. Is anything wrong with the following code?

public class StringConcat {
    public static String withStringBuilder(int count) {
     String s = "Hello" + " " + " peter " + count;
     return s; 
    }

}
 
A1. No, the compiler internally uses a StringBuilder to use two append( ) calls to append the string and converts it to a String object using the toString( ) method.

Note: you can try the javap command  described below to see why.


Q2. Is anything wrong with the following code?
public class StringConcat {
    public static String withStringBuilder() {
      String s = "Hello" + " " + " peter " + " how are you";
      return s; 
    }

}
 
A2. No, the compiler is smart enough to work out that it is a static concatenation and it uses its optimization to concatenate the string during compile-time. If you verify this by using a Java decompiler like jd-gui.exe to decompile the compiled class back, you will get the source code as below.
 
public class StringConcat
 {
  public static String withStringBuilder(int count)
  {
    String s = "Hello  peter  how are you";
    return s;
  }
}

You can also use the javap command to dissemble the compiled class file using the following command
 
C:\workspaces\proj\Test\src>javap -c StringConcat

Gives the following output
 
Compiled from "StringConcat.java"
public class StringConcat extends java.lang.Object{
public StringConcat();
  Code:
   0:   aload_0
   1:   invokespecial       #1; //Method java/lang/Object."<init>":()V
   4:   return

public static java.lang.String withStringBuilder(int);
  Code:
   0:   ldc                 #2; //String Hello  peter  how are you
   2:   astore_1
   3:   aload_1
   4:   areturn

}

The line 0 shows that it has been optimized


Q3. Is anything wrong with the following code snippet

public class StringConcat {
 
 public static String withoutStringBuilder(int count) {
        String str = "";
        for (int i = 0; i < count; i++) {
            str += i;
        }

        return str;
    }
 
}
 
A3. Yes. it consumes more memory and can have performance implications. This is because, a String object in Java is immutable. This means, you can't modify a String. If the value of count is 100, then the above code will create 100 new StringBuilder objects, of which 99 of them will be discarded for garbage collection. Creating new objects unnecessarily is not efficient, and the garbage collector needs to clean up those unreferenced 99 objects. StringBuffer and StringBuilder: are mutable and use them when you want to modify the contents. StringBuilder was added in Java 5 and it is identical in all respects to StringBuffer except that it is not synchronized, which makes it slightly faster at the cost of not being thread-safe. The code below creates only two new objects, the StringBuilder and the final String that is returned.
public class StringConcat {
 
    public static String withStringBuilder(int count) {
        StringBuilder sb = new StringBuilder(100);
        for (int i = 0; i < count; i++) {
            sb.append(i);
        }

        return sb.toString();
    }

}
 
Now, if you want to be more pedantic as to how we know that the 100 StringBuilder objects are created, we can use the javap option to our rescue. The javap is a class file dissembler. If you compile the codebase in the question and then run the javap command with the StringConcat.class file as shown below
C:\workspaces\proj_blue\Test\src>javap -c StringConcat

You will get an output as shown below
 
Compiled from "StringConcat.java"
public class StringConcat extends java.lang.Object{
public StringConcat();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public static java.lang.String withoutStringBuilder(int);
  Code:
   0:   ldc     #2; //String
   2:   astore_1
   3:   iconst_0
   4:   istore_2
   5:   iload_2
   6:   iload_0
   7:   if_icmpge       35
   10:  new     #3; //class java/lang/StringBuilder
   13:  dup
   14:  invokespecial   #4; //Method java/lang/StringBuilder."<init>":()V
   17:  aload_1
   18:  invokevirtual   #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   21:  iload_2
   22:  invokevirtual   #6; //Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
   25:  invokevirtual   #7; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
   28:  astore_1
   29:  iinc    2, 1
   32:  goto    5
   35:  aload_1
   36:  areturn

}

The dissembled looks cryptic, but if you inspect it carefully the code within the public static java.lang.String withoutStringBuilder(int);

Line 5 to 32: is the code within the for loop. The "goto 5" indicates looping back.
Line 10: creates a new StringBuilder object every time
Line 18: uses the StringBuilder's append method to concatenate the String.
Line 25: uses the toString( ) method to convert the StringBuilder back to the existing String reference via toString( ) method.

If you run the improved code snippet in the answer through javap, you get the following output
Compiled from "StringConcat.java"
public class StringConcat extends java.lang.Object{
public StringConcat();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public static java.lang.String withStringBuilder(int);
  Code:
   0:   new     #2; //class java/lang/StringBuilder
   3:   dup
   4:   bipush  100
   6:   invokespecial   #3; //Method java/lang/StringBuilder."<init>":(I)V
   9:   astore_1
   10:  iconst_0
   11:  istore_2
   12:  iload_2
   13:  iload_0
   14:  if_icmpge       29
   17:  aload_1
   18:  iload_2
   19:  invokevirtual   #4; //Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
   22:  pop
   23:  iinc    2, 1
   26:  goto    12
   29:  aload_1
   30:  invokevirtual   #5; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
   33:  areturn

}

As you could see

Line 0 to 6: initializes one StringBuilder object outside the for loop.
Line 12 to 26: is the for loop.
Line 19: indicates that since the StringBuilder is mutable, the string is appended via the append method.


Important: The creation of extra strings is not limited to the overloaded mathematical operator "+", but there are several other methods like concat( ), trim( ), substring( ), and replace( ) in the String class that generate new string instances. So use StringBuffer or StringBuilder for computation intensive operations to get better performance. Experiment with javap for the String methods like concat( ), trim( ), substring( ), and replace( ) .

Note: So, javap and jd-gui.exe are handy tools for debugging your application for certain issues. For example, the java decompiler is handy for debugging generics to see how the java source code with generics is converted after compilation by decompiling the .class file back to source code.

Labels: