小编use*_*918的帖子

JDK1.7 ClassLoader内存泄漏

我有孩子优先的UrlClassLoader来动态加载jar文件.然后我做一个反射来调用加载的jar文件中的方法.一旦完成,我宁愿卸载类加载器.然后我尝试做一些压力测试代码,以确保我的代码顺利运行.基本上,我尝试做的是在循环语句中加载和卸载jar.这是我的代码:

    for (int i = 0; i < 1000; i++) {
        //Just to show the progress
        System.out.println("LOAD NUMBER : " + i);

        ChildFirstURLClassLoader classLoader = null;
        try {
            File file = new File("C:\\library.jar");
            String classToLoad = "com.test.MyClass";
            URL jarUrl = new URL("file:" + file.getAbsolutePath());

            classLoader = new ChildFirstURLClassLoader(new URL[] {jarUrl}, null);
            Class<?> loadedClass = classLoader.loadClass(classToLoad);
            Method method = loadedClass.getDeclaredMethod("execute",
                    new Class[] {});

            ClassLoader currCl= Thread.currentThread().getContextClassLoader();
            Thread.currentThread().setContextClassLoader(classLoader);
            method.invoke(null);
            Thread.currentThread().setContextClassLoader(currCl);

            method = null;
            loadedClass = null;
        } finally {
            if (classLoader != …
Run Code Online (Sandbox Code Playgroud)

java memory-leaks classloader

12
推荐指数
1
解决办法
1251
查看次数

标签 统计

classloader ×1

java ×1

memory-leaks ×1