Implementing a SoftReference-based HashMap (cont'd)

Test Code
Here is some test code that demonstrates that I'm not talking nonsense. I invite you to use this code to test the SoftHashMap yourself.

Soft and weak references are quite difficult to experiment with, because there is a lot of freedom left to the writers of the JVM about how they can implement them. I wish the implementation would hold back longer before removing these references; that the JVM would wait until it is really running low on memory before clearing them, but I am not the one who wrote the JVM.

//: SoftHashMapTest.java
import java.lang.ref.*;
import java.util.*;

public class SoftHashMapTest {
  private static void print(Map map) {
    System.out.println("One=" + map.get("One"));
    System.out.println("Two=" + map.get("Two"));
    System.out.println("Three=" + map.get("Three"));
    System.out.println("Four=" + map.get("Four"));
    System.out.println("Five=" + map.get("Five"));
  }
  private static void testMap(Map map) {
    System.out.println("Testing " + map.getClass());
    map.put("One", new Integer(1));
    map.put("Two", new Integer(2));
    map.put("Three", new Integer(3));
    map.put("Four", new Integer(4));
    map.put("Five", new Integer(5));
    print(map);
    byte[] block = new byte[10*1024*1024]; // 10 MB
    print(map);
  }
  public static void main(String[] args) {
    testMap(new HashMap());
    testMap(new SoftHashMap(2));
  }
}

Many thanks to Sydney Redelinghuys for his input and corrections in this article.
Dr. Heinz Kabutz owns Maximum Solutions, a Cape Town-based consulting firm that specializes in Java technology. He spends the majority of his time programming Java OO applications and also advises companies who wish to embrace Java as a technology. He has been a lead programmer in one of the first big Java developments in South Africa, now consisting of almost 600,000 lines of Java code. Heinz, who completed his doctorate in computer science at the University of Cape Town, South Africa, in 1996, has been programming in Java since 1997. He can be reached at h.kabutz@computer.org. Editor's note: This article first appeared Dr. Kabutz's "The Java(tm) Specialists' Newsletter"; to subscribe, send an email to join-thejavaspecialists@burst.sparklist.com.


The SoftHashMap Itself

Back to Introduction

In this Article
Introduction Test Code
The SoftHashMap Itself








FEATURE SOFTWARE:
dtSearch Web
Add power searching to your web site.
Buy Now!

Encrypt It
Encrypt and Decrypt Data, Passwords and Files within your application.
Buy Now!

FEATURE BOOK:
PointBase Mobile Edition
Enable local data access for mobile users.
Buy Now!
Sun JavaDoc: java.util.WeakHashMap

Sun JavaDoc: java.lang.ref.WeakReference

From the DevX Tip Library: What is a WeakHashMap?

From the DevX Tip Library: Why Would You Use a WeakHashMap?

DevX Java Zone

Java Pro Magazine

Tip of the Day
Use Math.PI and Math.E for Scientific Calculations

Download of the Week
Ebitec JMS Mail Bridge

TALK BACK
What has been your experience using WeakReferences and WeakHashMaps? Let us know in the java.general discussion group!




 
Sponsored Links

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map
Jupiterweb networks

internet.comearthweb.comDevx.comClickZ

Search Jupiterweb:

Jupitermedia Corporation has four divisions:
JupiterWeb, JupiterResearch, JupiterEvents, and JupiterImages

Copyright 2004 Jupitermedia Corporation All Rights Reserved.
Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Jupitermedia Corporate Info | Newsletters | Tech Jobs | E-mail Offers

Copyright Information/Privacy Statement