How to fix resource leaks in java
One way to fix resource leaks quickly is a straightforward –
restarting periodically. It provides good protection against slow resource leaks.
But it is very very important to spot applications that are draining resources
too quickly, so that any software bugs can be fixed.
Leaks that prevent continuous server operation for at least 24 hours must be
fixed in the application code, not just by restarting your application servers.
Common java programming mistakes are:
- Not returning the resource to the pool after handling an error or not removing it from the pool after handling an error
- Just relying on the garbage collector ( GC ) to invoke finalize() and free resources is very bad. Never rely on the garbage collector to manage any resource other than memory
- Not discarding old object references which prevent recycling the memory occupied by the objects.
- Monitoring resource usage should be a combination of code instrumentation and external monitoring utilities. With code instrumentation, calls to an application-provided interface, or calls to a system-provided interface like Oracle Dynamic Monitoring System (DMS), are inserted at key points in the application’s resource usage lifecycle. Done correctly, this can give the most accurate picture of resource use. Unfortunately, the same programming errors that cause resource leaks are also likely to cause monitoring errors. That is, you may forget to release the resource, or forget to monitor the release of the resource.
OS commands like vmstat or ps command in UNIX, provides process level details such as the amount of memory allocated, the number of threads and state of threads, or number of network connections etc. We could use this information to detect a resource leak. There are some good commerical products also available to detect resource leaks.
In addition to compromising availability, resource leaks and overuse decrease performance.
Related posts:

0 Comments.