Originally by dnebing
VaadinPortlet does not implement destroy() so the CurrentInstance leaves artifacts in memory (memory leak) when destroyed.
VaadinPortlet should implement the destroy() method and should invoke a new method on CurrentInstance to clean up and destroy the thread local.
Imported from https://dev.vaadin.com/ issue #11372
Should also probably add a portletDestroyed() method to allow subclasses to clean up resources, modeled after the portletInitialized() method to allow subclasses to initialize themselves.
Originally by @Legioth
Clearing CurrentInstance in destroy() would only clean up the ThreadLocal of the thread where destroy() is run whereas any leaked CurrentInstance on other threads would still be there. The ThreadLocal API does not seem to allow accessing a ThreadLocal of another thread.
The correct way of avoiding leaks is to make sure CurrentInstance.clearAll() is run in a finally block in all places where there might be code that has set any instance. #11906 will fix this for VaadinServlet.init() and VaadinPortlet.init().
Please create separate tickets for any other situations where CurrentInstance.clearAll() might be missing.