Java Performance Tuning Interview Questions And Answers

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.



Next >>

 

1. How do you optimize a JSP page?


Use jspInit() method to cache static data
Use StringBuffer rather than using + operator when you concatenate multiple strings
Use print() method rather than println() method
Use ServletOutputStream instead of JSPWriter to send binary data
Initialize the 'out' object (implicit object) with proper size in the page directive.
Flush the data partly
Minimize code in the synchronized block
Set the content length
Release resources in jspDestroy() method.
Give 'false' value to the session in the page directive to avoid session object creation.
Use include directive instead of include action when you want to include the child page content in the translation phase.
Avoid giving unnecessary scope in the 'useBean' action.
Do not use custom tags if you do not have reusability.
Use application server caching facility
Use Mixed session mechanisms such as 'session' with hidden fields
Use 'session' and 'application' as cache.
Use caching tags provided by different organizations like openSymphony.com
Remove 'session' objects explicitly in your program whenever you finish the task
Reduce session time out value as much as possible
Use 'transient' variables to reduce serialization overhead if your session tracking mechanism uses serialization process.
Disable JSP auto reloading feature.
Use thread pool for your JSP engine and define the size of thread pool as per application requirement.


2. How do you optimize arrays, vectors and collections?


Create copies of simple array by initializing them through loops or by using System.arraycopy(), create copies of complex arrays by cloning them
Iterator.hasNext() and Enumerator.hasMoreElements() need not be repeatedly called when the size of the collection is known. Use collection.size() and a loop counter instead.
ArrayList is faster than Vector
Go for a non-synchronized version of collection unless used in a threaded application
LinkedList is faster than ArrayList for inserting elements to the front of the array, but slower at indexed lookup
Accessing arrays is much faster than accessing vectors, String, and StringBuffer
Vector is convenient to use, but inefficient. Ensure that elementAt() is not used inside a loop.
Re-use Hashtables by using Hashtable.clear().
Removing elements from a Vector will necessitate copying within the Vector if the element is removed from anywhere other than the end of the collection.
Presizing collections to the expected size is more efficient than using the default size and letting the collection grow.
For multidimensional arrays store a reference for the currently accessed row in a variable.
When adding multiple items to a collection, add them all in one call if possible.


3. How do you optimize entity beans?


Tune the entity beans pool size to avoid overhead of creation and destruction of beans.
Tune the entity beans cache size to avoid overhead of activation, passivation and database calls.
Use setEntityContext() method to cache bean specific resources.
Release acquired resources in unSetEntityContext() method
Use Lazy loading to avoid unnecessary pre-loading of child data.
Choose optimal transaction isolation level to avoid blocking of other transactional clients.
Use proper locking strategy.
Make read-only entity beans for read only operations.
Use dirty flag to avoid unchanged buffer data updation.
Commit the data after the transaction completes to reduce database calls in between transaction.
Do bulk updates to reduce database calls.
Use CMP rather than BMP to utilize built-in performance optimization facilities of CMP.
Use ejbHome() methods for global operations.
Tune connection pool size to reduce overhead of creation and destruction of database connections.
Use JDBC tuning techniques in BMP.
Use direct JDBC rather than using entity beans when dealing with huge data such as searching a large database.
Use business logic that is specific to entity bean data.


4. How do you optimize exceptions?


Be specific while handling the exception in your catch block.
Be specific while throwing exception in your throws clause.
Do not use Exception Handling to control programming flow.
Very little overhead is imposed by using exception handling mechanism unless an exception occurs or thrown a new exception object explicitly.
Always use the finally block to release the resources to prevent resource leaks.
Handle exceptions locally wherever possible.
Do not use Exception handling in loops.


5. How do you optimize IO?


Use Buffered IO classes
File information such as file length requires a system call and can be slow? Don?t use it often. Similarly, System.currentTimeMillis() uses a native call to get current time. Make sure your production code does not have this statement.
Many java.io.File methods are system calls which can be slow
Use BufferedIO streams to access URLConnection sInput/Output streams.
Use the transient keyword to define fields to avoid having those fields serialized. Examine serialized objects to determine which fields do not need to be serialized for the application to work.
Increase server listen queues for high load or high latency servers.


Your Ad Here

6. How do you optimize Java Message Service (JMS)


Start producer connection after you start consumer.
Use concurrent processing of messages.
Close the Connection when finished.
Choose either DUPS_OK_ACKNOWLEDGE or AUTO_ACKNOWLEDGE rather than CLIENT_ACKNOWLEDGE.
Control transactions by using separate transactional session for transactional messages and non-transactional session for non-transactional messages.
Close session object when finished.
Make Destination with less capacity and send messages accordingly.
Set high Redelivery delay time to reduce network traffic.
Set less Redelivery limit for reducing number of message hits.
Choose non-durable messages wherever appropriate to avoid persistence overhead.
Set optimal message age (TimeToLive value).
Receive messages asynchronously.
Close Producer/Consumer when finished.
Choose message type carefully to avoid unnecessary memory overhead.
Use 'transient' key word for variables of ObjectMessage which need not be transferred.


7. How do you optimize loops and conditional statements?


Reduce the number of temporary objects being used,especially in loops.
Use short-circuit Boolean operators instead of normal Boolean operators
Eliminate unnecessary repeated method calls from loops
Move loop invariants outside the loop.
Perform the loop backwards (this actually performs slightly faster than forward loops do). [Actually it is converting the test to compare against 0 that makes the difference].
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.
Use exception terminated infinite loops for long loops.
Whatever can be calculated outside of a loop should be calculated outside of the loop.
For multidimensional arrays store a reference for the currently accessed row in a variable.
Cache the size of the collection in a local variable to use in a loop instead of repeatedly calling collection.size().


8. How do you optimize message driven beans?

 
Tune the Message driven beans pool size to promote concurrent processing of messages. 
Use setMesssageDrivenContext() or ejbCreate() method to cache bean specific resources. 
Release acquired resources in ejbRemove() method. 
Use JMS tuning techniques in Message driven beans. 


9. How do you optimize methods in a class?


Avoid creating temporary objects within frequently called methods
Define methods that accept reusable objects to be filled in with data, rather than methods that return objects holding that data.
Use methods that alter objects without making copies
Eliminate repeatedly called methods where alternatives are possible [For example, if you are processing a vector in a loop, get the size of the vector in the beginning in a length local variable rather than calling size() method on the vector in the condition part of the for loop]
Use private and static methods and final classes to encourage inlining by the compiler
Inline methods manually where it is appropriate
Keep methods short and simple to make them automatic inlining candidates
Access to private member by the inner classes to the enclosing class goes by a method call even if it is not intended to.
Keep synchronized methods out of loops if you possibly can.


10. How do you optimize servlets?


Use init() method to cache static data
Use StringBuffer rather than using + operator when you concatenate multiple strings
Use print() method rather than println() method
Use ServletOutputStream rather than PrintWriter to send binary data
Initialize the PrintWriter with proper size
Flush the data partly
Minimize code in the synchronized block
Set the content length
Release resources in destroy() method.
Implement getLastModified() method to use browser cache and server cache
Use application server caching facility
Use Mixed session mechanisms such as HttpSession with hidden fields
Remove HttpSession objects explicitly in your program whenever you finish the task
Reduce session time out value as much as possible
Use 'transient' variables to reduce serialization overhead if your HttpSession tracking mechanism uses serialization process.
Disable servlet auto reloading feature.
Use thread pool for your servlet engine and define the size as per application requirement.


Your Ad Here

Next >>

 
Databases Questions and Answers
 
DB2 Interview Questions and Answers
IMS Interview Questions and Answers
MYSQL Interview Questions and Answers
Oracle Interview Questions and Answers
PL/SQL Interview Questions And Answers
Quel Interview Questions and Answers
SQL Interview Questions and Answers

 
 
.NET Interview Questions and Answers
 
ASP Interview Questions and Answers
C# Interview Questions and Answers
Visual Basic Interview Questions and Answers
 
J2EE Interview Questions and Answers
 
Enterprise Java Beans (EJB) Interview Questions And Answers
Hibernate Interview Questions And Answers
Jave Messaging Service (JMS) Interview Questions And Answers
Jave Server Faces (JSF) Interview Questions And Answers
Java Server Pages (JSP) Interview Questions And Answers
Servlets Interview Questions And Answers
Spring Framework Interview Questions And Answers
Struts Framework Interview Questions And Answers
 
JAVA Interview Questions and Answers
 
Core Java Interview Questions and Answers
Java Platform, Micro Edition (Java ME) Interview Questions And Answers
JAVA GUI Interview Questions and Answers
Java Performance Tuning Interview Questions And Answers
JUnit Interview Questions And Answers
Remote Method Invocation (RMI) Interview Questions And Answers
Unified Modeling Language (UML) Interview Questions And Answers
 
Java Application Server Interview Questions And Answers
 
Tomcat Application Server Interview Questions And Answers
WebLogic Application Server Interview Questions And Answers
 
Mainframe Interview Questions and Answers
 
CICS Interview Questions and Answers
COBOL Interview Questions and Answers
IMS Interview Questions and Answers
JCL Interview Questions and Answers
REXX Interview Questions and Answers
TELON Interview Questions and Answers
VSAM Interview Questions and Answers
 
Object Oriented Language Questions and Answers
 
C Interview Questions and Answers
C++ Interview Questions and Answers
Eiffel Interview Questions and Answers
J2EE Interview Questions and Answers
JAVA Interview Questions and Answers
Sather Interview Questions and Answers
Small talk Interview Questions and Answers
 
Operation Systems Interview Questions and Answers
 
MS DOS Interview Questions and Answers
Unix OS Interview Questions and Answers
Windows Interview Questions and Answers
 
Scripting languages Interview Questions and Answers
 
Java Script Interview Questions and Answers
Practical Extraction and Reporting Language Interview Questions and Answers
PHP Interview Questions and Answers
VBScript Interview Questions and Answers
 
UserInterfaces Questions and Answers
 
Foxit Interview Questions and Answers
Glade Interview Questions and Answers
Tweak Interview Questions and Answers
 
WEB Technologies Questions and Answers
 
AJAX Interview Questions and Answers
HTML Interview Questions and Answers
WML Interview Questions and Answers
XML Interview Questions and Answers
 
Interview Tips And Some Common Questions
 
Behavioral Interview Questions And Answers


Your Ad Here