gpt4 book ai didi

api - 在 org.apache.catalina.core.JreMemoryLeakPreventionListener 中急切调用 URLConnection 的 setDefaultUseCaches(false) 的原因是什么

转载 作者:行者123 更新时间:2023-11-29 03:54:35 25 4
gpt4 key购买 nike

这个问题可能有点难找答案。这是一个系列中的问题 What is the reason that Policy.getPolicy() is considered as it will retain a static reference to the context and can cause memory leak .您可以阅读它,以便了解更多背景。

org.apache.cxf.common.logging.JDKBugHacks 中提取源代码还有来自 org.apache.catalina.core.JreMemoryLeakPreventionListener .

有一段代码。在这里。

URL url = new URL("jar:file://dummy.jar!/"); 
URLConnection uConn = new URLConnection(url) {

@Override
public void connect() throws IOException{
// NOOP
}

};
uConn.setDefaultUseCaches(false);

评论说

/*
* Several components end up opening JarURLConnections without
* first disabling caching. This effectively locks the file.
* Whilst more noticeable and harder to ignore on Windows, it
* affects all operating systems.
*
* Those libraries/components known to trigger this issue
* include:
* - log4j versions 1.2.15 and earlier
* - javax.xml.bind.JAXBContext.newInstance()
*/

但是我很难理解。为什么他们热切地调用 setDefaultUseCaches(false) 以及为什么在 Windows 上默认缓存为 true 是有害的?我在 java.net.JarURLConnection 中找不到任何线索.

最佳答案

我自己找到了答案。如果您认为我错了,任何人都可以纠正我。在 sun.net.www.protocol.jar.JarURLConnection .我假设这是 java.net.JarURLConnection 的默认实现。下面有一段代码。

如果缓存设置为真,那么它不会关闭 JarFile 的连接。这意味着它已锁定。

class JarURLInputStream extends java.io.FilterInputStream {
JarURLInputStream (InputStream src) {
super (src);
}
public void close () throws IOException {
try {
super.close();
} finally {
if (!getUseCaches()) {
jarFile.close(); //will not close
}
}
}
}



public void connect() throws IOException {
if (!connected) {
/* the factory call will do the security checks */
jarFile = factory.get(getJarFileURL(), getUseCaches());

/* we also ask the factory the permission that was required
* to get the jarFile, and set it as our permission.
*/
if (getUseCaches()) {
jarFileURLConnection = factory.getConnection(jarFile);
}

if ((entryName != null)) {
jarEntry = (JarEntry)jarFile.getEntry(entryName);
if (jarEntry == null) {
try {
if (!getUseCaches()) {
jarFile.close(); //will not close
}
} catch (Exception e) {
}
throw new FileNotFoundException("JAR entry " + entryName +
" not found in " +
jarFile.getName());
}
}
connected = true;
}
}

关于api - 在 org.apache.catalina.core.JreMemoryLeakPreventionListener 中急切调用 URLConnection 的 setDefaultUseCaches(false) 的原因是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7071761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com