Java Performance Tuning
Java(TM) - see bottom of page
Our valued sponsors who help make this site possible
JProfiler: Get rid of your performance problems and memory leaks!
Training online: Concurrency, Threading, GC, Advanced Java and more ...
Tips September 29th, 2002
Get rid of your performance problems and memory leaks!
Get rid of your performance problems and memory leaks!
Back to newsletter 022 contents
Example of developing CMP 2.0 Entity Beans (Page last updated September 2002, Added 2002-09-30, Author Pravin Tulachan, Publication InformIT). Tips:
- CMP implementations are often written to support particular databases, allowing them to use optimal performance features for that database.
JDBC drivers (Page last updated September 2002, Added 2002-09-30, Author Steven J. Vaughan-Nichols, Publication SDTimes). Tips:
- Use a JDBC 3.0 compliant driver
- Sun's JDBC-ODBC bridge doesn't support concurrent access, but also allows deadlocks.
Benchmarking XML parsing (Page last updated September 2002, Added 2002-09-30, Author Cameron Laird, Publication IBM). Tips:
- The performance of two functionally identical programs can often vary by orders of magnitude.
- It's often less costly to tune a technology that an organization can accept, rather than impose an unfamiliar technology, even if the latter option provides more speed.
- Managing DOM instances in memory taxes memory allocation schemes, so the overall speed of an XML parser is often quite sensitive to the memory allocation patterns of the parser.
- Java-coded DOM parsers are particularly expensive in memory costs, with a total run-time footprint of up to five times the size of an XML document instance. SAX parsers are more efficient.
- Special purpose parsers are often faster (the article mentions some).
Changing icon look with composition (Page last updated September 2002, Added 2002-09-30, Authors Joe Winchester, Renee Schwartz, Publication IBM). Tips:
- Icons (and other graphics) which are changed in color or shading can use two sets of graphics, but can also use composition for example with XORing or pixel manipulation, as described in the article.
Processing Stack Trace Data (Page last updated September 2002, Added 2002-09-30, Author Richard G. Baldwin, Publication Earthweb). Tips:
- From 1.4, Throwable.getStackTrace() and StackTraceElement objects are available to examine indiviual elements of the runtime stack.
ArrayList/LinkedList/HashMap performance (Page last updated September 2002, Added 2002-09-30, Author Glen McCluskey, Publication Sun). Tips:
- ArrayList is faster than LinkedList for random access and for algorithms that follow a random access pattern, such as binary searching.
- LinkedList is faster than ArrayList for repeated insertions to the front of the list.
- HashMap lookup is faster than binary search on an ArrayList.
- Appending elements to the end of a list has a fixed averaged cost for both ArrayList and LinkedList. For ArrayList, appending typically involves setting an internal array location to the element reference, but occasionally results in the array being reallocated. For LinkedList, the cost is uniform and involves allocating an internal Entry object.
- Inserting or deleting elements in the middle of an ArrayList implies that the rest of the list must be moved. Inserting or deleting elements in the middle of a LinkedList has fixed cost.
- A LinkedList does not support efficient random access.
- An ArrayList has space overhead in the form of reserve capacity at the end of the list. A LinkedList has significant space overhead per element.
New 1.4 network features (Page last updated September 2002, Added 2002-09-30, Author Qusay H. Mahmoud, Publication Sun). Tips:
- 1.4 now directly supports setting timeouts for socket connections with a parameter to the connect() method. This differs from the SO_TIMEOUT connection configuration parameter, which only affects subsequent reads on the established connection.
- 1.4 improved connected handling of datagram sockets so that the OS level can test address access, which is faster than previously.
Creating a JDBC Log Handler (Page last updated September 2002, Added 2002-09-30, Author Jeff Heaton, Publication Earthweb). Tips:
- Logging data to a database can be useful. Article describes how to create a log handler for the logging API which logs to a database.
Managing thread death (Page last updated September 2002, Added 2002-09-30, Author Brian Goetz, Publication IBM). Tips:
- Use the ThreadGroup uncaughtException handler to handle restarting or logging threads dieing unexpectedly.
equals() versus == (Page last updated September 2002, Added 2002-09-30, Author Sreekanth Iyer, Publication IBM). Tips:
- intern() is expensive and slow, but can be used to internalize strings which can reduce duplicate strings and allows comparsion by the faster identity (==) operator rather than the equality method.
Industrial-strength JNDI optimization (Page last updated September 2002, Added 2002-09-30, Author Brett McLaughlin, Publication IBM). Tips:
- Cache the context objects.
- EJB applications can spend a significant proportion of time on remote EJB home lookups. Cache the home objects.
- Use an EJBHomeFactory (pattern) to manage lookups and cached home objects.
1.4.1 Changes (Page last updated September 2002, Added 2002-09-30, Author Sun, Publication Sun). Tips:
- -XX:+UseConcMarkSweepGC - This flag turns on concurrent garbage collection. This collector executes mostly concurrently with the application. It trades the utilization of processing power that would otherwise be available to the application for shorter garbage collection pause times.
- -XX:+UseParallelGC - This flag enables garbage collection to occur on multiple threads for better performance on multiprocessor machines.
Non-blocking sockets (Page last updated September 2002, Added 2002-09-30, Author Giuseppe Naccarato, Publication OnJava). Tips:
- Use non-blocking sockets to optimize asynchronous high-performance read/write operations.
Yet another "use StringBuffer not String concatenation" tip (Page last updated September 2002, Added 2002-09-30, Author Ramneek Handa, Publication DevX). Tips:
- Use StringBuffer not String concatenation.
Buffered Iterator (Page last updated September 2002, Added 2002-09-30, Author Ryan Brase, Publication Builder.com). Tips:
- Use a buffered iterator to access elements of a collection while that collection is being built [article discusses building such a buffered iterator].
Choosing a JDBC driver (Page last updated August 2002, Added 2002-09-30, Author DataDirect, Publication DevX). Tips:
- Make sure the driver closes associated resources, like closing ResultSets when the generating Statement has been closed.
- Make sure the driver supports the load you expect to put on it.
- Does the driver have a tracing mode or utility.
JDBC tuning (Page last updated September 2002, Added 2002-09-30, Author Robert Brunner, Publication DevX). Tips:
- Using autocommit mode every SQL statement is in its own transaction, which is bad for performance.
- Batch processing and updateable ResultSets can improve performance.
- Type 4 JDBC drivers generally offer the highest performance.
- A "spy" utility helps to trace JDBC communications.
Choosing a JDBC driver (Page last updated August 2002, Added 2002-09-30, Author Barrie Sosinsky, Publication DevX). Tips:
- You can optimize data access performance through batching.
- Type 3 and Type 4 JDBC drivers are both pure Java drivers, and therefore, offer the best performance (Driver types: 1, JDBC-ODBC bridge, plus an ODBC driver; 2, native API, part-Java driver; 3, pure Java driver for database middleware; 4, pure Java driver for direct-to-database).
- Connection pooling is an important performance feature.
- A disconnected RowSet can be used to cache rows.
- PreparedStatement pooling is good for performance, especially when it works with connection pooling.
Monitoring web services (Page last updated September 2002, Added 2002-09-30, Authors Gunjan Samtani & Dimple Sadhwani, Publication Web Services Journal). Tips:
- Types of web service performance monitoring: Availability test (Checks whether a Web service is present and ready for immediate use); Performance test (Measures the throughput i.e. requests/second, and latency i.e. response time; Stress test (Determines the maximum load the web service can manage).
- Web services invocation operations are expensive. Use corase-grained services i.e., where a lot of work is to be done and a lot of information has to be returned.
- Asynchronous messaging can improve throughput at the cost of latency.
- Cache wherever possible.
- Avoid real-time data aggregation from other web services.
- Use a stripped down XML parser that only performs essential parsing.
- Avoid chaining Web services.
EJB primary keys (Page last updated August 2002, Added 2002-09-30, Author Saad Rehmani, Publication Java Developers Journal). Tips:
- Integer keys have better performance than String keys. Author recommends using an container-generated unique integer key.
EJBs or plain Servlets? (Page last updated September 2002, Added 2002-09-30, Author Michael Bardash, Gerhard Bayer, Max Dolgicer, Publication ATDmag). Tips:
- If an application is presentation-centric and does not require support for high-end middleware services such as distributed transactions, persistence, application-level load balancing, state management and asynchronous messaging, then plain servlets are sufficient. If an application requires at least a partial list of such high-end middleware services, then EJBs are required.
- Server clustering, DB connection pooling and location transparency facilitate scalability.
- Servlet scalability is not stipulated by the servlet API, but is based mostly on proprietary, vendor-created implementations of servlet pools and load-balancing mechanisms. EJB architecture was designed for scalability. Put another way, servlets can scale at the server level, EJBs can scale at the architecture level.
Stylesheet execution optimization TRaX (Page last updated September 2002, Added 2002-09-30, Author Jeff Ryan, Publication Earthweb). Tips:
- TRaX can compile stylesheets and hold them in memory, dramatically improving performance.
Profiling and performance analysis (Page last updated October 2002, Added 2002-09-30, Author Peter Varhol, Publication Java Pro). Tips:
- [Article discusses profiling as timing methods, performance analysis as more in-depth, and the difference between sampling and full execution logging for profilers].
Opimizing I/O (Page last updated September 2002, Added 2002-09-30, Author Merlin Hughes, Publication IBM). Tips:
Back to newsletter 022 contents
- ByteArrayOutputStream.toByteArray() returns a copy of the underlying array, which can be an inefficient use of memory.
- StringBuffer controls write access to its internal array. It can can export the array to a String and copies the array only when necessary; that is, when it has exported a String and a caller subsequently modifies the StringBuffer. If no such modification occurs, then no unnecessary copying will be performed.
- [Article discusses a variant of the ByteArrayOutputStream class which can export access to the internal byte array with a read-only InputStream].
- The internal 1024-byte buffer of piped streams is inflexible for different usage scenarios; it is just too small for large volumes of data.
- Array-based operations of piped streams simply call through to an inefficient byte-by-byte copy operation. This operation is itself synchronized, resulting in extremely heavy lock contention.
- If a piped stream becomes empty or full and a thread is blocked on this state changing, the thread is awakened even if just a single byte is read or written. In many cases, it will use this single byte and immediately block again, resulting in little useful work being done.
- [Article discusses improved piped streams which avoid some performance problems of SDK piped streams].
Last Updated: 2022-06-29
Copyright © 2000-2022 Fasterj.com. All Rights Reserved.
All trademarks and registered trademarks appearing on JavaPerformanceTuning.com are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. JavaPerformanceTuning.com is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
RSS Feed: http://www.JavaPerformanceTuning.com/newsletters.rss
Trouble with this page? Please contact us