You are currently browsing the monthly archive for November 2006.

If you like my post about Handy tool for developing on windows, then you should look at PowerShell.

Overview

Windows PowerShell includes:

• More than 130 command-line tools (called “cmdlets”) for performing common system administration tasks, such as managing services, processes, event logs, certificates, the registry, and using Windows Management Instrumentation (WMI).

• Command-line tools are designed to be easy to learn and easy to use with standard naming conventions and common parameters, and simple tools for sorting, filtering, and formatting data and objects.

• Support for existing scripting languages and existing command-line tools, and multiple versions of Windows, including Windows XP, Windows Server 2003, Windows Vista™, and Windows Server code name “Longhorn”.

• Features that enable users to navigate data stores, like the registry and certificate stores, as if they were a file system.

• Standard utilities for managing Windows data in different stores and formats, including Active Directory Service Interfaces (ADSI), Windows Management Instrumentation (WMI), Component Object Model (COM) objects, ActiveX Data Objects (ADO), HTML, and XML.

• Sophisticated expression parsing and .NET Framework object manipulation at the command line, including pipelining of objects help IT professionals to work more efficiently and effectively.

• An extensible interface that enables independent software vendors and enterprise developers to build custom cmdlets to meet unique application and system administration requirements.

Believe or not your code has 90% of time spent in 10% of code. Usually I am tuning on hot spots identified by he profiler. I recommand the NetBean 5.5 profiler. But except that I still see many programmers’s code has “Code Smell” that causes performance slow or memory consuming. So I am going to list some tips here and more examples in the future.

No. 1
Bad:

static String append(String[] array) {
   String result = "";
   for (int i = 0; i < array.length; i++) {
      result = result + array[i];
   }
   return result;
}

Good:

static String append(String[] array) {
  StringBuffer result = new StringBuffer();
  for (int i = 0; i < array.length; i++) {
    result.append(array[i]);
  }
  return result.toString();
}

Tip: Using StringBuffer instead creating new String.

No. 2
Bad:
for (int i = 0; i < array.length; i++) { … }

Good:
int length = array.length;
for (int i = 0; i < length; i++) { … }

Tip: Caching values in local variables can therefore produce a significant improvement.

No. 3
Tip: using Flyweight Pattern
The pattern here states about a mechanism by which you can avoid creating a large number of object instances to represent the ntire system. To decide if some part of your program is a candidate for using Flyweights, consider whether it is possible o remove some data from the class and make it extrinsic. If this makes it possible to reduce greatly the number of different lass instances your program needs to maintain, this might be a case where Flyweights will help.

Ref: Desing Flyweight Pattern

No. 4
Tip: Cache Frequently Computed Values

public final class String {
   private int hash = 0;
   public int hashCode() {
      int h = hash;
      if (h == 0) {
        ...
        hash = h;
      }
      return h;
   }
}

Ref: Cache 101

No. 5
Tip: Recycle your code by object pooling

class ObjectPool {
   private Class objectClass; // Class of objects managed by this factory
   PooledObject freeObjects; // List of previously used objects

   public ObjectPool(Class objectClass) {this.objectClass = objectClass; }

   public PooledObject get() throws InstantiationException, IllegalAccessException {
      PooledObject pooledObject = null;
      if (freeObjects == null) {
         pooledObject = (PooledObject) objectClass.newInstance(); // Allocate a new object
      } else {
         pooledObject = freeObjects; // Get an existing object from the free list
         freeObjects = freeObjects.nextFree;
      }
      pooledObject.factory = this; pooledObject.nextFree = null;
      return pooledObject;
   }
}
abstract class PooledObject {
   PooledObject nextFree; // Next object on free list
   ObjectPool factory;
   void free() {
      nextFree = factory.freeList; // Add object to free list
      factory.freeList = this;
   }
}

No. 6
Tip: Reflection Recommendations
* Avoid reflection if possible
* Define interfaces for extension classes
* Automatic code generation
* Do lookups once and cache for java.lang.reflect.Method object

You don’t have to download a new JRE with the updates; you can just update the JRE you already have. The tool will be maintained to provide the latest updates as they’re needed. So, when this happens again (and it will), you’ll just need to use this tool instead of replacing your JRE.

see:

Java Daylight Saving Time and Time Zone

Sun Java SE TZupdater tool 

Pattern is a solution to a recurring problem in a context. Once Pattern (solution) is developed from a recurring problem it can be reused without reinventing the solution again.
This article mainly focuses on performance improvement practices using Patterns in J2EE. There is no quick silver bullet to solve all performance problems on J2EE. So I just list all the resources for you.

Advanced J2EE topics
Design Patterns for Optimizing the Performance of J2EE Applications
Best practices to improve performance using Patterns in J2EE
Take control with the Proxy design pattern
Optimistic Locking pattern for EJBs
Repair invalid cached services in the Service Locator pattern
J2EE Design Patterns for presentation tier
Design Patterns for Building Flexible and Maintainable J2EE Applications
Patterns of Enterprise Application Architecture

Developing performance-oriented JDBC applications is not easy. JDBC drivers do not throw exceptions to tell you when your code is running too slow. This performance tips presents some general guidelines for improving JDBC application performance that have been compiled by examining the JDBC implementations of numerous shipping JDBC applications.These guidelines includes:

  • Using Database MetaData methods appropriately
  • Retrieve only required data
  • Select functions that optimize performance
  • Manage connections and updates

Also design Your Application for Best Performance:

  • Connection Pools
  • Caching Data
  • Process as Much Data as Possible Inside the Database
  • Use Built-in DBMS Set-based Processing
  • Make Your Queries Smart
  • Make Transactions Single-batch
  • Never Have a DBMS Transaction Span User Input
  • Use In-place Updates
  • Keep Operational Data Sets Small
  • Use Pipelining and Parallelism

Ref:
JDBC performance tips
Performance Tuning Your JDBC Application

Today’s big news on Java community is Sun open sources Java SE, ME, and Glassfish under GPLv2.
But you may ask Why GPL? Here are the quick review of their benefits:

  • Java becomes Linux friendly, and can now be shipped by default on GNU/Linux distro’s since it’s free.
  • Government-friendly. Foreign governments can now bet their infrastructures on Java without worrying about being dependent on proprietary, or US-owned intellectual propery (which could one day be embargoed), since Java’s license makes it completely free. Sun officials specifically named China and Brasil as examples of countries that have been lobbying Sun for open source Java. Even in the US, some projects won’t accept bids not built on open source software.
  • New technical use cases. Companies can now freely port Java to new hardware platforms & operating systems, and customized app-specific JVMs can also be created where needed – however due to the viral policies of GPL – all such ports must also be released as GPL which may limit such activities for commercial purposes since the ports could not be made proprietary.

Details on here:
Sun open sources Java SE, ME, and Glassfish under GPLv2
Could Glassfish become the next major open source appserver?
GlassFish: building an Open Source Java EE 5 Application Server
OpenJDK: the open-source JDK project

This blog will tell you some coding tips for writing J2EE application.

  • EJB container
  • JDBC connection
  • Servlets
  • JSP
  • JMS

Ref:
Precise Java (J2EE)
J2EE Performance for WebLogic, WebSphere, JBoss, etc.

Usually I worked on my JBoss, SQL server and also open IM, Skype, Outlook etc. Those communication programs will take the port as a Socket channel. The problem comes up because the special port numbers like 1098, 1099 is used as our application JMS listening port. So I have to run “netstat -nao” or “ipconfig /renew” , “ipconfig /release”to find the disgusting PID, then go to kill it.

Now I found the Microsoft has the series tools to help us. Process Monitor is an advanced monitoring tool for Windows that shows real-time file system, Registry and process/thread activity. Let’s try it…

Also I use those hints for many times. You may like them, too.

Why is Java optimization so important? Throughout its history, Java has always endured claims of being slow. Although factors such as better hardware, better JVMs, and improvements to the Java language have drastically improved Java performance over the years, it still has a reputation of being slow. So for Java applications, performance is always a key issue. And Any Java developer should remind himself to optimize Java applications.

Here are some guidelines and programming standards to improve your Java code.

  • Collections
  • String and StringBuffer
  • Serialization
  • I/O
  • Object creation
  • Loops
  • Exceptions

Ref:
Precise Java (J2SE)
book:
Effective Java Programming Language Guide
Java Puzzlers: Traps, Pitfalls, and Corner Cases

Memory leaks are blocks of allocated memory that the program no longer references. Leaks waste space by filling up pages of memory with inaccessible data and waste time due to extra paging activity. Leaked memory eventually forces the system to allocate additional virtual memory pages for the application, the allocation of which could have been avoided by reclaiming the leaked memory.

Tips:

  • Pair code that handles allocation and freeing of resources as closely together as possible, preferable in the same method.
  • With collections, objects that are added should be removed as well.
  • Take your application to a development environment and run it inside a memory profiler.

It is almost impossible to improve the performance of your code if your application is impacted by memory leaks.  Memory leaks can be in either Java or native code.  In native code, memory leaks are caused by the programmer forgetting to free a memory block.  Java memory leaks are often caused by saving an object reference in a class level collection and forgetting to remove it at the proper time. 

There are also other kinds of problems with managing resources that impact performance, such as not closing JDBC Statements/ResultSets in a finally block (many JDBC drivers store a Statement reference in the Connection object).  Fixing memory/resource leaks in the application not only has a great impact on performance but also system stability, as you will consume fewer resources. 

Ref:
Reference Objects and Garbage Collection
Handling memory leaks in Java programs
Memory Leaks
Overview of memory leaks
Launching the Java application VM manually when debugging