profesorfalken / jProcesses

Get crossplatform processes details with Java
Apache License 2.0
63 stars 26 forks source link

Handle leak running in Windows #32

Open effad opened 4 years ago

effad commented 4 years ago

When running this code with Java 13 in Windows:

import java.util.List;

import org.jutils.jprocesses.JProcesses;
import org.jutils.jprocesses.model.ProcessInfo;

public class LeakTest {

    public static void main(String[] args) throws InterruptedException {
        JProcesses jpro = JProcesses.get();
        jpro.fastMode();
        while (true) {
            List<ProcessInfo> processInfos = jpro.listProcesses();
            Thread.sleep(100);
            System.out.println("Run " + System.currentTimeMillis());
        }
    }
}

the number of open windows (file) handles will grow constantly.

effad commented 4 years ago

The leak also occurs with JDK-1.8.0_202

effad commented 4 years ago

I think the actual bug is in com.profesorfalken.wmi4java.WMIVBScript.executeScript(String).

effad commented 4 years ago

By adding

                    process.getInputStream().close();
                    process.getOutputStream().close();
                    process.getErrorStream().close();

at the end of executeScript the leak can be made smaller, but it does not disappear altogether.

effad commented 4 years ago

By adding

                  process.getInputStream().close();
                  process.getOutputStream().close();
                  process.getErrorStream().close();

at the end of executeScript the leak can be made smaller, but it does not disappear altogether.

Yes it does disappear completely, but the Garbage Collector has to be run in order for the handles to be freed.