java.util.jar.JarFile cause native heap memory leak

本文介绍了一种在使用Java的JarFile类加载配置文件时出现的内存泄漏问题,并提供了一个替代方案使用JarInputStream来避免这一问题,特别是在多线程环境下。

最近项目中使用了JarFile 这个类 来load jar包中的 configuration,大致的情况如下

public void processJarEntries(JarFile paramJarFile, ZipEntryFilter paramZipEntryFilter, ZipEntryProcessor paramZipEntryProcessor)
  {
    for (Enumeration localEnumeration = paramJarFile.entries(); localEnumeration.hasMoreElements(); ) {
      ZipEntry localZipEntry = (ZipEntry)localEnumeration.nextElement();
      if (paramZipEntryFilter.accept(localZipEntry))
        paramZipEntryProcessor.process(paramJarFile, localZipEntry);
    }
  }


ZipEntryFilter 是过滤jar文件中不相关的文件 ZipEntryProcessor 用来做具体对找到的文件操作


问题引入: 此时会通过JarFile中的 两个method 来 遍历jar包中的文件 hasMoreElements & nextElment

在多线程处理的情况下引起 native heap memory leak 可以 通过  pmap -ax pid|grep heap 来查看


解决:

JarInputStream localJarInputStream = null;

localJarInputStream = new JarInputStream(getBufferedInputStream(paramFile));

public static InputStream getBufferedInputStream(File paramFile) throws IOException {
    FileInputStream localFileInputStream = new FileInputStream(paramFile);
    return new BufferedInputStream(localFileInputStream);
  }

public void processJarEntries(JarInputStream paramJarInputStream, JarEntryProcessor paramJarEntryProcessor) {
    ZipEntry localZipEntry = paramJarInputStream.getNextEntry();
    while (localZipEntry != null) {
      paramJarEntryProcessor.process(localZipEntry);
      localZipEntry = paramJarInputStream.getNextEntry();
    }
    paramJarEntryProcessor.completeProcess();
  }

 

此bug可以在oracle官网中查到




 

08-Sep-2025 13:35:44.453 SEVERE [http-nio-8080-exec-15] org.apache.catalina.core.StandardContext.startInternal Context [/mesFERROTEC] startup failed due to previous errors 08-Sep-2025 13:35:44.976 WARNING [http-nio-8080-exec-15] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc The web application [mesFERROTEC] registered the JDBC driver [com.mysql.cj.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered. 08-Sep-2025 13:35:44.978 WARNING [http-nio-8080-exec-15] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [mesFERROTEC] appears to have started a thread named [mysql-cj-abandoned-connection-cleanup] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread: java.lang.Object.wait(Native Method) java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144) com.mysql.cj.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:85) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) java.lang.Thread.run(Thread.java:748) 08-Sep-2025 13:35:44.979 WARNING [http-nio-8080-exec-15] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [mesFERROTEC] appears to have started a thread named [pool-21-thread-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread: sun.misc.Unsafe.park(Native Method) java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215) java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078) java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093) java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809) java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) java.lang.Thread.run(Thread.java:748) 08-Sep-2025 13:35:44.986 INFO [http-nio-8080-exec-15] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [/usr/local/tomcat/webapps/mesFERROTEC.war] has finished in [109,199] ms 08-Sep-2025 13:35:47.008 INFO [mysql-cj-abandoned-connection-cleanup] org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading Illegal access: this web application instance has been stopped already. Could not load []. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access. java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load []. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access. at org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading(WebappClassLoaderBase.java:1378) at org.apache.catalina.loader.WebappClassLoaderBase.getResource(WebappClassLoaderBase.java:1030) at com.mysql.cj.jdbc.AbandonedConnectionCleanupThread.checkThreadContextClassLoader(AbandonedConnectionCleanupThread.java:117) at com.mysql.cj.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:84) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)
最新发布
09-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值