如何使软引用在Java中被清除? [英] How to cause soft references to be cleared in Java?

查看:189
本文介绍了如何使软引用在Java中被清除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个缓存,它具有对缓存对象的软引用。我正在尝试为缓存对象清空后发生的情况使用缓存的类的行为编写功能测试。



问题是:我无法似乎可靠地让软参考被清除。简单地使用一堆内存并不能解决问题:在任何软引用被清除之前,我都会得到一个OutOfMemory。



有什么方法可以让Java更热切地获得清除软引用?






找到这里


尽管所有的
SoftReference在
OutOfMemoryError抛出之前都会被清除,所以它们
理论上不会导致OOME。

那么这是否意味着上面的场景必须意味着我有一个内存泄漏的地方,某些类持有一个硬引用我的缓存对象?

解决方案


问题是:我似乎无法
可靠地获得软引用
清除。

这不是SoftReferences所特有的。由于Java中垃圾收集的性质,并不能保证任何垃圾收集的内容都将在任何时间点被实际收集。即使只有一小段代码:

  Object temp = new Object(); 
temp = null;
System.gc();

不保证在第一行实例化的对象是垃圾回收在此,或者实际上任何一点。这只不过是你用内存管理语言必须忍受的事情之一,你放弃了对这些事情的陈述权。是的,这可能使得有时难以确定地测试内存泄漏。




也就是说,根据Javadocs你引用了SoftReferences,在抛出OutOfMemoryError之前一定要清零(实际上,这是它们的全部点,也是它们与默认对象引用不同的唯一方式)。因此,听起来好像存在某种内存泄漏,因为您坚持引用更难的对象。



如果使用 -XX:+ HeapDumpOnOutOfMemoryError 选项添加到JVM,然后将堆转储加载到像 jhat ,你应该能够看到你的对象的所有引用,从而看看除了你的对象之外是否有任何引用。或者,您可以在测试运行时使用探查器实现同样的功能。


I have a cache which has soft references to the cached objects. I am trying to write a functional test for behavior of classes which use the cache specifically for what happens when the cached objects are cleared.

The problem is: I can't seem to reliably get the soft references to be cleared. Simply using up a bunch of memory doesn't do the trick: I get an OutOfMemory before any soft references are cleared.

Is there any way to get Java to more eagerly clear up the soft references?


Found here:

"It is guaranteed though that all SoftReferences will get cleared before OutOfMemoryError is thrown, so they theoretically can't cause an OOME."

So does this mean that the above scenario MUST mean I have a memory leak somewhere with some class holding a hard reference on my cached object?

解决方案

The problem is: I can't seem to reliably get the soft references to be cleared.

This is not unique to SoftReferences. Due to the nature of garbage collection in Java, there is no guarantee that anything that is garbage-collectable will actually be collected at any point in time. Even with a simple bit of code:

Object temp = new Object();
temp = null;
System.gc();

there is no guarantee that the Object instantiated in the first line is garbage collected at this, or in fact any point. It's simply one of the things you have to live with in a memory-managed language, you're giving up declarative power over these things. And yes, that can make it hard to definitively test for memory leaks at times.


That said, as per the Javadocs you quoted, SoftReferences should definitely be cleared before an OutOfMemoryError is thrown (in fact, that's the entire point of them and the only way they differ from the default object references). It would thus sound like there is some sort of memory leak in that you're holding onto harder references to the objects in question.

If you use the -XX:+HeapDumpOnOutOfMemoryError option to the JVM, and then load the heap dump into something like jhat, you should be able to see all the references to your objects and thus see if there are any references beside your soft ones. Alternatively you can achieve the same thing with a profiler while the test is running.

这篇关于如何使软引用在Java中被清除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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