Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve memory leak examples for modern V8 #2851

Closed
victor-homyakov opened this issue Oct 4, 2023 · 1 comment · Fixed by #2889
Closed

Improve memory leak examples for modern V8 #2851

victor-homyakov opened this issue Oct 4, 2023 · 1 comment · Fixed by #2889
Labels
ADO cat: devtools DevTools-related content.

Comments

@victor-homyakov
Copy link

victor-homyakov commented Oct 4, 2023

Documentation page https://learn.microsoft.com/en-us/microsoft-edge/devtools-guide-chromium/memory-problems/heap-snapshots has a few examples of memory leaks:

The code to demonstrate easily detectable memory leak looks like

function createLargeClosure() {
    var largeStr = new Array(1000000).join('x');
    var lC = function() {
        return largeStr;
    };
    return lC;
}

The problem is that this code does not create large closures anymore.

On modern versions of V8 the code var largeStr = new Array(1000000).join('x') creates a string of type CONS_ONE_BYTE_STRING_TYPE, which takes only about 600 bytes in memory (concatenation of concatenations of concatenations etc. of a single small string chunk like 'xxxxxxxx') instead of desired million bytes. In order to create really large closure you should use one of methods on string, which convert string into its plain form ONE_BYTE_STRING_TYPE. Right now some of those methods are indexOf('something'), slice(1), substring(1), toLowerCase() (situation may change with the next versions of V8).

The large memory leak example should look like

function createLargeClosure() {
    var largeStr = 'x'.repeat(1000000).toLowerCase(); // ONE_BYTE_STRING_TYPE, takes 1000012 bytes on my machine
    var lC = function() {
        return largeStr;
    };
    return lC;
}

Also don't forget to fix similar code on the example page https://microsoftedge.github.io/Demos/devtools-memory-heap-snapshot/example-07.html.


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

AB#46865090

@captainbrosset
Copy link
Contributor

Thank you for the very detailed report and solution. I will mark this as tracked on our backlog so we can fix the docs soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ADO cat: devtools DevTools-related content.
Projects
None yet
2 participants