19

I am using PhantomJS via Selenium and encountered a problem on one website with a lot of images.

When I am trying to take screenshot PhantomJS process memory consumption gets very high, ≈400-450 MB (≈100 MB before screenshot)

With --load-images=no it is better, ≈70-100 MB.

Is there any way to solve this issue without disabling images completely? Maybe it is possible to take screenshot of visible area only instead of full page?

With other WebDrivers (such as Chrome) it is working fine.

class Program
{
    public static RemoteWebDriver CreatePhantomJsDriver()
    {
        var service = PhantomJSDriverService.CreateDefaultService();
        //service.AddArgument("--load-images=no");

        var options = new PhantomJSOptions();
        options.AddAdditionalCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36");

        return new PhantomJSDriver(service, options);
    }

    public static void SaveScreenshot(RemoteWebDriver driver)
    {
        try
        {
            driver.TakeScreenshot().SaveAsFile(DateTime.Now.Ticks + ".jpg", ImageFormat.Jpeg);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

    static void Main(string[] args)
    {
        using (var driver  = CreatePhantomJsDriver())
        {
            driver.Manage().Window.Size = new Size(1600, 1200);

            driver.Url = "http://color-looks.ru/index";

            Thread.Sleep(5000);

            SaveScreenshot(driver);

            Thread.Sleep(5000);
        }
    }
}

(it is not the website I was using because it requires login/password auth, just some image heavy website from Google, memory consumption is a bit lower but still huge — ≈300 MB)

11
  • You could set the page.clipRect, but I don't know if it would be useful to you and whether you can set it in the same way you set the user agent string.
    – Artjom B.
    Aug 31, 2015 at 9:17
  • Have you seen stackoverflow.com/questions/8647980/… ?
    – Nathan
    Sep 10, 2015 at 4:10
  • @Nathan, I don't see how this can help me. I don't want to close the page immediately after taking the screenshot, and when I close it (by disposing the driver object) the memory is released fine. It is also decreased back to normal if I go to another page/URL. My problem is that memory consumption highly increases when I take screenshot on a page, and because of that I cannot use multiple (5-8) instances of PhantomJS at the same time (not enough memory on the machine I use for that).
    – Alex P.
    Sep 10, 2015 at 4:55
  • @Alex11223 I've tried to fiddle a little with the code and the page you've provided produces a peak of 66mb, varrying a little with each page load. As Nathan suggests, the GC isn't collecting everything so enforcing it to collect right after SaveScreenshot does lower the memory consumption back down to some 20mb and keeps it there no matter the screenshot count. For comparison, a Chrome tab uses 230mb to render the page, so I think you're well off here. What do you mean with 'With other WebDrivers (such as Chrome) it is working fine' btw?
    – Heki
    Sep 10, 2015 at 6:39
  • @Heki I don't see such high memory usage with Chrome webdriver instead of PhantomJS. I have 2 Chrome processes (main process and tab process), 45-50 MB each. Are you sure it uses 230 MB?
    – Alex P.
    Sep 10, 2015 at 8:22

2 Answers 2

1

The method TakeScreenshot returns a Bitmap object. The code sample you provided does not dispose of this object, so the GDI object is hanging around in memory for a long time (possibly indefinitely).

Change your code to this:

public static void SaveScreenshot(RemoteWebDriver driver)
{
    try
    {
        using(var screenshot = driver.TakeScreenshot())
        {
            screenshot.SaveAsFile(DateTime.Now.Ticks + ".jpg", ImageFormat.Jpeg);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}
1
  • You also may want to make sure you shutdown the PhantomJS client after pulling the screenshot. Jul 18, 2016 at 17:06
0

try this one:

Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
ss.SaveAsFile(DateTime.Now.Ticks + ".jpg", ImageFormat.Jpeg);
0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.