INT-3915: Possible Memory Leak in FileChannelCache
JIRA: https://jira.spring.io/browse/INT-3915 Close the redundant `FileChannel` when Map collision occurs.
This commit is contained in:
committed by
Artem Bilan
parent
aca5646181
commit
b5ec73b638
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2013 the original author or authors.
|
* Copyright 2002-2015 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -19,9 +19,9 @@ package org.springframework.integration.file.locking;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.RandomAccessFile;
|
import java.io.RandomAccessFile;
|
||||||
|
import java.nio.channels.FileChannel;
|
||||||
import java.nio.channels.FileLock;
|
import java.nio.channels.FileLock;
|
||||||
import java.nio.channels.OverlappingFileLockException;
|
import java.nio.channels.OverlappingFileLockException;
|
||||||
import java.nio.channels.FileChannel;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ConcurrentMap;
|
import java.util.concurrent.ConcurrentMap;
|
||||||
|
|
||||||
@@ -29,6 +29,7 @@ import java.util.concurrent.ConcurrentMap;
|
|||||||
* Static cache of FileLocks that can be used to ensure that only a single lock is used inside this ClassLoader.
|
* Static cache of FileLocks that can be used to ensure that only a single lock is used inside this ClassLoader.
|
||||||
*
|
*
|
||||||
* @author Iwein Fuld
|
* @author Iwein Fuld
|
||||||
|
* @author Gary Russell
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
*/
|
*/
|
||||||
final class FileChannelCache {
|
final class FileChannelCache {
|
||||||
@@ -47,9 +48,21 @@ final class FileChannelCache {
|
|||||||
public static FileLock tryLockFor(File fileToLock) throws IOException {
|
public static FileLock tryLockFor(File fileToLock) throws IOException {
|
||||||
FileChannel channel = channelCache.get(fileToLock);
|
FileChannel channel = channelCache.get(fileToLock);
|
||||||
if (channel == null) {
|
if (channel == null) {
|
||||||
|
@SuppressWarnings("resource")
|
||||||
FileChannel newChannel = new RandomAccessFile(fileToLock, "rw").getChannel();
|
FileChannel newChannel = new RandomAccessFile(fileToLock, "rw").getChannel();
|
||||||
FileChannel original = channelCache.putIfAbsent(fileToLock, newChannel);
|
FileChannel original = channelCache.putIfAbsent(fileToLock, newChannel);
|
||||||
channel = (original != null) ? original : newChannel;
|
if (original != null) {
|
||||||
|
channel = original;
|
||||||
|
try {
|
||||||
|
newChannel.close();
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
channel = newChannel;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
FileLock lock = null;
|
FileLock lock = null;
|
||||||
if (channel != null) {
|
if (channel != null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user