Search code examples
javaloggingmemory-leaksclassloader

Memory leak caused by Logger


I read that the following code leads to a memory leak as the Level class holds a reference to the CUSTOMLEVEL member from outside the Classloader:

public class LeakServlet extends HttpServlet {
  private static final String STATICNAME = "This leaks!";
  private static final Level CUSTOMLEVEL = new Level("test", 550) {}; // anon class!

  protected void doGet(HttpServletRequest request, HttpServletResponse response)
                 throws ServletException, IOException {
      Logger.getLogger("test").log(CUSTOMLEVEL, "doGet called");
  }
}

The question is how is the Level class loaded outside this particular classloader ?
Can I have some other instances within the java API of a similar behaviour?


Solution

  • There are lots of sources for class loader leaks: This part of a blog series on the issue, enumerates some of the culprits.