9

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?

4

1 回答 1

3

类加载器泄漏的来源有很多: 关于这个问题的博客系列的这一部分,列举了一些罪魁祸首。

于 2013-05-27T09:49:56.477 回答