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 August 2004
Get rid of your performance problems and memory leaks!
Get rid of your performance problems and memory leaks!
Back to newsletter 045 contents
Tuning garbage collection (Page last updated June 2004, Added 2004-08-30, Author Jack Shirazi Kirk Pepperdine, Publisher IBM). Tips:
- The easiest way to determine if garbage collection and memory utilization issues are having a negative effect on your applications performance is to set the -verbose:gc option.
- Not having enough memory causes the garbage collector to thrash as it tries to satisfy the current need.
- Virtual memory can interact badly with GC in a JVM - you may be better off having no virtual memory or with the JVM memory locked into the machine physical memory.
- Eliminating object leaks for any long running JVMs should be a priority.
- For tuning, systematically apply changes one at a time and measure their effect on the system.
- Try various heap parameter values and garbage collection algorithms for your system.
Metrics of coupling (Page last updated July 2004, Added 2004-08-30, Author Jack Shirazi Kirk Pepperdine, Publisher IBM). Tips:
- By measuring the couplings in your system, you can determine the riskiness of any changes required by performance tuning.
Thread-safe servlets (Page last updated July 2004, Added 2004-08-30, Author Phillip Bridgham, Publisher Javaworld). Tips:
- If a variable is read-only then no harm results in sharing the variable across all instances. For thread-safety, all other variables should use synchronized access or a unique variable for each thread.
- Minimizing the scope of variables helps to reduce their cross-thread visibility. You can often change a wider scoped variable for a smaller scoped variable in conjunction with passing the variable to any other scopes that need access to it.
- Accurately identify which code block truly needs to be synchronized, and synchronize as little as possible.
- Using the SingleThreadModel interface for your serlets is expensive.
Servlet and JSP performance tuning (Page last updated June 2004, Added 2004-08-30, Author Rahul Chaudhary, Publisher Javaworld). Tips:
- Use the HttpServlet init() method for caching data especially obtained from remote calls, like JNDI looked-up DataSource objects.
- Disable the servlet container auto-reloading
- Do not create HttpSessions in JSP pages by default (use <%@ page session="false"%>)
- Do not store large object graphs inside an HttpSession
- Release HttpSessions when done with HttpSession.invalidate()
- Set a session time-out value as low as possible.
- Use gzip compression on outgoing pages
- Do not use SingleThreadModel
- Have the servlet container use a thread pool instead of creating/destroying threads on demand; tune the size of the pool
- Use the include directive rather than include action where possible
- Choose the smallest scope (page|request|session|application) in the useBean action
- Avoid string concatenation.
- Avoid the use of System.out.println
- Use ServletOutputStream instead of PrintWriter for binary output
1.5/5.0 new features (Page last updated June 2004, Added 2004-08-30, Author Calvin Austin, Publisher JavPro). Tips:
- concurrency utility library is based on a popular package from Doug Lea that has been standardized in JSR 166. It provides many useful APIs and tools for writing safer multithreaded code. The APIs include a thread executor task that can be used for managing threads, as well as a range of locks, like the Semaphore; timers; and other synchronization primitives. The JSR also delivers a nanosecond timer, java.lang.System.nanoTime(), for platforms that support that granularity.
- A simple example is the atomic lock, which guarantees that when retrieving a value and setting it the value does not change in the meantime.
- This release also introduces class-data sharing in the Hotspot Java Virtual Machine (JVM). This technology shares read-only class data between multiple running JVMs, which not only can reduce the memory footprint of multiple JVMs but also improves startup time as core JVM classes are prepacked.
- A JMX framework provides a way to also manage or set values-for example, changing the logging level dynamically
- To enable a JVM to be monitored using the standard SNMP ports in the beta 2 release, modify the management.properties file, or supply these options at runtime: java -Dcom.sun.management.snmp.acl=false -Dcom.sun.management.snmp.port=161 -Dcom.sun.management.snmp.trap=162 -Dcom.sun.management.snmp.acl=false -jar Java2Demo.jar
- You can now call Thread.getAllStackTraces() to generate a list of all stack frames from within your application.
- If the JVM aborts you can configure it to call your own error handler program: -XX:OnError="command" optional %p used as process id
- If you have the latest OpenGL drivers, you can now use native hardware acceleration for any Swing application by using this run-time property: java -Dsun.java2d.opengl=true -jar Java2D.jar
- JSR 200 introduces a JAR file compression technology that can be used to dramatically shrink JAR files for network transmission. This compression and inflation can be achieved programmatically by using the javax.pack.Pack200 class. In addition, for simple downloads, you can use the stand-alone tools pack200 and unpack200.
- J2SE 1.5 introduces a disconnected CachedRowSet and WebRowSet implementations. The CachedRowSet contains an in-memory collection of rows retrieved from the database. The connection to the database is managed by the RowSet API and simply needs information about the datasource or database connection details. The rowset can then be updated without tying up database connection resources, and then it can be resynced at a later time.
Concurrency classes in 5.0 (Page last updated May 2004, Added 2004-08-30, Author Brian Goetz, Publisher JavaPro). Tips:
- java.util.concurrent provides: a rich set of industrial-strength, high-performance, scalable, concurrency building blocks
- The two existing thread-safe implementations of Map-Hashtable and Collections.synchronizedMap()-these implementations have poor concurrency characteristics. The single monitor used to synchronize all methods can quickly become a scalability bottleneck on multiprocessor systems, and it is often necessary to lock the entire collection during iteration to avoid throwing ConcurrentModificationException.
- ConcurrentHashMap and ConcurrentLinkedQueue are optimized for efficient access by multiple concurrent threads
- ConcurrentLinkedQueue is a FIFO queue allowing efficient, wait-free, insertions and removals that can overlap each other.
- ArrayBlockingQueue is a bounded, blocking FIFO queue with an insert operation that will block if there is not sufficient room in the array and with a remove operation that will block if there are no elements to be removed. Bounded blocking queues are very useful for preventing queues from consuming arbitrary amounts of memory in the event that producers are inserting elements faster than consumers can remove them
- PriorityQueue and PriorityBlockingQueue implement ordered heaps
- ConcurrentHashMap is an efficient scalable concurrent replacement for Hashtable.
- CopyOnWriteArrayList is optimized for the case where iterations greatly outnumber insertions and removals (ideal for use in managing event listeners).
- The Executor framework provides a mechanism for asynchronously executing, querying, and canceling execution of task objects of type Runnable (and its value-returning variant, Callable) according to an execution policy which can be specified separately from task submission.
- Executor includes a scheduling service, which can support one-time, fixed-rate periodic, and fixed-delay periodic execution of tasks, including shut down and cancellation of pending tasks
- AtomicInteger, AtomicLong, and AtomicReference, which provide thread-safe querying and updating of shared counters, sequence numbers, and references without requiring external synchronization.
- java.util.concurrent.locks.Lock behaves similarly to synchronization, but offers the ability to interrupt a thread that is waiting for a lock, the ability to specify a timeout while waiting for a lock, and multiple wait sets per lock. It also has superior performance characteristics to synchronization
- ReadWriteLock class supports easy implementation of multiple-reader, single-writer, locking disciplines for shared resources.
- java.util.concurrent Semaphore is a counting semaphore that enables up to n callers to hold permits on a given resource at once
- java.util.concurrent CountDownLatch allows one thread to wait for the results of a set of operations executing in other threads to complete
- java.util.concurrent Exchanger allows two threads to rendezvous and exchange data items and which is useful in the case where one thread fills a buffer and one thread empties it
- java.util.concurrent CyclicBarrier allows a set of threads to wait until they all reach a common barrier point.
6 J2EE best practices (Page last updated June 2004, Added 2004-08-30, Author Tarak Modi, Publisher FTPOnline). Tips:
- Client-side validation is to provide the user with quick feedback-to make the application appear more responsive (but it is not a replacement for server-side validation as client-side validation can be bypassed).
- If you use EJBs when they are not required, they can hurt your application's performance.
- A separate data access layer allows many optimizations to be implemented transparently.
Back to newsletter 045 contents
Last Updated: 2021-08-29
Copyright © 2000-2021 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