Home | About Us | Contact Us | Terms Of Use
J2SE 5.0 includes a number of new features and enhancements to improve performance in many areas of the platform. Improvements to new language features include: additions to the virtual machine, enhancements to the base and integration libraries, the user interface, deployment, tools and architectures, and OS & hardware platforms. Enhancements to program execution speed include: Garbage collection ergonomics, StringBuilder class, Java 2D technology enhancements, and performance and memory usage improvements to image I/O.
11. How do you optimize stateful session beans?
Tune Stateful session beans cache size to avoid overhead of activation and passivation process.
Set optimal Stateful session bean age(time-out) to avoid resource congestion.
Use 'transient' key word for unnecessary variables of Stateful session bean to avoid serialization overhead.
Remove Stateful session beans explicitly from client using remove() method.
12. How do you optimize stateless session beans?
Tune the Stateless session beans pool size to avoid overhead of creation and destruction of beans. Use setSessionContext() or ejbCreate() method to cache bean specific resources. Release acquired resources in ejbRemove() method
13. How do you optimize Strings?
Use StringBuffer instead of String concat ?+? operator
Formatting numbers using java.text.DecimalFormat is always slower than Double.toString(double) method, because internally java.text.DecimalFormat() calls Double.toString(double) then parses and converts the results.
Convert String to char[] arrays to process characters rather than accessing one at a time using String.charAt() method
Creating Double from string is slow
Intern() Strings to enable (==) comparisions
Use char arrays for all character processing in loops, rather than using String or StringBuffer classes
Set the initial string buffer size to maximum if it known.
StringTokenizer is very inefficient, and can be optimized by storing the string and delimiter in a character array instead of in String
DON?T create static strings via new().
Where the compiler cannot resolve concatenated strings at compile time, the code should be converted to StringBuffer appends, and the StringBuffer should be appropriately sized rather than using the default size.
The compiler concatenates strings where they are fully resolvable, so don t move these concatenations to runtime with StringBuffer.
14. How do you optimize threads?
Avoid synchronization where possible
Code a multi-thread for multi-processor machine.
Synchronizing on method rather than code blocks is slightly faster
Polling is only acceptable when waiting for outside events and should be performed in a "side" thread. Use wait/notify instead.
Prioritize threads. Use notify instead of notifyAll. Use synchronization sparingly.
Keep synchronized methods out of loops if you possibly can.
Maximize thread lifetimes and minimize thread creation/destruction cycles.
Use Thread pools where these improve performance.
Use Thread.sleep() instead of a for loop for measured delays.
Use a separate timer thread to timeout socket operations
Use more server threads if multiple connections have high latency.
15. How will you optimize performance in JDBC?
Use batch transactions.
Choose right isolation level as per your requirement. TRANSACTION_READ_UNCOMMITED gives best performance for concurrent transaction based applications. TRANSACTION_NONE gives best performance for non-concurrent transaction based applications.
Use PreparedStatement when you execute the same statement more than once.
Use CallableStatement when you want result from multiple and complex statements for a single request.
Use batch update facility available in Statements.
Use batch retrieval facility available in Statements or ResultSet.
Set up proper direction for processing rows.
Use proper getXXX() methods.
Close ResultSet, Statement and Connection whenever you finish your work with them.
Write precise SQL queries.
Cache read-only and read-mostly tables data.
Fetch small amount of data iteratively rather than whole data at once when retrieving large amount of data like searching database etc.
16. What are common optimizing practices for a class?
Use primitive data types instead of objects as instance variables
Keep constructors simple and inheritance hierarchies shallow
Avoid initializing instance variable more than once
Eliminate unnecessary type casts
Enumerators are faster than Iterators due to the specific implementation
Ints are the fastest datatype
Use static variables for fields that only need to be assigned once
Final classes can be faster
Use interfaces, so that the actual underlying data structure can be actually changed without impacting the client program
Null out references when they are no longer needed so that gc can collect them when it is running.
Avoid character processing using methods like charAt(), setCharAt() methods
Use local variables in preference to class variables
Compound operators such as n += 4; are faster than n = n + 4; because fewer bytecodes are generated.
Shifting by powers of two is faster than multiplying.
Multiplication is faster than exponentiation.
int increments are faster than byte or short increments.
Floating point increments are much slower than any integral increment.
It can help to copy slower-access vars to fast local vars if you are going to operate on them repeatedly, as in a loop.
For some applications that access the date a lot, it can help to set the local timezone to be GMT, so that no conversion has to take place.
Minimize creation of short-lived objects.
Avoid excessive writing to the Java console
Minimize the number of JNI calls
Avoid unnecessary instanceof operation
Avoid using long divides.
Persistency adds overheads that make persistent objects slower.
Use pipelining and parallelism. Designing applications to support lots of parallel processes working on easily distinguished subsets of the work makes the application faster. If there are multiple steps to processing, try to design your application so that subsequent steps can start working on the portion of data that any prior process has finished, instead of having to wait until the prior process is complete.
Use bitshift instead of multiplying or dividing by powers of 2.
Scope of variables can impact performance.
Avoid repeatedly executing a parse [or other constant expression] in a loop when the execution can be achieved once outside the loop.
Use exception only where you need them
17. What are common optimizing practices for EJB?
Use Local interfaces that are available in EJB2.0 if you deploy both EJB Client and EJB in the same EJB Server.
Use Vendor specific pass-by-reference implementation to make EJB1.1 remote EJBs as Local EJBs if you deploy both EJB Client and EJB in the same EJB Server.
Wrap entity beans with session beans to reduce network calls and to promote declarative transactions. Optionally, make entity beans as local beans where appropriate.
Make coarse grained session and entity beans to reduce network calls.
Control serialization by modifying unnecessary data variables with 'transient' key word to avoid unnecessary data transfer over network.
Cache EJBHome references to avoid JNDI lookup overhead.
Avoid transaction overhead for non-transactional methods of session beans by declaring 'NotSupported' or 'Never' transaction attributes that avoid further propagation of transactions.
Set proper transaction age(time-out) to avoid large transaction.
Use clustering for scalability.
Tune thread count for EJB Server to increase EJB Server capacity.
Choose servlet's HttpSession object rather than Stateful session bean to maintain client state if you don't require component architecture and services of Stateful session bean.
Choose best EJB Server by testing with ECperf tool kit.
Choose normal java object over EJB if you don't want built-in services such as RMI/IIOP, transactions, security, persistence, resource pooling, thread safe, client state etc..