我正在创建一个同时执行方法的接口,同时抽象出同步细节(在需要时交换分布式实现).我已经创建了一个单独的jvm实现,它允许将字符串作为互斥体存储在映射中,以确保使用一个引用,即使传入不同引用的字符串.并发似乎工作正常,但我是惊讶地看到测试表明参考计数从未减少.我假设使用WeakValues()就足以防止内存泄漏,但似乎并非如此.任何人都可以指出可能导致这种泄漏的原因吗?
public class SynchronousMethodExecutorSynchronizedImpl implements ISynchronousMethodExecutor {
// mutex map to provide string references
final Map<String, String> mutexMap = new MapMaker()
.weakValues()
.makeComputingMap(
new Function<String, String>() {
@Override
public String apply(String id) {
return id;
}
});
@Override
public Object doSynchronousMethod(String domain, String id, ISynchronousMethod synchronousMethod) {
synchronized(mutexMap.get(domain + "." + id))
{
return synchronousMethod.execute();
}
}
}
这是在最后一个断言失败的测试:
public class SynchronousMethodExecutorSynchronizedImplTest extends TestCase {
int counter;
SynchronousMethodExecutorSynchronizedImpl methodExecutor;
@Override
public void before() throws Exception {
super.before();
methodExecutor = new SynchronousMethodExecutorSynchronizedImpl();
}
@Test
public void concurrentExecute() throws InterruptedException {
assertEquals(0, counter);
for(int i=0; i<1000; i++)
getConcurrentExecutorThread().start();
// wait for threads to complete
Thread.sleep(1000);
assertEquals(1, methodExecutor.mutexMap.size());
try
{
final List<long[]> infiniteList = new LinkedList<long[]>();
for(long i = Long.MIN_VALUE; i < Long.MAX_VALUE; i++)
infiniteList.add(new long[102400]);
fail("An OutOfMemoryError should be thrown");
}
catch(OutOfMemoryError e)
{
}
assertEquals(2000, counter);
assertEquals(0, methodExecutor.mutexMap.size());
}
// synchronous method
private ISynchronousMethod method = new ISynchronousMethod() {
@Override
public Object execute() {
counter++;
return null;
}
};
/**
* Executes a line of code.
*
* @return Thread
*/
private Thread getConcurrentExecutorThread() {
return new Thread() {
@Override
public void run() {
methodExecutor.doSynchronousMethod("TEST", "1", method);
try
{
Thread.sleep(500);
}
catch (InterruptedException e)
{
}
methodExecutor.doSynchronousMethod("TEST", new String("1"), method);
}
};
}
}
最后一个断言是打破测试的原因:
assertEquals(0,methodExecutor.mutexMap.size());
解决方法:
您将存储与键和值完全相同的String对象.关键是对对象的强烈引用,只要存在对它的强引用,对它的弱引用就没有意义了.弱可达(here)的定义指出:
An object is weakly reachable if it is neither strongly nor softly reachable but can be reached by traversing a weak reference.
顺便说一句,即使有了这个纠正,我也不认为你可以假设地图最后总是空的.它可能会接近空洞,但我认为可以说是关于它的所有内容.