From ebe1e1430e1898ceb4cc0b8a62bba5ae92f79351 Mon Sep 17 00:00:00 2001 From: j2blake Date: Sun, 17 Nov 2013 13:34:56 -0500 Subject: [PATCH 1/2] =?UTF-8?q?VIVO-552=20Reduce=20=E2=80=9Cmemory=20leak?= =?UTF-8?q?=E2=80=9D=20messages=20at=20shutdown.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Tomcat also complains if the VALUE of a ThreadLocal is an instance of a class that is defined in the Webapp. So even though the key type was HttpServleetRequest, some of the values were VitroRequest objects. Refactor so the value is the hash code of the request. --- .../config/FreemarkerConfigurationImpl.java | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/freemarker/config/FreemarkerConfigurationImpl.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/freemarker/config/FreemarkerConfigurationImpl.java index 74ddff2f3..30a40bc85 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/freemarker/config/FreemarkerConfigurationImpl.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/freemarker/config/FreemarkerConfigurationImpl.java @@ -10,7 +10,6 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; -import java.util.WeakHashMap; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; @@ -50,17 +49,17 @@ public class FreemarkerConfigurationImpl extends Configuration { private static final Log log = LogFactory .getLog(FreemarkerConfigurationImpl.class); - private final ThreadLocal currentRequest = new ThreadLocal<>(); - private final Map rbiMap = Collections - .synchronizedMap(new WeakHashMap()); + private final ThreadLocal currentRequestHash = new ThreadLocal<>(); + private final Map rbiMap = Collections + .synchronizedMap(new HashMap()); protected void setRequestInfo(HttpServletRequest req) { - currentRequest.set(req); - rbiMap.put(req, new RequestBasedInformation(req, this)); + currentRequestHash.set(req.hashCode()); + rbiMap.put(req.hashCode(), new RequestBasedInformation(req, this)); } private RequestBasedInformation getRequestInfo() { - return rbiMap.get(currentRequest.get()); + return rbiMap.get(currentRequestHash.get()); } @Override @@ -103,7 +102,7 @@ public class FreemarkerConfigurationImpl extends Configuration { @Override public Locale getLocale() { - return currentRequest.get().getLocale(); + return getRequestInfo().getReq().getLocale(); } private String[] joinNames(Set nameSet, String[] nameArray) { @@ -235,16 +234,23 @@ public class FreemarkerConfigurationImpl extends Configuration { * custom attribute, and the locale. In the future, it could be more. */ private static class RequestBasedInformation { + private final HttpServletRequest req; private final Configuration c; private final Map customAttributes = new HashMap<>(); private final Map sharedVariables = new HashMap<>(); public RequestBasedInformation(HttpServletRequest req, Configuration c) { + this.req = req; this.c = c; + setSharedVariables(req); setCustomAttributes(req); } + public HttpServletRequest getReq() { + return req; + } + public Map getCustomAttributes() { return customAttributes; } From d0458047cbbaabbd4514a7a17a75aa1c84fb95fa Mon Sep 17 00:00:00 2001 From: j2blake Date: Sun, 17 Nov 2013 15:27:24 -0500 Subject: [PATCH 2/2] =?UTF-8?q?VIVO-552=20Reduce=20=E2=80=9Cmemory=20leak?= =?UTF-8?q?=E2=80=9D=20messages=20at=20shutdown.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Back out the changes - locks up on the develop branch. --- .../config/FreemarkerConfigurationImpl.java | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/freemarker/config/FreemarkerConfigurationImpl.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/freemarker/config/FreemarkerConfigurationImpl.java index 30a40bc85..617a09f6c 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/freemarker/config/FreemarkerConfigurationImpl.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/freemarker/config/FreemarkerConfigurationImpl.java @@ -3,7 +3,6 @@ package edu.cornell.mannlib.vitro.webapp.freemarker.config; import java.io.IOException; -import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -49,22 +48,15 @@ public class FreemarkerConfigurationImpl extends Configuration { private static final Log log = LogFactory .getLog(FreemarkerConfigurationImpl.class); - private final ThreadLocal currentRequestHash = new ThreadLocal<>(); - private final Map rbiMap = Collections - .synchronizedMap(new HashMap()); + private final ThreadLocal rbiRef = new ThreadLocal<>(); - protected void setRequestInfo(HttpServletRequest req) { - currentRequestHash.set(req.hashCode()); - rbiMap.put(req.hashCode(), new RequestBasedInformation(req, this)); - } - - private RequestBasedInformation getRequestInfo() { - return rbiMap.get(currentRequestHash.get()); + void setRequestInfo(HttpServletRequest req) { + rbiRef.set(new RequestBasedInformation(req, this)); } @Override public Object getCustomAttribute(String name) { - Map attribs = getRequestInfo().getCustomAttributes(); + Map attribs = rbiRef.get().getCustomAttributes(); if (attribs.containsKey(name)) { return attribs.get(name); } else { @@ -74,13 +66,13 @@ public class FreemarkerConfigurationImpl extends Configuration { @Override public String[] getCustomAttributeNames() { - Set rbiNames = getRequestInfo().getCustomAttributes().keySet(); + Set rbiNames = rbiRef.get().getCustomAttributes().keySet(); return joinNames(rbiNames, super.getCustomAttributeNames()); } @Override public TemplateModel getSharedVariable(String name) { - Map vars = getRequestInfo().getSharedVariables(); + Map vars = rbiRef.get().getSharedVariables(); if (vars.containsKey(name)) { return vars.get(name); } else { @@ -90,7 +82,7 @@ public class FreemarkerConfigurationImpl extends Configuration { @Override public Set getSharedVariableNames() { - Set rbiNames = getRequestInfo().getSharedVariables().keySet(); + Set rbiNames = rbiRef.get().getSharedVariables().keySet(); @SuppressWarnings("unchecked") Set superNames = super.getSharedVariableNames(); @@ -102,7 +94,7 @@ public class FreemarkerConfigurationImpl extends Configuration { @Override public Locale getLocale() { - return getRequestInfo().getReq().getLocale(); + return rbiRef.get().getReq().getLocale(); } private String[] joinNames(Set nameSet, String[] nameArray) {