Thursday, February 22, 2007
Ways to Improve The Performance of your java Program
| Following article has tips on ways to Improve the performance of your java Program I must say this is good article I ever gone through.Please have a look and please leave your comments for the same. |
Wednesday, February 21, 2007
Garbage collection and memory management in Java applications
| As a modern language, Java has an automatic memory management mechanism called garbage collection, which makes it much easier for you to manage objects created in the application code. When an object is created by new keyword, it is placed into the Java heap of the Java Virtual Machine (JVM). The JVM has a garbage collector, and when this object is no longer needed by any code, the garbage collector removes it from the Java heap. Listing 1 is an example of a method defined in a Java class public void aMethod() { String example = new String("My Example"); // String is a Java class } In above example, a new String object is allocated in the Java heap. In Java , you don't need to write code to explicitly delete the object from the heap after you're done with it. When the method is called and returned, the only reference to this String object, s, is set to null and removed from the thread stack. The JVM garbage collector detects that the number of references to this String object is 0, indicating no code may access or use the object any longer. When the garbage collector has an opportunity to run, it treats this String object as garbage, removes it from the Java heap, and frees the memory allocated for it. Java memory leaks If a program holds a reference to a heap chunk that is not used during the rest of its life, it is considered a memory leak because the memory could have been freed and reused. GC won't reclaim it due to the reference being held by the program. A Java program could run out of memory due to such leaks. Java memory leaks are mostly a result of non-obvious programming errors. Finding Reason for memory leaks Usually, solving a memory leak problem is a two-step process:identifying which Java classes caused the memory leak, and then determining where in the application leak occurred. The goal of the first step is easy to achieve with the tools The second step is more challenging, because tracking the relevant information in a profiling tool slows down performance. The profiler discussed in this article doesnot provide the detailed information needed to track down the source of memory leaks inthe code, partly because of performance considerations. Some tools may be configured to collect such information with significant adverse impact on performance, such as Hprof and other third party tools.Given the scale of J2EE applications and the J2EE environment associated with them, performance concerns often prevent developers from tracking sufficient information using those tools. To simplify the discussion of resolving the difficulty in the second step in this article, memory leaks are divided into two types. 1. Rarely used object leak 2. Frequently used object leak Preventing memory leaks You can prevent memory leaks by watching for some common problems. Collection classes, such as hashtables and vectors, are common places to find the cause of a memory leak. This is particularly true if the class has been declared static and exists for the life of the application. Another common problem occurs when you registera class as an event listener without bothering to unregister when the class is no longer needed. Also, many times member variables of a class that point to other classes simply needto be set to null at the appropriate time. |
Saturday, February 17, 2007
How does Java allocate stack and heap memory? Explain re-entrant, recursive and idempotent
| Each time an object is created in Java it goes into the area of memory known as heap. The primitive variables like int and double are allocated in the stack, if they are local method variables and in the heap if they are member variables (i.e. fields of a class). In Java methods local variables are pushed into stack when a method is invoked and stack pointer is decremented when a method call is completed. In a multi-threaded application each thread will have its own stack but will share the same heap. This is why care should be taken in your code to avoid any concurrent access issues in the heap space. The stack is threadsafe (each thread will have its own stack) but the heap is not threadsafe unless guarded with synchronisation through your code. A method in stack is reentrant allowing multiple concurrent invocations that do not interfere with each other. A function is recursive if it calls itself. Given enough stack space, recursive method calls are perfectly valid in Java though it is tough to debug. Recursive functions are useful in removing iterations from many sorts of algorithms. All recursive functions are re-entrant but not all re-entrant functions are recursive. Idempotent methods are methods, which are written in such a way that repeated calls to the same method with the same arguments yield same results. For example clustered EJBs, which are written with idempotent methods, can automatically recover from a server failure as long as it can reach another server. ![]() |
Sunday, February 11, 2007
What is serialization? How would you exclude a field of a class from serialization?
| Serialization is a process of reading or writing an object. It is a process of saving an object’s state to a sequence of bytes, as well as a process of rebuilding those bytes back into a live object at some future time. An object is marked serializable by implementing the java.io.Serializable interface, which is only a marker interface -- it simply allows the serialization mechanism to verify that the class can be persisted, typically to a file. ![]() |
Friday, February 02, 2007
What are some of the best practices relating to Java collection?
| Use ArrayLists, HashMap etc as opposed to Vector, Hashtable etc, where possible to avoid any synchronization overhead. Even better is to use just arrays where possible. If multiple threads concurrently access a collection and at least one of the threads either adds or deletes an entry into the collection, then the collection must be externally synchronized. This is achieved by: List myList = Collections.synchronizedList (myList); Map myMap = Collections.synchronizedMap (myMap); Set the initial capacity of a collection appropriately (e.g. ArrayList, HashMap etc). This is because collection Avoid where possible The code below is hard to maintain and understand by List myOrder = new ArrayList() Example 2: Better approach
(VO is an acronym for Value Object).
|

