question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fast memory leak/creep when cy.get fails to find the element, and eventually crashes

See original GitHub issue

Current behavior

While cy.get() retries to find an element , the memory usage of the tab shoots up quickly

See video https://youtu.be/N5jtyBdd05Q

Desired behavior

Memory usage should not increase this quickly

Test code to reproduce

describe('Memory issue', () => { it('Simple memory test', () => { cy.visit('https://www.androidpolice.com') cy.get('.garbageId') }) })

Cypress Version

5.1.0 and latest 8.6.0

Other

Initially I found this issue while testing my company’s website, then I found that it is easily reproducible with other websites as well

image

Pre-requisistes

  • numTestsKeptInMemory has to be set to 1 or higher
  • set defaultCommandTimeout to a higher value (eg. 50000) so there’s more time for the memory to increase

Steps to reproduce

  1. Visit a website (eg. https://www.androidpolice.com)
  2. Use cy.get() to find an non-existent element

As you can see in the video linked above, the memory usage starts shooting up when Cypress is trying to find the element. When you re-run the test without closing the tab, the memory doesn’t reset and keeps adding up. Eventually when the usage reaches 4GB, the tab crashes with the “Aw snap” message.

This issue is a lot more apparent if the test is longer with more steps, which can put the memory usage close to the ceiling of 4GB.

So far the reliable workaround is to set numTestsKeptInMemory to 0, which stops the memory creep altogether.

Very occasionally I can observe the garbage collection kicking in and reducing the memory usage, however the cy.get() still rapidly increases the memory usage after

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
BlueWindscommented, Dec 16, 2021

So a bit of an update here - the retries were the dragon’s tail. Tug it, and you get not a snake but a dragon nesting in the depths of the codebase with its treasure horde and toothy grin.

I’ve updated the attached PR with a better solution, which should reduce unnecessary DOM snapshotting in any situation where implicit assertions that an element exists in the DOM fail - including temporarily while we’re waiting for an element to be actionable in tests that eventually pass.

1reaction
BlueWindscommented, Dec 8, 2021

Poking around here further, it’s because of DOM snapshotting. Whenever an assertion fails (badSelector doesn’t exist on the page), we call Cypress.log(). https://github.com/cypress-io/cypress/blob/develop/packages/driver/src/cy/assertions.ts#L200. This happens several times per second as we retry the query, and because this is a DOM-related assertion, we snapshot the dom for each failure.

In Firefox, these snapshots get cleaned up efficiently, but in the current electron version (and certain chromium / chrome versions) they stick around in memory. While it might be possible to clean up the snapshots better, I’m still investigating why we make dozens of copies of the DOM per second. Even if they’re GCed regularly, it still seems wasteful. Leaking memory at 100mb / second means that we’re allocating memory at 100mb / second.

All this is fairly deem in the Cypress internals, and not easy to debug / change regardless.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fixing Cypress errors part 1: chromium out of memory crashes
Error : Out of memory, chromium renderer crashed. Chromium crashing is one of the issues I kept seeing in the pipeline. It would...
Read more >
Error Messages
Getting this error means you've tried to interact with a "dead" DOM element - meaning the current subject has been removed from the...
Read more >
Cypress Tips and Tricks | Better world by better software
Whenever a test fails, I watch the failure video side by side with the video from the last successful test run.
Read more >
Cypress long automation script crashes the Chrome ...
At 0 you will not get any DOM snapshots. The memory usage will still climb, but it will climb at a much slower...
Read more >
cypress-page-object-model - NPM Package Overview
The following error was thrown by a plugin. We stopped running your tests because a plugin crashed. Please check your plugins file.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found