Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Memory leaks phantomjs不关闭和离开孤立进程_Memory Leaks_Webdriver_Phantomjs_Ghostdriver - Fatal编程技术网

Memory leaks phantomjs不关闭和离开孤立进程

Memory leaks phantomjs不关闭和离开孤立进程,memory-leaks,webdriver,phantomjs,ghostdriver,Memory Leaks,Webdriver,Phantomjs,Ghostdriver,在PhantomJS 1.9.2、Ubuntu12 LTS和Ghostdirver 1.04以及selenium 2.35上,我在测试后得到了悬空的PhantomJS进程。有人知道如何解决这个问题的好方法吗 下面是一个测试程序,演示了奇怪的行为: package testing; import org.openqa.selenium.WebDriver; import org.openqa.selenium.phantomjs.PhantomJSDriver; import org.openq

在PhantomJS 1.9.2、Ubuntu12 LTS和Ghostdirver 1.04以及selenium 2.35上,我在测试后得到了悬空的PhantomJS进程。有人知道如何解决这个问题的好方法吗

下面是一个测试程序,演示了奇怪的行为:

package testing;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;

public class PhantomIsNotKilledDemo {

    private static WebDriver getDriver(){
        String browserPathStr = System.getProperty("selenium.pathToBrowser");
        if (browserPathStr == null) browserPathStr = "/home/user1/apps/phantomjs/bin/phantomjs";

        DesiredCapabilities caps = DesiredCapabilities.phantomjs();

        caps.setCapability("takesScreenshot", true);
        caps.setCapability(
                PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
                browserPathStr );

        WebDriver driver = new PhantomJSDriver(caps);

        return driver;
    }

    public static void main(String[] args) {
        int max = 10;
        for (int i = 0; i < max; i++){
            WebDriver d1 = getDriver();
            d1.get("http://www.imdb.com/title/tt1951264");

            System.out.println("done with cycle " + (i+1) +" of "+max);
            d1.close();
            //d1.quit();
        }

        System.out.println("done");
        System.exit(0);
    }
}
找到10个悬空的幻影进程

如果我改用
d1.quit()
,我就不会有悬而未决的进程。这显然更好,但我仍然希望通过
.close
获得相同的结果

注意,这是一个


更新根据Richard的建议更改此帖子(见下文)

我会将您的代码重写为如下内容:

public static void main(String[] args) {
    int max = 10;
    for (int i = 0; i < max; i++){
        WebDriver d1 = getDriver();

        d1.get("http://www.imdb.com/title/tt1951264");

        System.out.println("done with cycle " + (i+1) +" of "+max);
        d1.quit();
    }

    System.out.println("done");
    System.exit(0);
}
publicstaticvoidmain(字符串[]args){
int max=10;
对于(int i=0;i
我不完全清楚为什么
.close()
没有结束WebDriver。理论上,
.close()
如果在最后一个打开的窗口中被调用,应该退出WebDriver。当调用url时,可能有什么东西正在打开第二个窗口?或者,
.close()
对phantomjs的工作方式可能不同


至于为什么
.quit()
没有关闭所有的phantomjs会话,您调用的最后一个
getDriver()
在循环之外没有相应的
.quit()
。我重组了for循环以创建WebDriver实例,执行测试,然后在每个循环结束时退出WebDriver/phantomjs会话。

您应该使用
quit()
来终止进程,而不是
close()

正如您所发现的,close将关闭当前窗口(和浏览器),但不会关闭进程。如果要向进程发送其他命令或要检查进程,这将非常有用

“退出”适用于您希望关闭每个窗口并停止进程的情况,这听起来像是您正在寻找的

这两种方法的名称如下:

关闭()

关闭当前窗口,如果是当前窗口,则退出浏览器 当前打开的最后一个窗口

退出()

退出此驱动程序,关闭每个相关窗口


关于
WebDriver d1=getDriver()的错误位置,您是对的我改变我的问题。谢谢你指出这一点。然而,真正的问题是为什么
.close()
没有做它应该做的事情。实际上只有一个窗口。您可以使用其他URL,并具有相同的效果。最后,有人对此进行了说明。也许他们澄清了文件,因为我问了我的问题,因为当时我没有看到区别。这仍然是一个相当奇怪和相当意外的行为,但哦,好吧,就这样吧。谢谢你指出这一点。
public static void main(String[] args) {
    int max = 10;
    for (int i = 0; i < max; i++){
        WebDriver d1 = getDriver();

        d1.get("http://www.imdb.com/title/tt1951264");

        System.out.println("done with cycle " + (i+1) +" of "+max);
        d1.quit();
    }

    System.out.println("done");
    System.exit(0);
}