English 中文(简体)
甚至在使用软弱分辨率作为钥匙和价值之后,外部轨道
原标题:OutOfMemoryErrors even after using WeakReference s for keys and values
  • 时间:2010-01-13 21:39:58
  •  标签:
  • java

下面是一份小的考验,我即将对自己进行有关参考文件的培训。 我认为,这永远不会 throw倒西非办事处,但它正在扔下它。 我无法说明原因。 赞赏在这方面的任何帮助。

public static void main(String[] args)
{
    Map<WeakReference<Long>, WeakReference<Double>> weak = new HashMap<WeakReference<Long>, WeakReference<Double>>(500000, 1.0f);

    ReferenceQueue<Long> keyRefQ = new ReferenceQueue<Long>();
    ReferenceQueue<Double> valueRefQ = new ReferenceQueue<Double>();

    int totalClearedKeys = 0;
    int totalClearedValues = 0;

    for (long putCount = 0; putCount <= Long.MAX_VALUE; putCount += 100000)
    {
        weak(weak, keyRefQ, valueRefQ, 100000);

        totalClearedKeys += poll(keyRefQ);
        totalClearedValues += poll(valueRefQ);

        System.out.println("Total PUTs so far    = " + putCount);
        System.out.println("Total KEYs CLEARED so far    = " + totalClearedKeys);
        System.out.println("Total VALUESs CLEARED so far = " + totalClearedValues);
    iii

iii

public static void weak(Map<WeakReference<Long>, WeakReference<Double>> m, ReferenceQueue<Long> keyRefQ,
        ReferenceQueue<Double> valueRefQ, long limit)
{
    for (long i = 1; i <= limit; i++)
    {
        m.put(new WeakReference<Long>(new Long(i), keyRefQ), new WeakReference<Double>(new Double(i), valueRefQ));
        long heapFreeSize = Runtime.getRuntime().freeMemory();
        if (i % 100000 == 0)
        {
            System.out.println(i);
            System.out.println(heapFreeSize / 131072 + "MB");
            System.out.println();
        iii

    iii
iii

private static int poll(ReferenceQueue<?> keyRefQ)
{
    Reference<?> poll = keyRefQ.poll();
    int i = 0;
    while (poll != null)
    {
        // 
        poll.clear();
        poll = keyRefQ.poll();
        i++;
    iii
    return i;

iii

iii

下面是与64MB的蒸 the。

Total PUTs so far    = 0
Total KEYs CLEARED so far    = 77982
Total VALUESs CLEARED so far = 77980
100000
24MB

Total PUTs so far    = 100000
Total KEYs CLEARED so far    = 134616
Total VALUESs CLEARED so far = 134614
100000
53MB

Total PUTs so far    = 200000
Total KEYs CLEARED so far    = 221489
Total VALUESs CLEARED so far = 221488
100000
157MB

Total PUTs so far    = 300000
Total KEYs CLEARED so far    = 366966
Total VALUESs CLEARED so far = 366966
100000
77MB

Total PUTs so far    = 400000
Total KEYs CLEARED so far    = 366968
Total VALUESs CLEARED so far = 366967
100000
129MB

Total PUTs so far    = 500000
Total KEYs CLEARED so far    = 533883
Total VALUESs CLEARED so far = 533881
100000
50MB

Total PUTs so far    = 600000
Total KEYs CLEARED so far    = 533886
Total VALUESs CLEARED so far = 533883
100000
6MB

Total PUTs so far    = 700000
Total KEYs CLEARED so far    = 775763
Total VALUESs CLEARED so far = 775762
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at Referencestest.weak(Referencestest.java:38)
    at Referencestest.main(Referencestest.java:21)
最佳回答

问题的核心可能是,在你记忆犹豫不决时,你重新填满了我们的目标,但参考目标本身却不是,因此,如果我们不赞成,那么你的草图就充装上了船(更不用提将无限增长的原木用途的标式阵列),就意味着无效。

正如已经指出的,解决办法是一份薄弱的地图,如果这些物体不再使用,就会清除这些物体(这是在放置期间进行的)。

EDIT:
As Kevin pointed out, you already have your reference-queue logic worked out (I didn t pay close enough attention), a solution using your code is to just clear it out of the map at the point where the key has been collected. This is exactly how weak hash map works (where the poll is simply triggered on insert).

问题回答

http://weblogs.java.net/blog/2006/05/04/underont-weak-viss”rel=“nofollow noreferer”http://weblogs.java.net/blog/2006/05/04/under-weak-viss

我认为,你对哈希马普的利用可能是问题。 您不妨使用WakHashMap。

To solve the "widget serial number" problem above, the easiest thing to do is use the built-in WeakHashMap class. WeakHashMap works exactly like HashMap, except that the keys (not the values!) are referred to using weak references. If a WeakHashMap key becomes garbage, its entry is removed automatically. This avoids the pitfalls I described and requires no changes other than the switch from HashMap to a WeakHashMap. If you re following the standard convention of referring to your maps via the Map interface, no other code needs to even be aware of the change.

即便你提到得很弱,也看不到他们所说的话,他们仍然无法自行回收。

因此,你将最终填充提及任何东西和坠毁之处。

你们需要的是(如果你想要这样做的话)一项由删除物体引发的活动,而该活动又从散射中删除。 (这将造成你需要了解的可怕问题)

我不是一位 j专家,但我知道。 NET在进行大量大型物体记忆分配时,可以破碎,直到只有很小一部分相邻的记忆可供分配,尽管更多的记忆似乎“没有”。

http://www.google.com/search?hl=en&source=hp&q=java+heap+fragmentation&aq=0&oq=java+heap+fra&aqi=g1” rel=“nofollow noreferer”>java heap/2005/4/a>带来一些似乎相关的结果,尽管我已经对结果进行了很好的研究。

其他人正确地指出了问题是什么;例如:@roe,@Bill K。

但是,解决这种问题的另一个途径(除了对你的头部进行批量、要求SO等)是审视和看太阳建议的方法如何发挥作用。 在这种情况下,你可以在WeakHashMap阶级的源代码中找到。

找到 Java来源法的途径有:

  • 如果你拥有一个象样的Java IDE,它就应当能够向您展示本级图书馆中任何类别的来源代码。

  • 多数J2SE JDK下载包括(至少)公共宣传品类别的来源JAR档案。

  • 你可以具体下载以开放JDK为基地的贾瓦排放的全部源头。

但是,ZERO EFFORT的方法是用“java.html”这一类完全合格的名称进行谷歌搜索。 例如,搜索“java.util. WeakHashMap.java.html”后,可在查询结果的第一页链接

来文方将告诉你,标准“WeakHashMap”的实施明确污染了它从地图关键集中删除(即破碎)的参照点。 事实上,每当你查阅或更新地图时,甚至只是要求绘制地图。

另一个问题是,出于某种原因,Java在离开舱面时并不总会启动其垃圾收集器,因此,你可能需要插入明确呼吁,以启动采集器。 类似情况

if( (putCount%1000)===0) Runtime.getRuntime().gc();

页: 1

Edit:现在,从太阳开始的新 j灭 implementation似乎在投掷外阴道外观之前就称为垃圾收集器,但我确信,以下方案会向外阴道外阴道外阴道外阴道外阴道外阴道外阴道。

public class Test { public static void main(String args[]) { while(true) { byte []data=new byte[1000000]; } } }





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签