我read以下代码导致内存泄漏,因为Level类保存了对Classloader外部的CUSTOMLEVEL成员的引用:
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");
}
}
问题是如何在这个特定的类加载器之外加载Level类? 我可以在java API中有类似行为的其他实例吗?