본문 바로가기
Apache Tomcat

Tomcat 8.5에서 JreMemoryLeakPreventionListener의 urlCacheProtection 옵션

강철지그·2017년 12월 5일·조회 3,891

1. 개요

  • Java 9 이상에서 실행하는 경우
  • JreMemoryLeakPreventionListenerurlCacheProtection 옵션을 사용하는 경우
  • JAR URL connection의 캐싱을 비활성화하려면 Java 9에서 추가된 API를 사용한다.

JreMemoryLeakPreventionListenerurlCacheProtection은 JAR 파일을 읽는 과정에서 JarURLConnection 캐시가 남아 JAR 파일이 잠기거나, 애플리케이션 재배포 시 리소스가 해제되지 않는 문제를 줄이기 위한 옵션이다. 특히 Windows에서는 열린 JAR 파일이 잠겨 교체나 삭제가 실패하는 형태로 더 눈에 띄게 나타날 수 있다.

Java 9부터는 JAR URL connection에 대한 캐싱만 비활성화하는 API를 사용할 수 있다. 반면 Java 8 이하에서는 같은 수준으로 제한하기 어려워 모든 URLConnection의 기본 캐싱 정책을 비활성화하는 방식이 사용된다. 따라서 Java 버전에 따라 적용 범위가 달라진다는 점을 확인해야 한다.


2. 관련 코드

    /**
     * Protect against resources being read for JAR files and, as a side-effect,
     * the JAR file becoming locked. Note this disables caching for all
     * {@link URLConnection}s, regardless of type. Defaults to
     * <code>true</code>.
     */
    private boolean urlCacheProtection = true;
    public boolean isUrlCacheProtection() { return urlCacheProtection; }
    public void setUrlCacheProtection(boolean urlCacheProtection) {
        this.urlCacheProtection = urlCacheProtection;
    }
                /*
                 * 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()
                 *
                 * https://bugs.openjdk.java.net/browse/JDK-8163449
                 *
                 * Java 9 onwards disables caching for JAR URLConnections
                 * Java 8 and earlier disables caching for all URLConnections
                 */

                // Set the default URL caching policy to not to cache
                if (urlCacheProtection) {
                    try {
                        JreCompat.getInstance().disableCachingForJarUrlConnections();
                    } catch (IOException e) {
                        log.error(sm.getString("jreLeakListener.jarUrlConnCacheFail"), e);
                    }
                }

3. 확인 포인트

  • urlCacheProtectiontrue로 설정되어 있는지 확인한다. 코드 주석 기준 기본값은 true다.
  • Java 9 이상에서는 JAR URL connection 캐싱만 비활성화되는지 확인한다.
  • Java 8 이하에서는 모든 URLConnection의 캐싱 정책에 영향을 줄 수 있으므로, 애플리케이션에서 URL 캐싱에 의존하는 부분이 있는지 점검한다.
  • 재배포 또는 종료 후 JAR 파일이 잠겨 삭제·교체되지 않는 문제가 재현되는 환경이라면, 옵션 적용 전후의 파일 잠금 상태를 비교해 본다.

관련 글

댓글 0

로그인 후 댓글을 남길 수 있습니다.

아직 댓글이 없습니다.