开发者

google-guava MapMaker .softValues() - values don't get GC-ed, OOME: HeapSpace follows

I am having trouble using the MapMaker from google-guava. Here is the code:

package test;


import java.lang.ref.SoftReference;
import java.util.Map;
import java.util.Random;

import com.google.common.collect.MapEvictionListener;
import com.google.common.collect.MapMaker;


public class MapMakerTest {

private static Random RANDOM = new Random();

private static char[] CHARS =
    ("abcdefghijklmnopqrstuvwxyz" +
     "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
     "1234567890-=!@#$%^&*()_+").toCharArray();

public static void main(String[] args) throws Exception {
    MapEvictionListener<String, String> listener = new MapEvictionListener<String, String>() {

        @Override
        public void onEviction(String key, String value) {
            System.out.println(">>>>> evicted");
        }
    };
    Map<String, String> map = new MapMaker().
        concurrencyLevel(1).softValues().
        evictionListener(listener).makeMap();
    while (true) {
        System.out.println(map.size());
        String s = getRandomString();
        map.put(s, s);
        Thread.sleep(50);
    }
}

private开发者_如何学编程 static String getRandomString() {
    int total = 50000;
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < total; ++i) {
        sb.append(CHARS[RANDOM.nextInt(CHARS.length)]);
    }
    return sb.toString();
}
}

When java is called like: java -Xms2m -Xmx2m -cp guava-r09.jar:. test.MapMakerTest (the heap settings are so small intentionally to easier see what happens) around the 60th iteration it explodes with OutOfMemoryError: HeapSpace.

However, when the map is Map<String, SoftReference<String>> (and according changes in the rest of the code: the listener, and the put), I can see the evictions taking place, and the code simply works, and the values get garbage collected.

In all of the documentation, including this one: http://guava-libraries.googlecode.com/svn/tags/release09/javadoc/index.html, there is no mention of SoftReferences explicitly. Isn't the Map implementation supposed to wrap the values in SoftReference when put is called? I am really confused about the supposed usage.

I am susing guava r09.

Could anyone maybe explain what I am doing wrong, and why my assumptions are wrong?

Best regards, wujek


You use the same object for key and value, therefore it is strongly reachable as a key and is not eligible for garbage collection despite the fact that value is softly reachable:

map.put(s, s); 

Try to use different instances:

map.put(s, new String(s));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜