GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — high tls memory usage (rss) · Issue #1522 · nodejs/node

high tls memory usage (rss) #1522

New issue
Copy link
New issue
Copy link
Closed
Closed
high tls memory usage (rss)#1522
Copy link
Labels
httpsIssues and PRs related to the https subsystem.memoryIssues and PRs related to Node.js memory management or memory footprint.tlsIssues and PRs related to the tls subsystem.

Description

@ChALkeR
ChALkeR
opened on Apr 25, 2015
Issue body actions

https.request leaks in this example:

'use strict';
var https = require('https');
var options = {
    hostname: 'google.com'
};
function next() {
    setTimeout(iterate, 10);
}
function onError(error) {
    console.log(error);
}
function noop() {}
function onResponse(res) {
    console.log('statusCode: ', res.statusCode);
    res.on('data', noop);
    res.on('end', next);
}
function iterate() {
    console.log(JSON.stringify(process.memoryUsage()));
    console.log('iterating...');
    https.request(options, onResponse).
        on('error', onError).
        end();
};
iterate();

rss grows over time, heapUsed remains about constant (~30m).
This could be reproduced with all released iojs versions that I tried v1.0.1 and v1.8.1.

Node v0.12.2 behaves better in this testcase: rss grows to about 100m, but goes down periodically (I wouldn't call that a perfect behaviour, let's call that «fine»).

53ba494, dad73f6 and 8a83eba are three sequental commits.
53ba494 is fine (as 0.12), dad73f6 fails to build, 8a83eba leaks.
dad73f6 is «upgrade v8 to 3.31.74.1» with 831 files changed, 104394 insertions(+), 21427 deletions(-).
Node v0.12.2 could be fine because it still uses v8 3.28.73.
io.js 1.0.0/1.1.0 + shared v8 3.30.33.16 behaves fine (the same as 0.12).

All the testcases and massif data are at http://oserv.org/bugs/iojs/memory0/

Activity

Next
  1. ChALkeR commented on Apr 25, 2015

    @ChALkeR
    ChALkeR
    on Apr 25, 2015
    MemberAuthor
    More actions

    Could be reproduced with wreck:

    'use strict';
    var wreck = require('wreck');
    function callback(error, res, payload) {
        if (error) {
            console.log(error);
            return;
        }
        console.log('statusCode: ', res.statusCode);
        setTimeout(iterate, 10);
    }
    function iterate() {
        console.log(JSON.stringify(process.memoryUsage()));
        console.log('iterating...');
        wreck.get('https://google.com/', callback);
    };
    iterate();
  2. ChALkeR commented on Apr 25, 2015

    @ChALkeR
    ChALkeR
    on Apr 25, 2015
    MemberAuthor
    More actions

    I found a work-around for that particular testcase.
    It's not applicable in real world, i think, but it could give some ideas on where the bug is.

    'use strict';
    
    var requests = 20;
    var total = 1000;
    // If `maxSockets` is less then `requests`, then RSS stops growing from some point.
    var maxSockets = 10;
    
    var https = require('https');
    var agent = new https.Agent();
    agent.maxSockets = maxSockets;
    var count = 0;
    var options = {
        agent: agent,
        hostname: 'google.com'
    };
    function next() {
        setTimeout(iterate, 0);
    }
    function onError(error) {
        console.log(error);
    }
    function noop() {}
    function onResponse(res) {
        console.log('statusCode: ', res.statusCode);
        res.on('data', noop);
        res.on('end', next);
    }
    function iterate() {
        console.log(JSON.stringify(process.memoryUsage()));
        if (count >= total) process.exit();
        console.log('iterating... ' + ' num: ' + (++count));
        https.request(options, onResponse).
            on('error', onError).
            end();
    };
    for (var i = 0; i < requests; i++) iterate();

    In this example, if agent.maxSockets is less than the number of https.requests being executed at the same time, then RSS stops growing.
    If only one https.request is being executed at every moment, then it can't help.
    agent.maxSockets = 10 and 20 calls to https.request is ok
    agent.maxSockets = 10 and 10 calls to https.request is not ok

    http://oserv.org/bugs/iojs/memory0/test-agent.js — testcase.
    http://oserv.org/bugs/iojs/memory0/test-agent.txt — results.

    http://oserv.org/bugs/iojs/memory0/test-fast.js could be used to reproduce this a bit faster (20 requests at the same time).

  3. ChALkeR commented on Apr 25, 2015

    @ChALkeR
    ChALkeR
    on Apr 25, 2015
    MemberAuthor
    More actions

    cc @indutny

  4. ChALkeR commented on Apr 25, 2015

    @ChALkeR
    ChALkeR
    on Apr 25, 2015
    MemberAuthor
    More actions

    http://oserv.org/bugs/iojs/memory0/test-fast.js could be used to reproduce this a bit faster (20 requests at the same time).

  5. mscdex
    added
    httpsIssues and PRs related to the https subsystem.
    on Apr 25, 2015
  6. ChALkeR commented on Apr 25, 2015

    @ChALkeR
    ChALkeR
    on Apr 25, 2015
    MemberAuthor
    More actions

    io.js 1.8.1:
    iojs v1.8.1

    node 0.12.2:
    node v0.12.2

    The time that those two were running for could be different, but you could clearly see the difference still.

  7. ChALkeR commented on Apr 25, 2015

    @ChALkeR
    ChALkeR
    on Apr 25, 2015
    MemberAuthor
    More actions

    Actually, it goes down after manually calling gc().
    If I call gc() manually every 100 requests, RSS does not go above 70 MiB.

    Could it be that gc() is never called?

  8. ChALkeR commented on Apr 25, 2015

    @ChALkeR
    ChALkeR
    on Apr 25, 2015
    MemberAuthor
    More actions

    Until a full gc run (calling gc()), TLSWrap native objects get never deleted.
    Full gc is not called automatically, probably because v8 thinks that incremental gc works fine (well, heapUsed is ~30 MiB most of the time).

    Full gc is not called automatically until ~500MiB rss (without valgrind/massif) is reached.

  9. ChALkeR commented on Apr 25, 2015

    @ChALkeR
    ChALkeR
    on Apr 25, 2015
    MemberAuthor
    More actions

    But even when rss reaches ~500MiB and full gc is called (deleting TLSWrap objects), rss memory usage does not go down.

  10. indutny commented on Apr 25, 2015

    @indutny
    indutny
    on Apr 25, 2015
    Member
    More actions

    @ChALkeR I have modified the test case to work with localhost server (partly because I'm on 3G internet, partly because I'm not so patient). And it appears to me that it grows up to 1.1gb very fast, then drops to 800mb and then stays around 600mb for 50 parallel connections.

    The breaking point (1.1gb) happens at 21373 iteration for me. Have you tried waiting that long?

  11. indutny commented on Apr 25, 2015

    @indutny
    indutny
    on Apr 25, 2015
    Member
    More actions

    I have a fix for lowering 1.1gb, btw. Will push a PR tomorrow.

  12. ChALkeR commented on Apr 26, 2015

    @ChALkeR
    ChALkeR
    on Apr 26, 2015
    MemberAuthor
    More actions

    Lowering to which point? If agent.maxSockets is lower than the number of parallel connections, rss somehow stabilizes at ~40 MiB. What's the difference in this case?

    Just checked — in this case TLSWrap objects are reused.
    For 20 parallel connections and 1000 total requests:

    • with agent.maxSockets = 10 — only 10 TLSWrap objects are created,
    • with agent.maxSockets = 17 — only 17 TLSWrap objects are created,
    • with agent.maxSockets = 18 — 21 TLSWrap objects are created,
    • with agent.maxSockets = 19 — 83 TLSWrap objects are created,
    • with agent.maxSockets = 20 — 1000 TLSWrap objects are created.
  13. indutny commented on Apr 26, 2015

    @indutny
    indutny
    on Apr 26, 2015
    Member
    More actions

    @ChALkeR to 400mb as far as I remember. Anyway, do you see that everything is getting collected after growing to 1gb?

  14. ChALkeR commented on Apr 26, 2015

    @ChALkeR
    ChALkeR
    on Apr 26, 2015
    MemberAuthor
    More actions

    No.

    For me, full gc kicks in at 500 MiB. But it does not lower rss memory usage, it just (almost) stops growing.

    statusCode:  302
    {"rss":556351488,"heapTotal":75819520,"heapUsed":25609128}
    iterating...  num: 21408
    statusCode:  302
    {"rss":556351488,"heapTotal":75819520,"heapUsed":25723656}
    iterating...  num: 21409
    statusCode:  302
    {"rss":556351488,"heapTotal":75819520,"heapUsed":25751024}
    iterating...  num: 21410
    
  15. indutny commented on Apr 26, 2015

    @indutny
    indutny
    on Apr 26, 2015
    Member
    More actions

    @ChALkeR I wasn't talking about GC in general. I was talking about the point where all accumulated TLSWraps are collected and RSS lowers significantly.

  16. 53 remaining items

  17. shigeki
    added 4 commits that reference this issue on Apr 30, 2015

    tls: destroy SSL once it is out of use

    ea9a33a

    crypto: track external memory for SSL structures

    34a4642

    tls: zero SSL_CTX freelist for a singleUse socket

    c001422

    tls: destroy singleUse context immediately

    ce1d336
  18. indutny
    added 8 commits that reference this issue on Apr 30, 2015

    tls: destroy SSL once it is out of use

    2d241b3

    crypto: track external memory for SSL structures

    e6874dd

    tls: zero SSL_CTX freelist for a singleUse socket

    2684c90

    tls: destroy singleUse context immediately

    1787416

    tls: destroy SSL once it is out of use

    7ada680

    crypto: track external memory for SSL structures

    74060bb

    tls: zero SSL_CTX freelist for a singleUse socket

    1cfc455

    tls: destroy singleUse context immediately

    b72e4bc
  19. silverwind commented on Apr 30, 2015

    @silverwind
    silverwind
    on Apr 30, 2015
    Contributor
    More actions

    Fixed by 2d241b3 ... 1787416

  20. silverwind
    closed this as completedon Apr 30, 2015
  21. ChALkeR
    added
    memoryIssues and PRs related to Node.js memory management or memory footprint.
    on Aug 17, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    httpsIssues and PRs related to the https subsystem.memoryIssues and PRs related to Node.js memory management or memory footprint.tlsIssues and PRs related to the tls subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.