这个问题可能有点难以找到答案。这是一个系列中的问题考虑使用 Policy.getPolicy() 的原因是什么,因为它将保留对上下文的静态引用并可能导致内存泄漏 https://stackoverflow.com/questions/7057421/why-policy-getpolicy-is-considered-as-it-will-retain-a-static-reference-to-the。您可以阅读它,以便您了解更多背景。
源代码来自org.apache.cxf.common.logging.JDKBugHacks http://grepcode.com/file/repo1.maven.org/maven2/org.apache.cxf/cxf-common-utilities/2.3.1/org/apache/cxf/common/logging/JDKBugHacks.java也来自org.apache.catalina.core.JreMemoryLeakPreventionListener http://grepcode.com/file/repo1.maven.org/maven2/org.apache.tomcat/tomcat-catalina/7.0.0/org/apache/catalina/core/JreMemoryLeakPreventionListener.java.
有一段代码。这里是。
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 http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/net/JarURLConnection.java.
我自己找到了答案。如果您认为我错了,任何人都可以纠正我。
在sun.net.www.protocol.jar.JarURLConnection http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/sun/net/www/protocol/jar/JarURLConnection.java?av=h#JarURLConnection。我认为这是默认实现java.net.JarURLConnection
。下面有一段代码。
如果cache设置为true,那么它不会关闭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;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)