Monday, September 6, 2010

About JVM Options

  • Options that begin with -X are non-standard (not guaranteed to be supported on all VM implementations), and are subject to change without notice in subsequent releases of the JDK.
  • Options that are specified with -XX are not stable and are not recommended for casual use. These options are subject to change without notice. 
Don't expect to see abnormal tps increasement by using an option something like [-XX:+StringCache] this is why i am telling this here.

-XX:+StringCache : Enables caching of commonly allocated strings.

But it would be GREET :) if works huh ?

    Tuesday, August 24, 2010

    How to get tcp dump & snoop

    Snoop to file
    # snoop -xv -o snoopfilename port portNo


    Read from file with Snoop
    # snoop -xv -i snoopfilename port portNo

    TcpDump
    # tcpdump -i eth0 -s 0 -w snoopfilename port portNo

    Wednesday, August 18, 2010

    Vector vs ArrayList vs HashSet

    List syncList = Collections.synchronizedList(new ArrayList());

    Set syncSet = Collections.synchronizedSet(new HashSet());

    Vector is naturally synchronized.

    Performance Test Result
    #1 :ArrayList
    #2 :Vector
    #3 : HashSet

    Read more: Java Code Geeks: Java Best Practices – Vector vs ArrayList vs HashSet

    http://www.javacodegeeks.com/2010/08/java-best-practices-vector-arraylist.html#ixzz0wxSNUeuS

    Saturday, August 14, 2010

    Scalability hints

    Avoid lock on static variables or static methods:
    It locks your class. Even worse than locking your object!

    private static Object myVar = null;
    private foo{
        synchronized (myVar){// do not do that
           ....
       }
    }
    private static synchronized myMethod(){
         .....
    }

    Use lock free data structures
    Use java.util.concurrent.atomic package.It uses lock-free algorithms under the help with hardware synchronization primitives without using native code.

    Reduce lock granularity:

    Try to prefer  "block locks" instead of "method locks" if possible.synchronized methods locks the Object instance as you know.
    public class SchemaManager {
    private HashMap schema;
    private HashMap treeNodes;
    //Bad way!
    public boolean synchronized updateSchema(HashMap nodeTree) {
              String nodeName = (String)nodeTree.get("nodeName");
              String nodeAttributes = (List)nodeTree.get("attributes");
              if (nodeName == null)
                      return false;
              else
                      return schema.update(nodeName,nodeAttributes);
    }
    //Good Way
    public boolean updateSchema(HashMap nodeTree) {
    String nodeName = (String)nodeTree.get("nodeName");
    String nodeAttributes = (List)nodeTree.get("attributes");
    synchronized (schema) {
              if (nodeName == null)
                       return false;
              else
                       return schema.update(nodeName,nodeAttributes); }
    }
    public boolean synchronized updateTreeNodes() { ...... } }


    Make synchronized blocks as short as possible
    Move the thread safe code outside of the synchronized block.
     
    public boolean updateSchema(HashMap nodeTree) {
                   synchronized (schema) {
                              String nodeName = (String)nodeTree.get("nodeName");
                              String nodeAttributes = (List)nodeTree.get("attributes");
                              if (nodeName == null)
                                     return false;
                              else
                                     return schema.update(nodeName,nodeAttributes);
                    }
    }
     
    public boolean updateSchema(HashMap nodeTree) {
               String nodeName = (String)nodeTree.get("nodeName");
               String nodeAttributes = (List)nodeTree.get("attributes");
               synchronized (schema) {
                         if (nodeName == null)
                                return false;
                         else
                                return schema.update(nodeName,nodeAttributes);
              }
    }




    Try to use ThreadLocal variables if possible.

    Friday, August 13, 2010

    Java 6 allows classpath wildcards

    All we hate to complete our $CLASSPATH  !

    We always try to setup a CLASSPATH enviroment variable while dealing with all & everytime & anywhere.

    Fortunately Sun noticed our tears at the end :)

    Now we can specify lib/*, and all of the JAR files listed in that directory (not recursively), in the classpath.

    We collect all dependencies under a directory and it is enough to say everthing is out there...