mardi 28 avril 2020

Memory not free on Tomcat with my .war project for Search and Run Junit Test Method/Class

in my function i load jar for search in all class all Test Method (Annotation.Test or TestFactory). when i call this method memory not is free, infact with free -h in poll i view that when call this function memory increases and at end not released. I type: j.close(); and child.close(); for close JarFile and URLClassLoader.

public JarClassMethod getAllTests() {

    JarClassMethod testClassObjMap= new JarClassMethod(); //<-----  simple HashMap<String,HashMap<String, List<String>>>

    LoadLibrary loadLibrary=new LoadLibrary();
    List<JarFile> jarList= loadLibrary.getListJar(pathJars).stream().map(f -> {
        try {
            return new JarFile(f);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    })
        .collect(Collectors.toList());

    for (JarFile j : jarList) {
        URLClassLoader child = new URLClassLoader(
                new URL[] {new File(j.getName()).toURI().toURL()},
                this.getClass().getClassLoader()
        );

        for (Enumeration<JarEntry> entries = j.entries(); entries.hasMoreElements(); ) {
            JarEntry entry = entries.nextElement();
            String file = entry.getName();
            if (file.endsWith(".class")) {
                String classname = file.replaceAll("/", ".")
                        .substring(0, file.lastIndexOf("."));
                try {
                    Class<?> currentClass = Class.forName(classname,true,child);
                    List<String> testMethods = new ArrayList<>();
                    for (int i = 0; i < currentClass.getMethods().length; i++) {
                        Method method = currentClass.getMethods()[i];
                        Annotation annTest = method.getAnnotation(Test.class);
                        Annotation annTestFactory = method.getAnnotation(TestFactory.class);
                        if (annTest != null || annTestFactory != null) {
                            testMethods.add(method.getName());
                        }
                    }//fine for metodi
                    if (testMethods.size() >=1) {
                        //if windows: testClassObjMap.put(j.getName().substring(j.getName().lastIndexOf("\\")+1),classname,testMethods);
                        testClassObjMap.put(j.getName().substring(j.getName().lastIndexOf("/")+1),classname,testMethods);
                        System.out.format("%s %s %s",j.toString(),classname,testMethods);
                    }

                }catch (NoClassDefFoundError e) {
                    System.out.println("WARNING: failed NoClassDefFoundError " + classname + " from " + file);
                } 
                catch (Throwable e) {
                    System.out.println("WARNING: failed to instantiate " + classname + " from " + file);
                }

            }//if .class

        }//chiudo jarentry for
        j.close();
        child.close();
    }//chiudo jarfile for
    return testClassObjMap;
}


public class LoadLibrary {
    public List<File> getListJar(String path_jars) throws IOException {
        String[] extensions = new String[] { "jar" };
        List<File> jarFiles = (List<File>) FileUtils.listFiles(new File(path_jars), extensions, false);
        return jarFiles;
}

//method for runTest
public void runOne(String jar, String class_name, Optional<String> test_name, TestExecutionListener listener)  {
        Launcher launcher = LauncherFactory.create();

        ClassLoader loader = URLClassLoader.newInstance(
                new URL[] { new File(pathJars+"/"+jar).toURI().toURL() },
                getClass().getClassLoader()
            );

        loader.getClass();
        Class cls=Class.forName(class_name,true,loader);
        Constructor constructor = cls.getConstructor();
        constructor.newInstance();

        LauncherDiscoveryRequest request;
        if (test_name.isPresent()) {
            Method m = cls.getMethod(test_name.get());
            request = LauncherDiscoveryRequestBuilder.request()
                    .selectors(selectMethod(cls,m))
                    .build();
        }
        else{
            request = LauncherDiscoveryRequestBuilder.request()
                    .selectors(selectClass(cls))
                    .build();
        }

        TestPlan testPlan = launcher.discover(request);
        launcher.registerTestExecutionListeners(listener);
        launcher.execute(request);

    }

For you I close all? how free memory at end of each function RunOne and getAllTests()?

thanks Regards





Aucun commentaire:

Enregistrer un commentaire