Thursday, April 24, 2008

Performance issues and Tools to use

Insufficient Memory

The Java Virtual Machine (JVM)* has the following types of memory: heap, non-heap, and native.

3 Types:
1) Heap Memory Error
2) Non Heap Memory Error
3) Native Memory Error

Memory Leaks

The JVM is responsible for automatic memory management, which reclaims the unused memory for the application. However, if an application keeps a reference to an object that it no longers needs, the object cannot be garbage collected and will occupy space in the heap until the object is removed. Such unintentional object retention is referred to as a memory leak. If the application leaks large amounts of memory, it will eventually run out of memory, and an OutOfMemoryError will be thrown. In addition, garbage collection may take place more frequently as the application attempts to free up space, thus causing the application to slow down.

Finalizers

Another possible cause of an OutOfMemoryError is the excessive use of finalizers. The java.lang.Object class has a protected method called finalize. A class can override this finalize method to dispose of system resources or to perform cleanup before an object of that class is reclaimed by garbage collection. The finalize method that can be invoked for an object is called a finalizer of that object. There is no guarantee when a finalizer will be run or that it will be run at all. An object that has a finalizer will not be garbage collected until its finalizer is run. Thus, objects that are pending for finalization will retain memory even though the objects are no longer referenced by the application, and this could lead to a problem similar to a memory leak.


Deadlocks

A deadlock occurs when two or more threads are each waiting for another to release a lock. The Java programming language uses monitors to synchronize threads. Each object is associated with a monitor, which can also be referred as an object monitor. If a thread invokes a synchronized method on an object, that object is locked. Another thread invoking a synchronized method on the same object will block until the lock is released. Besides the built-in synchronization support, the java.util.concurrent.locks package that was introduced in J2SE 5.0 provides a framework for locking and waiting for conditions. Deadlocks can involve object monitors as well as java.util.concurrent locks.

Typically, a deadlock causes the application or part of the application to become unresponsive. For example, if a thread responsible for the graphical user interface (GUI) update is deadlocked, the GUI application freezes and does not respond to any user action.


Looping Threads

Looping threads can also cause an application to hang. When one or more threads are executing in an infinite loop, that loop may consume all available CPU cycles and cause the rest of the application to be unresponsive.


High Lock Contention

Synchronization is heavily used in multithreaded applications to ensure mutually exclusive access to a shared resource or to coordinate and complete tasks among multiple threads. For example, an application uses an object monitor to synchronize updates on a data structure. When two threads attempt to update the data structure at the same time, only one thread is able to acquire the object monitor and proceed to update the data structure. Meanwhile, the other thread blocks as it waits to enter the synchronized block until the first thread finishes its update and releases the object monitor. Contended synchronization impacts application performance and scalability.

Problem: Insufficient memory
Symptom: OutOfMemoryError
Tool: Java Heap Analysis Tool (jhat)

Problem: Memory leaks Growing use of memory
Symptom: Frequent garbage collection Java Monitoring and Management Console (jconsole)
Tool: JVM Statistical Monitoring Tool (jstat)

Symptom: A class with a high growth rate. A class with an unexpected number of instances Memory Map (jmap)
An object is being referenced unintentionally jconsole or jmap with jhat
Tool: See jmap -histo option
See jmap -dump option

Problem: Finalizers
Symptom: Objects are pending for finalization jconsole
Tool: jmap -dump with jhat

Problem: Deadlocks Threads block on object monitor or
Symptom: java.util.concurrent locks jconsole
Tool: Stack Trace (jstack)

Problem: Looping threads
Symptom: Thread CPU time is continuously increasing
Tool: jconsole with JTop

Problem: High lock contention
Symptom: Thread with high contention statistics
Tool: jconsole

Had a great time exploring performance monitors.

-Knowledge is power.

No comments: