优化Java字符串集合的内存使用情况 [英] Optimize memory usage of a collection of Strings in Java

查看:150
本文介绍了优化Java字符串集合的内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多名称-需要存储在某种缓存中的值对(大约100k)(例如哈希映射),其中值是一个平均大小约为30k字节的字符串. /p>

现在我知道一个事实,大量的值具有完全相同的字符串数据.为了避免多次分配相同的字符串数据,我想以某种方式重用以前分配的字符串,从而减少内存消耗.另外,这需要相当快.即不能一一扫描所有先前分配的值.

关于如何解决此问题的任何建议?

解决方案

不要不要使用String.intern(多年来存在与此相关的各种内存问题).而是创建您自己的缓存,类似于String.intern.基本上,您需要一个Map,每个键都映射到其自身.然后,在缓存任何字符串之前,先实习"它:

private Map<String,WeakReference<String>> myInternMap = new WeakHashMap<String,,WeakReference<String>>();
public String intern(String value) {
  synchronized(myInternMap) {
    WeakReference<String> curRef = myInternMap.get(value);
    String curValue = ((curRef != null) ? curRef.get() : null);
    if(curValue != null) {
      return curValue;
    }

    myInternMap.put(value, new WeakReference<String>(value));
    return value;
  }
}

请注意,对键和值使用弱引用,这样就不会保留不再使用的字符串的引用.

I have a large number of name - value pairs (approx 100k) that I need to store in some sort of cache (say a hash map) where the value is a string with an average of about 30k bytes in size.

Now I know for a fact that a large number of the values have exactly the same string data. In order to avoid having to allocate the identical string data several times, I would like to somehow reuse a previously allocated string thus consuming less memory. In addition this needs to be reasonably fast. i.e. scanning through all the previously allocated values one-by-one is not an option.

Any recommendations on how I could solve this problem?

解决方案

Do not use String.intern (there have been various memory issues related to this through the years). instead, create your own cache, similar to String.intern. basically, you want a Map, where each key maps to itself. then, before caching any string, you "intern" it:

private Map<String,WeakReference<String>> myInternMap = new WeakHashMap<String,,WeakReference<String>>();
public String intern(String value) {
  synchronized(myInternMap) {
    WeakReference<String> curRef = myInternMap.get(value);
    String curValue = ((curRef != null) ? curRef.get() : null);
    if(curValue != null) {
      return curValue;
    }

    myInternMap.put(value, new WeakReference<String>(value));
    return value;
  }
}

note, you use weakreferences for the keys and values so that you don't keep references for strings which you are no longer using.

这篇关于优化Java字符串集合的内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆