Wednesday, February 19, 2014

Identifying a deadlock in your application using JConsole

It's very simple!!  You need not setup anything for this. JPS and JConsole comes with JDK toolkit.

Step 1:
Got to command prompt and type 'jps' command. This will tell you the currently running java process id's.

Example:
C:\Users\kh1205>jps

7620 org.eclipse.equinox.launcher_1.1.1.R36x_v20101122_1400.jar
7048 Main
5740 Jps


Step 2:
Open jconsole using your application process id, in my case 7048 is my eclipse application process id.

Example:
C:\Users\kh1205>jconsole 7048

This will launch jconsole window with various sections.

Step 3:
Navigate to Threads section in jconsole window, and navigate through main thread(ex:main) and worker threads(ex: worker-1, worker-2). In one of these threads, your application is in waiting state.





Tuesday, February 11, 2014

Ellipses (...) conventions in Eclipse

There are lot of discussions on this topic, but there is no official eclipse wiki page which described ellipses standards and conventions which has to be used while developing eclipse products.

Below are the resources which helps us to understand how to use ellipses in eclipse.


As per my understanding, eclipse is mostly following Windows User interface guidelines for ellipses usage conventions.

Below points which helps us to understand in summary.

1. Display an ellipsis if a dialog box is displayed to the user to change/enter information. Omit the ellipses for "show properties", "help" and "about" which are essentially "show" dialogs anyway. No more info is needed for them.

2. While command buttons are used for immediate actions, more information might be needed to perform the action. Indicate a command that needs additional information by adding an ellipsis at the end of the button label
Screen shot of Print command button with ellipses

In this example, the Print... command displays a Print dialog box to gather more information.
Screen shot of Print command button, no ellipses
By contrast, in this example the Print command prints a single copy of a document to the default printer without any further user interaction.

3. Proper use of ellipses is important to indicate that users can make further choices before performing the action, or even cancel the action entirely.

This doesn't mean you should use an ellipsis whenever an action displays another window—only when additional information is required to perform the action. Consequently, any command button whose implicit verb is to "show another window" doesn't take an ellipsis, such as with the commands About, Advanced, Help (or any other command linking to a Help topic), Options, Properties, or Settings.

4. Generally, ellipses are used in user interfaces to indicate incompleteness. Commands that show other windows aren't incomplete—they must display another window and additional information isn't needed to perform their action. 

    Some actions in Eclipse:
    Restart -> it will just restart without asking. It might ask if any unsaved changes, for confirmation ellipses not required.
    Exit -> It will just exit from the eclipse. No ellipse required

    Import...  -user has to select what has to imported. We should use ellipse here
    Export...  -user has to select what has to exported. We should use ellipse here



    




Thursday, February 6, 2014

Run time data areas - JVM Memory model

I love to share an excellent article from Point software, It's just a brilliant composition and easy to understand with diagrams.

Every developer gets once confronted by Java memory questions like: What size should I define for the Heap space? An OutOfMemoryError covers which part of the runtime data area? In the Heap, PermGen, or Thread? And how do I solve it?

Java Memory Model

The Java memory model is specified in the latest JVM specification, Java SE 7 Edition, and mainly in the chapters “2.5 Runtime Data Areas” and “2.6 Frames”. In a nutshell primitive, object and class data are stored in 3 different memory areas: heap space, method area and native are.
The heap space holds object data, the method area holds class code, and the native area holds references to the code and object data.
The method area is also known as the permanent generation space (PermGen). All class data are loaded into this memory space. This includes the field and method data and the code for the methods and constructors.
[UPDATE]
Oracle has planned in JDK 7 to completely remove the PermGen space from the JVM. Reason is the consolidation of HotSpot and JRockit. As a result the method area will be part of the operating system’s native heap.
All objects being instantiated during runtime are stored in the heap space. The heap space again is divided into several parts: eden, survivor, and old generation Space.
Method executions are within a thread. Local variables of primitive types and references are stored here. The references for example points to Objects like String stored in the Heap space. Here is a video demonstrating the interaction between a stack and the heap.
For a better understanding let’s have a look at another example code:
The data are then stored like this:
With the JConsole tool it is possible to view the memory allocations in the heap, the number of threads and loaded classes of a running Java application (e.g. Eclipse):

Java Memory Architecture

There is an excellent white paper about Memory Management in the Java
HotSpot™ Virtual Machine
. It describes about the automatic memory management handle using garbage collection.
The Java memory architecture consists of the following parts:

Heap memory

Since objects are stored in the heap part it is worth to have a closer look. The heap space itself is again separated into several spaces:
  • Young generation with eden and survivor space
  • Old Generation with tenured space
Each space harbors objects with different life cycles:
  • New/short-term objects are instantiated in the eden space.
  • Survived/mid-term objects are copied from the eden space to the survivor space.
  • Tenured/long-term objects are copied from the survivor to the old generation space.
By separating objects by their life time allows a shorter time consumption of the minor garbage collection and in return there is more cpu time for the application.
The reason is that in Java – unlike C – memory is freed (by destroying objects) automatically by two different garbage collectors: a minor and major garbage collection.
Instead of validating all objects in the heap – whether it can be destroyed or not – the minor garbage collector marks undestroyed objects with a garbage count. After a certain count the object is move to the old generation space.
A more detailed blog of the garbage collection will be discussed in another blog. For now it is sufficient to know that there are two garbage collectors.

OutOfMemoryError – but where?

Having this memory architecture in mind also helps to understand the different OutOfMemoryErrors like:
  1. Exception in thread “main”: java.lang.OutOfMemoryError: Java heap space
    Reason: an object could not be allocated into the heap space.

  2. Exception in thread “main”: java.lang.OutOfMemoryError: PermGen space
    Reason: classes and methods could not be loaded into the PermGen space. This occurs when an application requires a lot of classes e.g. in various 3rd party libraries.

  3. Exception in thread “main”: java.lang.OutOfMemoryError: Requested array size exceeds VM limit
    Reason: this occurs when an arrays is created larger than the heap size.

  4. Exception in thread “main”: java.lang.OutOfMemoryError: request bytes for . Out of swap space?
    Reason: this occurs when an allocation from the native heap failed and might be close to its limit. The indicates the source of the module where this error occurs.

  5. Exception in thread “main”: java.lang.OutOfMemoryError: (Native method)
    Reason: this error indicates that the problem originates from a native call rather than in the JVM.

Useful Links

Resources:

Check style - What is the meaning of inherit severity ?

Inherit severity indicates that, whatever is the parent severity will be applicable for a child element.

The default parent of every module is a 'checker', and default severity is 'warning'.

Look at the Checkstyle configuration file below.

<module name="Checker"> //Default Parent
  <property name="severity" value="warning"/>  //Default property of checker

  <module name="TreeWalker">
    <module name="JavadocMethod">
      <property name="severity" value="warning"/>
      <property name="suppressLoadErrors" value="true"/>
      <metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="error"/>
    </module>
    <module name="JavadocType"/>
    <module name="JavadocVariable"/>
    <module name="JavadocStyle">
      <property name="checkFirstSentence" value="false"/>
 .....

</module>


Resource:
http://checkstyle.sourceforge.net/config.html#Properties




Check Style Error- cannot initialize module TreeWalker - Unable to instantiate DoubleCheckedLocking

I was trying configure my old check style configuration file with the latest check style (Eclipse Checkstyle Plugin 5.6.1.201306282206) plugin. There is no issue during the configuration but during the activation of check style, it's throwing the below error.

cannot initialize module TreeWalker - Unable to instantiate DoubleCheckedLocking

After going through forums, what I understood was 'DoubleCheckedLocking' is not supported in the latest check style plug-in, since this attribute is exists in my old check style configuration file, it was throwing an error.

To resolve this, go to check style configuration file and remove the below line.

<module name="DoubleCheckedLocking"/>

Resource:
http://sourceforge.net/p/checkstyle/bugs/682/
http://checkstyle.sourceforge.net/releasenotes.html

Wednesday, February 5, 2014

Lazy eclipse plugin/bundle state

Plug-ins that are resolved and that have a bundle activation policy of lazy will enter the Starting state. The console window may show bundle state as <<LAZY>>. This indicates a bundle in the Starting state waiting for a class access to transition to active.

If any plug-in that remains in a starting state is indicating of a plugin that is failing to use best practices of eclipse bundlee activator class.

http://wiki.eclipse.org/Lazy_Start_Bundles
http://wiki.eclipse.org/Where_Is_My_Bundle