orgecc


WeakHashMap usage and problems

Posted in java by elifarley on the October 31st, 2007

Since a WeakHashMap should not be used as a cache (because its keys will be garbage collected too soon), what is it good for ?

- to keep information about objects without preventing them from being GC’d (according to Scott Vachalek).

Bob Lee tells us that “the most common use case for WHM is to use Class instances as keys” (He also points us to the Google Collections Library).

These comments and other really interesting information can be found at a post titled “Why WeakHashMap Sucks“, by Dr. Cliff Click, Chief JVM Architect of Azul Systems.

But since a SoftHashMap (a Map using SoftReference instead of WeakReference) would be useful as a cache, why has Sun decided to provide a WeakHashMap instead of a SoftHashMap ?

Here we have some more related links:

A ‘reference’ to an old bug

Posted in java by elifarley on the July 27th, 2007

Probably the fact that so few experienced Java developers know about the existence of weak references has to do with the bewildering explanations given in the Javadocs for the WeakHashMap and the java.lang.ref package.

If you agree, I urge you to vote for bug 4478625, which was submitted in 2001-JUL-11, but is still marked as ‘in progress’:

Bug 4478625: Better explanation of WeakReference, SoftReference, WeakHashMap

Reducing installed application footprint with compressed executable jars

Posted in java by elifarley on the February 27th, 2007

Though you can significantly reduce the download size of a Java application by applying Pack200 compression to its jars, the installed app size is still the same, since once downloaded, the jars must be expanded.

Wouldn’t it be nice if we could keep all jars tightly compressed even after installing the application ?
(more…)

Using RandomAccessFile to access data which doesn’t come from a local file.

Posted in java by elifarley on the February 27th, 2007

Some libraries and applications rely on a RandomAccessFile instance to process its IO tasks, and thus are usually limited to access data from a local file.
If an application needs to access data from another source (from an SFTP file, an HTTP file, CIFS-accessible file, a file inside a zip, tar, gzip or bzip2 archive, etc), it has to use another algorithm - the process isn’t transparent.

Such application could be benefited by an adapter class providing a “RandomAccessFile” view of arbitrary data sources, allowing it to transparently access data from different sources.

Example: a database server using a RandomAccessFile instance to access its data from a local file would automatically be able to access it from a gzip-compressed file.
(more…)


WP-Highlight