软引用与弱引用 / 内存溢出错误

6
我在处理软引用和弱引用时遇到了问题。代码中有一个开关,可以在软引用和弱引用之间切换全部逻辑。而且尽管使用弱引用似乎一切正常,但是我不断地遇到OutOfMemoryError(内存溢出错误)的问题。这种情况在MacOSX上的JDK7和JDK6以及Debian上的IcedTea6中都会发生。然而,我发现使用带有G1收集器的JDK7与软引用一起使用的设置可以解决问题,其他所有尝试(串行/并行GC、-client/-server等)都会失败并抛出异常。

代码有点大,但我已经尽可能缩小范围来保留问题。我在顶部留下了大量注释,在其中更详细地描述了如何重现此问题。

/*
 *
 * Leakling.java
 *
 *
 * Issue:
 *
 *
 *   This code throws OutOfMemoryError when using soft references, whereas weak references
 *   work ok. Moreover, with JDK7 G1 garbage collector soft references work as well. Other
 *   collectors seem to fail. Code was tested with MacOSX 10.8.2 JDKs 1.7.0_10-b18 and
 *   1.6.0_37-b06-434-11M3909, with Debian 6.0 IcedTea6 1.8.13.
 *   Various command line options including -Xmx, -client/-server, -XX:+UseParallelOldGC,
 *   -XX:+UseSerialGC were tested.
 *
 *
 * Examples:
 *
 *
 * 1. Default options, weak references, this works, counters go up and down,
 *    but everything keeps going just as expected:
 *
 *   java -Xmx50m Leakling \
 *       --loop-delay=10 --min-chunk-size=1000 --max-chunk-size=100000 --use-soft-references=false
 *
 *
 * 2. Default options, soft references, this eventually throws the exception:
 *
 *   java -Xmx50m Leakling \
 *       --loop-delay=10 --min-chunk-size=1000 --max-chunk-size=100000 --use-soft-references=true
 *
 *
 * 3. G1 collector (IcedTea6 doesn't support it), weak references, this works, but it did anyway:
 *
 *   java -XX:+UseG1GC -Xmx50m Leakling \
 *       --loop-delay=10 --min-chunk-size=1000 --max-chunk-size=100000 --use-soft-references=false
 *
 *
 * 4. G1 collector, soft references, this works with JDK7.
 *    JDK6 seems to just stop after having hit memory limit (with no message whatsoever).
 *
 *   java -XX:+UseG1GC -Xmx50m Leakling \
 *       --loop-delay=10 --min-chunk-size=1000 --max-chunk-size=100000 --use-soft-references=true
 *
 *
 * jarek, 02.01.2013
 *
 *
 */

import java.lang.ref.*;
import java.util.*;

public class Leakling {
  private static final String TAG = "memory-chunk-";


  class Chunk {
    final String name;
    final int size;
    final private byte[] mem;

    Chunk(String name, int minSize, int maxSize, Random randomizer) {
      int currSize = minSize;
      if (maxSize > minSize) {
        currSize += randomizer.nextInt(maxSize - minSize + 1);
      }
      this.size = currSize;
      this.mem = new byte[currSize];
      this.name = name;
      log(this + " has been created (" + currSize + " bytes)");
    }

    @Override
    public void finalize() throws Throwable {
      log(this + " is finalizing");
    }

    @Override
    public String toString() {
      return name + " of " + getReadableMemorySize(size);
    }
  }


  class WeakChunk extends WeakReference<Chunk> {
    final String name;

    public WeakChunk(Chunk chunk, ReferenceQueue<Chunk> queue) {
      super(chunk, queue);
      this.name = chunk.name;
    }

    @Override
    public String toString() {
      return "weak reference of " + name + " is " + ((get() == null) ? "null" : "alive");
    }
  }


  class SoftChunk extends SoftReference<Chunk> {
    final String name;

    public SoftChunk(Chunk chunk, ReferenceQueue<Chunk> queue) {
      super(chunk, queue);
      this.name = chunk.name;
    }

    @Override
    public String toString() {
      return "soft reference of " + name + " is " + ((get() == null) ? "null" : "alive");
    }
  }

  // Logging as implemented here gives extra timing info (secs.milis starting from the initialization).
  private final long start = System.currentTimeMillis();
  private final Formatter formatter = new Formatter(System.err);
  private final String formatString = "%1$d.%2$03d %3$s\n";

  // I found this be better synchronized...
  synchronized void log(Object o) {
    long curr = System.currentTimeMillis();
    long diff = curr - start;
    formatter.format(formatString, (int) (diff / 1000), (int) (diff % 1000), o.toString());
  }

  private final ArrayList<Chunk> allChunks = new ArrayList<Chunk>();
  private final ReferenceQueue<Chunk> softReferences = new ReferenceQueue<Chunk>();
  private final ReferenceQueue<Chunk> weakReferences = new ReferenceQueue<Chunk>();
  private final HashSet<Reference<Chunk>> allReferences = new HashSet<Reference<Chunk>>();
  private final Random randomizer = new Random();

  private int loopDelay = 200;
  private int minChunkSize = 100;
  private int maxChunkSize = 1000;
  private int chunkCounter = 0;
  private boolean useSoftReferences = false;
  private long minMemory = 10 * 1024 * 1024;  // Default range is 10-30MB
  private long maxMemory = 3 * minMemory;
  private long usedMemory = 0;

  private String getReadableMemorySize(long size) {
    if (size >= 1024 * 1024) {
      return (float) (Math.round((((float) size) / 1024f / 1024f) * 10f)) / 10f + "MB";
    }
    if (size >= 1024) {
      return (float) (Math.round((((float) size) / 1024f) * 10f)) / 10f + "kB";
    } else if (size > 0) {
      return size + "B";
    } else {
      return "0";
    }
  }

  private void allocMem() {
    Chunk chunk = new Chunk(TAG + chunkCounter++, minChunkSize, maxChunkSize, randomizer);
    allChunks.add(chunk);
    Reference ref = useSoftReferences
            ? (new SoftChunk(chunk, softReferences)) : (new WeakChunk(chunk, weakReferences));
    allReferences.add(ref);
    log(ref);
    usedMemory += chunk.size;
  }

  private void freeMem() {
    if (allChunks.size() < 1) {
      return;
    }
    int i = randomizer.nextInt(allChunks.size());
    Chunk chunk = allChunks.get(i);
    log("freeing " + chunk);
    usedMemory -= chunk.size;
    allChunks.remove(i);
  }

  private int statMem() throws Exception {
    for (Reference ref; (ref = softReferences.poll()) != null;) {
      log(ref);
      allReferences.remove(ref);
    }
    for (Reference ref; (ref = weakReferences.poll()) != null;) {
      log(ref);
      allReferences.remove(ref);
    }
    int weakRefs = 0;
    int softRefs = 0;
    for (Iterator<Reference<Chunk>> i = allReferences.iterator(); i.hasNext();) {
      Reference<Chunk> ref = i.next();
      if (ref.get() == null) {
        continue;
      }
      if (ref instanceof WeakChunk) {
        weakRefs++;
      }
      if (ref instanceof SoftChunk) {
        softRefs++;
      }
    }
    log(allChunks.size() + " chunks, "
            + softRefs + " soft refs, "
            + weakRefs + " weak refs, "
            + getReadableMemorySize(usedMemory) + " used, "
            + getReadableMemorySize(Runtime.getRuntime().freeMemory()) + " free, "
            + getReadableMemorySize(Runtime.getRuntime().totalMemory()) + " total, "
            + getReadableMemorySize(Runtime.getRuntime().maxMemory()) + " max");
    if (loopDelay > 1) {
      Thread.sleep(loopDelay);
    }
    return (int)((100 * usedMemory) / maxMemory); // Return % of maxMemory being used.
  }

  public Leakling(String[] args) throws Exception {
    for (String arg : args) {
      if (arg.startsWith("--min-memory=")) {
        minMemory = Long.parseLong(arg.substring("--min-memory=".length()));
      } else if (arg.startsWith("--max-memory=")) {
        maxMemory = Long.parseLong(arg.substring("--max-memory=".length()));
      } else if (arg.startsWith("--min-chunk-size=")) {
        minChunkSize = Integer.parseInt(arg.substring("--min-chunk-size=".length()));
      } else if (arg.startsWith("--max-chunk-size=")) {
        maxChunkSize = Integer.parseInt(arg.substring("--max-chunk-size=".length()));
      } else if (arg.startsWith("--loop-delay=")) {
        loopDelay = Integer.parseInt(arg.substring("--loop-delay=".length()));
      } else if (arg.startsWith("--use-soft-references=")) {
        useSoftReferences = Boolean.parseBoolean(arg.substring("--use-soft-references=".length()));
      } else {
        throw new Exception("Unknown command line option...");
      }
    }
  }

  public void run() throws Exception {
    log("Mem test started...");

    while(true) {
      log("going up...");
      do {// First loop allocates memory up to the given limit in a pseudo-random fashion.
          // Randomized rate of allocations/frees is about 4:1 as per the 10>=8 condition.
        if (randomizer.nextInt(10) >= 8) {
          freeMem();
        } else {
          allocMem();
        }
      } while (statMem() < 90); // Repeat until 90% of the given mem limit is hit...

      log("going down...");
      do {// Now do the reverse. Frees are four times more likely than allocations are.
        if (randomizer.nextInt(10) < 8) {
          freeMem();
        } else {
          allocMem();
        }
      } while (usedMemory > minMemory);
    }
  }

  public static void main(String[] args) throws Exception {
    (new Leakling(args)).run();
  }
}

  1. OOME 附带的消息是什么?
  2. 你不能指望别人查看整个代码。你应该将其编辑为精华并在此处发布,而不是使用 pastebin。
- Enno Shioji
你的 OOME 处理速度有多快? - jtahlborn
我已经缩小了代码范围,但它仍然很大。异常是java.lang.OutOfMemoryError: Java heap space 在Leakling$Chunk.<init>(Leakling.java:68) 在Leakling.allocMem(Leakling.java:156) 在Leakling.run(Leakling.java:242) 在Leakling.main(Leakling.java:258)而且复现很简单,只需编译,从代码注释中复制并粘贴命令即可。不需要进行任何分析。 - jarek
整个 // Now do the reverse. Frees are four times more likely than allocations are. 循环不是有问题吗?如果你一直得到 8、9、10,它不会一直分配内存吗? - jn1kk
1
在我的例子中,我设置了-Xmx50m,并且代码内部限制设置为30MB,输出的最后一行看起来差不多像“12.384 281块,953个软引用,0个弱引用,已使用12.4MB,剩余2.1MB,总共47.9MB,最大47.9MB” - 软引用似乎没有被释放。此外,使用相同条件使用弱引用从未出现过问题。 - jarek
2个回答

6
首先,不要混淆终结器和引用。两者都会影响对象从内存中移除的速度,并且您可以使用适当的引用类型更好地完成终结器可以做的所有事情。
其次,正如我所提到的,使用引用可能会导致垃圾回收延迟。至少对于“常见”的垃圾回收算法,弱引用/软引用对象可能需要额外的垃圾回收运行,然后才能完全回收。弱引用和软引用之间的基本区别在于,弱引用会被积极地进行垃圾回收,而软引用通常会尽可能长时间地保持。这很可能是让您感到困惑的原因。
当您使用弱引用对象运行时,随着操作的进行,会清理掉一些东西,从而使您避免 OOME。
当您使用软引用对象运行时,所有软引用对象都将被保留,直到接近限制。然后,当内存变得紧张时,垃圾回收器会尝试开始释放软引用对象,但这需要太长时间(因为可能需要多次垃圾回收才能完全回收内存),结果您最终会遇到 OOME。
我只有对 G1 垃圾回收器的表面了解,所以我不知道它为什么在这种情况下“有效”。
总之,软引用是一种不错的东西,但由于回收延迟,它们并不总是像您希望的那样有效。此外,这是一篇 很棒的文章,其中包含一些额外有用的细节。

G1只能在JDK7上运行(可能会触发清除软引用的时间早于出现问题)。在启用G1的情况下,JDK6甚至不会显示任何消息,只是以退出代码== 1退出。 - jarek

5

摆脱终结器。

对象的终结发生在单独的线程中,并且内存只有在终结完成后才能真正被回收。在您的终结器中,您正在执行一个系统调用(输出),这将在该线程中引入等待。当您处于内存极限时,任何终结器等待都很容易导致OOM。

至于软参考和弱参考的区别:我们的参考将在小收集期间被回收,而软参考不会被回收(我还没有检查过;可能是控制软参考寿命的标志将允许其在多个小收集期间保持活动状态)。很可能您的终结器线程可以跟上已丢弃的弱引用对象。


去除终结器可以解决问题。空终结器也是有效的,因此看起来是I/O定时问题。我现在会重新考虑逻辑以保留日志记录。谢谢。 - jarek
如果您查看@jtahlborn链接的文章,您会发现一个程序在没有任何程序级引用的情况下耗尽内存。这全是因为一个缓慢的终结器。 - parsifal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接