Java性能-Java performance

How can you improve Java I/O performance?

I/O efficiency should be a high priority for developers looking to optimally increase performance.

The basic rules for speeding up I/O performance are:
• Minimise accessing the hard disk.
• Minimise accessing the underlying operating system.
• Minimise processing bytes and characters individually.

Some of the techniques to improve I/O performance:
• Use buffering to minimise disk access and underlying operating system.
• It is recommended to use logging frameworks like Log4J or apache commons logging
• Use the NIO package, if you are using JDK 1.4 or later
• I/O performance can be improved by inimising the calls to the underlying operating systems.
• Where applicable caching can be used to improve performance by reading in all the lines of a file into a Java collection class like an ArrayList or a HashMap and subsequently access the data from an in-memory collection instead of the disk.

How would you improve performance of a Java application?

The Basic Rules are:

    * Pool valuable system resources like threads, database connections, socket connections etc.
    * Optimize your I/O operations.
    * Minimize network overheads
    * Establish whether you have a potential memory problem and manage your objects efficiently. Use lazy initialization when you want to distribute the load of creating large amounts of objects.

 

Where applicable apply the following performance tips in your code:

 

    * Use ArrayLists, HashMap etc as opposed to Vector, Hashtable etc where possible.
    * Set the initial capacity of a collection and StringBuffer/StringBuilder appropriately.
    * Minimise the use of casting or runtime type checking like instanceof in frequently executed methods or in loops.
    * Do not compute constants inside a large loop. (loop optimization)
    * Exception creation can be expensive because it has to create the full stack trace. The stack trace is obviously useful if you are planning to log or display the exception to the user. But if you are using your exception to just control the flow, which is not recommended, then throw an exception, which is precreated. An efficient way to do this is to declare a public static final Exception in your exception class itself.
    * Avoid using System.out.println and use logging frameworks like Log4J etc
    * Minimise calls to Date, Calendar, etc related classes.
    * Minimise JNI calls in your code

 

How would you detect and minimise memory leaks in Java?

In Java memory leaks are caused by poor program design where object references are long lived and the garbage collector is unable to reclaim those objects.

 

Detecting memory leaks:

    * Use tools like JProbe, OptimizeIt etc to detect memory leaks.
    * Use operating system process monitors like task manager on NT systems, ps, vmstat, iostat, netstat etc on UNIX systems.
    * Write your own utility class with the help of totalMemory() and freeMemory() methods in the Java Runtime class. Place these calls in your code strategically for pre and post memory recording where you suspect to be causing memory leaks.

Runtime.getRuntime().totalMemory()…

 

Minimising memory leaks

 

In Java, typically memory leak occurs when an object of a longer lifecycle has a reference to objects of a short life cycle. This prevents the objects with short life cycle being garbage collected. The developer must remember to remove the references to the short-lived objects from the long-lived objects. Objects with the same life cycle do not cause any issues because the garbage collector is smart enough to deal with the circular references

    * Design applications with an object’s life cycle in mind, instead of relying on the clever features of the JVM.
    * Unreachable collection objects can magnify a memory leak problem. The WeakHashMap is a combination of HashMap and WeakReference. This class can be used for programming problems where you need to have a HashMap of information, but you would like that information to be garbage collected if you are the only one referencing it.
    * Free native system resources like AWT frame, files, JNI etc when finished with them.(call dispose)