From e6af0f0a5a1333d06e18d0149d44231a5f8e654d Mon Sep 17 00:00:00 2001 From: Jonatan Kronqvist Date: Wed, 28 Aug 2013 13:53:45 +0300 Subject: Avoid leaking memory from inherited ThreadLocales. Fixes #12401 The issue is fixed by changing the normal HashMap inside the inheritable thread local to a map implementation holding only weak references to the values (WeakValueMap). Also included is a test UI that starts threads, which run until the JVM is quit. This along with VisualVM was used to reproduce the issue and verify the fix. Change-Id: I116cc4e56e8a19c3b770abab6b18b9e262f4dafa --- .../tests/performance/ThreadMemoryLeaksTest.java | 57 ++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 uitest/src/com/vaadin/tests/performance/ThreadMemoryLeaksTest.java (limited to 'uitest') diff --git a/uitest/src/com/vaadin/tests/performance/ThreadMemoryLeaksTest.java b/uitest/src/com/vaadin/tests/performance/ThreadMemoryLeaksTest.java new file mode 100644 index 0000000000..5ffc7141af --- /dev/null +++ b/uitest/src/com/vaadin/tests/performance/ThreadMemoryLeaksTest.java @@ -0,0 +1,57 @@ +package com.vaadin.tests.performance; + +import java.util.Date; +import java.util.Timer; +import java.util.TimerTask; + +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.Button; +import com.vaadin.ui.Label; + +public class ThreadMemoryLeaksTest extends AbstractTestUI { + + public static class Worker { + long value = 0; + private TimerTask task = new TimerTask() { + @Override + public void run() { + value++; + } + }; + private final Timer timer = new Timer(true); + + public Worker() { + timer.scheduleAtFixedRate(task, new Date(), 1000); + } + } + + int workers = 0; + Label label; + + @Override + protected void setup(VaadinRequest request) { + label = new Label(String.format("%d workers", workers)); + addComponent(label); + addComponent(new Button("Add worker", new Button.ClickListener() { + @Override + public void buttonClick(Button.ClickEvent event) { + new Worker(); + workers++; + label.setValue(String.format("%d workers", workers)); + } + })); + } + + @Override + protected String getTestDescription() { + return "Inherited ThreadLocals should not leak memory. Clicking the " + + "button starts a new thread, after which memory consumption " + + "can be checked with visualvm"; + } + + @Override + protected Integer getTicketNumber() { + return 12401; + } +} -- cgit v1.2.3