From 3cc90e37d8abfa53d127691ae0b7641298d64fa2 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Tue, 26 Mar 2013 01:41:48 +0200 Subject: Fixed minimal (empty hashmap) memory leak on redeploy (#9993) Change-Id: I2b3f83220070f1f46730d956abb24ba9edf02f20 --- server/src/com/vaadin/util/CurrentInstance.java | 35 ++++++++++++++++++------- 1 file changed, 26 insertions(+), 9 deletions(-) (limited to 'server/src') diff --git a/server/src/com/vaadin/util/CurrentInstance.java b/server/src/com/vaadin/util/CurrentInstance.java index a2cfd4fff7..60489d596e 100644 --- a/server/src/com/vaadin/util/CurrentInstance.java +++ b/server/src/com/vaadin/util/CurrentInstance.java @@ -68,6 +68,10 @@ public class CurrentInstance implements Serializable { @Override protected Map, CurrentInstance> childValue( Map, CurrentInstance> parentValue) { + if (parentValue == null) { + return null; + } + Map, CurrentInstance> value = new HashMap, CurrentInstance>(); // Copy all inheritable values to child map @@ -79,11 +83,6 @@ public class CurrentInstance implements Serializable { return value; } - - @Override - protected Map, CurrentInstance> initialValue() { - return new HashMap, CurrentInstance>(); - } }; private CurrentInstance(Object instance, boolean inheritable) { @@ -100,7 +99,11 @@ public class CurrentInstance implements Serializable { * if there is no current instance. */ public static T get(Class type) { - CurrentInstance currentInstance = instances.get().get(type); + Map, CurrentInstance> map = instances.get(); + if (map == null) { + return null; + } + CurrentInstance currentInstance = map.get(type); if (currentInstance != null) { return type.cast(currentInstance.instance); } else { @@ -142,11 +145,25 @@ public class CurrentInstance implements Serializable { } private static void set(Class type, T instance, boolean inheritable) { + Map, CurrentInstance> map = instances.get(); if (instance == null) { - instances.get().remove(type); + // remove the instance + if (map == null) { + return; + } + map.remove(type); + if (map.isEmpty()) { + instances.remove(); + map = null; + } } else { assert type.isInstance(instance) : "Invald instance type"; - CurrentInstance previousInstance = instances.get().put(type, + if (map == null) { + map = new HashMap, CurrentInstance>(); + instances.set(map); + } + + CurrentInstance previousInstance = map.put(type, new CurrentInstance(instance, inheritable)); if (previousInstance != null) { assert previousInstance.inheritable == inheritable : "Inheritable status mismatch for " @@ -163,7 +180,7 @@ public class CurrentInstance implements Serializable { * Clears all current instances. */ public static void clearAll() { - instances.get().clear(); + instances.remove(); } /** -- cgit v1.2.3