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

Memory leak in simple test case involving WeakSet #6180

Closed
Ginden opened this issue Apr 13, 2016 · 18 comments
Closed

Memory leak in simple test case involving WeakSet #6180

Ginden opened this issue Apr 13, 2016 · 18 comments
Labels
memory Issues and PRs related to the memory management or memory footprint. v8 engine Issues and PRs related to the V8 dependency.

Comments

@Ginden
Copy link

Ginden commented Apr 13, 2016

Version: 4.2.1, 5.1.10 confirmed.
Platform: at least Windows 10 and Linux

`Following code reproduces error:

'use strict';

const s = new WeakSet();
let j = 0;
function f(){
    for (let i = 0 ; i<100000; i++) {
        s.add({});  
    }
    console.log(`${j++} :${0|(process.memoryUsage().heapUsed/(1024*1024))}MB`);
    return Promise.resolve(null).then(f);
}
f();

It ends with following output

...
110 :853MB

<--- Last few GCs --->

   10993 ms: Scavenge 825.2 (856.1) -> 823.2 (863.1) MB, 49.7 / 0 ms [allocation failure].
   11126 ms: Scavenge 832.1 (863.1) -> 830.1 (870.1) MB, 43.4 / 0 ms [allocation failure].
   11255 ms: Scavenge 839.0 (870.1) -> 837.0 (877.1) MB, 44.6 / 0 ms [allocation failure].
   11390 ms: Scavenge 845.8 (877.1) -> 843.9 (884.1) MB, 47.5 / 0 ms [allocation failure].
   11517 ms: Scavenge 852.7 (884.1) -> 850.8 (891.1) MB, 42.5 / 0 ms [allocation failure].


<--- JS stacktrace --->

==== JS stack trace =========================================

Security context: 0000029D869E3AD1 <JS Object>
    1: add [native weak-collection.js:~92] [pc=0000036347B55F9B] (this=000003208D656691 <JS WeakSet>,k=00000167E7F946E9 <an Object with map 0000027C0AC172F1>)
    2: f(aka f) [C:\Users\michal.wadas\Documents\test_gc.js:7] [pc=0000036347B536C1] (this=0000029D86904189 <undefined>)
    3: arguments adaptor frame: 1->0
    4: /* anonymous */(aka /* anonymous */) [native promise.js:221] [pc=000003...

Objects are properly garbage collected in Chrome 49 (process.memoryUsage().heapUsed is changed to performance.memory.usedJSHeapSize).

Source: StackOverflow question.

@ChALkeR
Copy link
Member

ChALkeR commented Apr 13, 2016

The problem is that somewhy only scavenges are triggered here, but mark-sweeps aren't.
And scavenges can't collect those elements (for some reason).

In most real-world apps this wouldn't be a problem, because there are many things that push mark-sweep runs.

This is most likely already fixed in v8, if Chrome 49 is fine.
We should check how master is doing, I will be able to check that a bit later.

@ChALkeR ChALkeR added v8 engine Issues and PRs related to the V8 dependency. memory Issues and PRs related to the memory management or memory footprint. labels Apr 13, 2016
@vkurchatkin
Copy link
Contributor

@ChALkeR is there write-up on which heuristics are used by v8 for different types of GC? It seems that in node v5 and earlier WeakMap and WeakSet don't apply any pressure on garbage collector at all

@ChALkeR
Copy link
Member

ChALkeR commented Apr 13, 2016

@vkurchatkin That, I don't know.

/cc @jeisinger

@dnalborczyk
Copy link
Contributor

This is most likely already fixed in v8, if Chrome 49 is fine.

No garbage collection in master with v8 4.9.385.35 and in vee-eight-5.0 with v8 5.0.71.25 on OSX El Capitan 10.11.5 Beta 1, XCode v7.3

Daniel

@ChALkeR
Copy link
Member

ChALkeR commented Apr 13, 2016

@dnalborczyk Same as in it gets garbage collected or not?

@jeisinger
Copy link
Contributor

Are you running with 64 bit or 32 bit?

@dnalborczyk
Copy link
Contributor

@ChALkeR Oh, sorry, same as in the original issue description. No garbage collection. I corrected the above.

@dnalborczyk
Copy link
Contributor

Btw, if it helps, Chrome 51.0.2700.0 dev is eventually smoking up as well - though the heap size seems to remain the same ( if the output happens to be correct ). Firefox 47, Edge 14 (37), and Safari 9.1.1 keep running.

@jeisinger
Copy link
Contributor

filed https://bugs.chromium.org/p/v8/issues/detail?id=4909 to track this.

@jeisinger
Copy link
Contributor

btw, can you share a non-artificial example? I.e. something where you manage to fill up a weak set in a meaningful way without generating enough garbage anyways to trigger a full gc?

@benjamingr
Copy link
Member

@ChALkeR dumb question - but can't/should't node trigger a full GC before throwing an out of memory error? I'd actually expect v8 to do that.

@ChALkeR
Copy link
Member

ChALkeR commented Apr 15, 2016

@benjamingr That's v8 job and somewhy it doesn't do it here. It's mentioned in the issue that @jeisinger filed to v8 issue tracker:

Maybe we should try an emergency GC before aborting in the case of weakmaps and weaksets

@jeisinger
Copy link
Contributor

landed a fix in v8. turns out there was no code to actually free up GC'd slots in a weak collection... gives you an idea how much this feature is used on the web.

anyways, should probably be backmerged to node's lts version of v8

@targos
Copy link
Member

targos commented Apr 15, 2016

Link to the fix: https://codereview.chromium.org/1877233005

@jeisinger
Copy link
Contributor

turns out that at least one unit test, doesn't like the fix :)... investigating

ofrobots added a commit to ofrobots/node that referenced this issue Apr 15, 2016
Original commit messages:
v8/v8@e093a04
  Rehash and clear deleted entries in weak collections during GC
  Otherwise, they'll just keep growing until we run out of memory or hit the FixedArray's maximum capacity.

  BUG=v8:4909
  [email protected]
  LOG=n

  Review URL: https://codereview.chromium.org/1877233005

  Cr-Commit-Position: refs/heads/master@{nodejs#35514}

v8/v8@09db540
  Reland of Rehash and clear deleted entries in weak collections during GC
  BUG=v8:4909
  [email protected],[email protected]
  LOG=n

  Review URL: https://codereview.chromium.org/1890123002

  Cr-Commit-Position: refs/heads/master@{nodejs#35538}

V8-Bug: https://crbug.com/v8/4909
Fixes: nodejs#6180
@ChALkeR
Copy link
Member

ChALkeR commented Apr 26, 2016

#6375 (comment) by @jeisinger:

The WeakMap issue should be fixed here: https://codereview.chromium.org/1890123002

@benjamingr
Copy link
Member

Any chance the fix could be backported? @ChALkeR

@ChALkeR
Copy link
Member

ChALkeR commented Apr 26, 2016

/cc @bnoordhuis, @ofrobots

ofrobots added a commit to ofrobots/node that referenced this issue Apr 26, 2016
Original commit messages:
v8/v8@e093a04
  Rehash and clear deleted entries in weak collections during GC
  Otherwise, they'll just keep growing until we run out of memory or hit the FixedArray's maximum capacity.

  BUG=v8:4909
  [email protected]
  LOG=n

  Review URL: https://codereview.chromium.org/1877233005

  Cr-Commit-Position: refs/heads/master@{nodejs#35514}

v8/v8@09db540
  Reland of Rehash and clear deleted entries in weak collections during GC
  BUG=v8:4909
  [email protected],[email protected]
  LOG=n

  Review URL: https://codereview.chromium.org/1890123002

  Cr-Commit-Position: refs/heads/master@{nodejs#35538}

V8-Bug: https://crbug.com/v8/4909
Fixes: nodejs#6180
cjihrig pushed a commit that referenced this issue Aug 10, 2016
Original commit message:

If we can't rehash the backing store for weak sets & maps, do a last
resort GC

BUG=v8:4909
[email protected]

Committed: https://crrev.com/b93c80a6039c757723e70420ae73375b5d277814
Cr-Commit-Position: refs/heads/master@{#37591}

Fixes: #6180
PR-URL: #7689
Reviewed-By: Matt Loring <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
cjihrig pushed a commit that referenced this issue Aug 10, 2016
Original commit message:

Fix incorrect parameter to HasSufficientCapacity

It takes the number of additional elements, not the total target
capacity.

Also, avoid right-shifting a negative integer as this is undefined in
general

BUG=v8:4909
[email protected]

Review-Url: https://codereview.chromium.org/2162333002
Cr-Commit-Position: refs/heads/master@{#37901}

Fixes: #6180
PR-URL: #7689
Reviewed-By: Matt Loring <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
ofrobots pushed a commit to ofrobots/node that referenced this issue Aug 25, 2016
Original commit message:

If we can't rehash the backing store for weak sets & maps, do a last
resort GC

BUG=v8:4909
[email protected]

Committed: https://crrev.com/b93c80a6039c757723e70420ae73375b5d277814
Cr-Commit-Position: refs/heads/master@{#37591}

Fixes: nodejs#6180
PR-URL: nodejs#7689
Reviewed-By: Matt Loring <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
ofrobots pushed a commit to ofrobots/node that referenced this issue Aug 25, 2016
Original commit message:

Fix incorrect parameter to HasSufficientCapacity

It takes the number of additional elements, not the total target
capacity.

Also, avoid right-shifting a negative integer as this is undefined in
general

BUG=v8:4909
[email protected]

Review-Url: https://codereview.chromium.org/2162333002
Cr-Commit-Position: refs/heads/master@{nodejs#37901}

Fixes: nodejs#6180
PR-URL: nodejs#7689
Reviewed-By: Matt Loring <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
MylesBorins pushed a commit that referenced this issue Oct 10, 2016
Original commit messages:
v8/v8@e093a04
  Rehash and clear deleted entries in weak collections during GC
  Otherwise, they'll just keep growing until we run out of memory or hit the FixedArray's maximum capacity.

  BUG=v8:4909
  [email protected]
  LOG=n

  Review URL: https://codereview.chromium.org/1877233005

  Cr-Commit-Position: refs/heads/master@{#35514}

v8/v8@09db540
  Reland of Rehash and clear deleted entries in weak collections during GC
  BUG=v8:4909
  [email protected],[email protected]
  LOG=n

  Review URL: https://codereview.chromium.org/1890123002

  Cr-Commit-Position: refs/heads/master@{#35538}

Ref: https://crbug.com/v8/4909
Ref: #7883
Fixes: #6180
PR-URL: #7689
Reviewed-By: Matt Loring <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
MylesBorins pushed a commit that referenced this issue Oct 10, 2016
Original commit message:

If we can't rehash the backing store for weak sets & maps, do a last
resort GC

BUG=v8:4909
[email protected]

Committed: https://crrev.com/b93c80a6039c757723e70420ae73375b5d277814
Cr-Commit-Position: refs/heads/master@{#37591}

Fixes: #6180
Ref: #7883
PR-URL: #7689
Reviewed-By: Matt Loring <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
MylesBorins pushed a commit that referenced this issue Oct 10, 2016
Original commit message:

  Fix incorrect parameter to HasSufficientCapacity

  It takes the number of additional elements, not the total target
  capacity.

  Also, avoid right-shifting a negative integer as this is undefined in
  general

  BUG=v8:4909
  [email protected]

Review-Url: https://codereview.chromium.org/2162333002
Cr-Commit-Position: refs/heads/master@{#37901}

Fixes: #6180
Ref: #7883
PR-URL: #7689
Reviewed-By: Matt Loring <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
MylesBorins pushed a commit that referenced this issue Oct 10, 2016
Original commit messages:
v8/v8@e093a04
  Rehash and clear deleted entries in weak collections during GC
  Otherwise, they'll just keep growing until we run out of memory or hit the FixedArray's maximum capacity.

  BUG=v8:4909
  [email protected]
  LOG=n

  Review URL: https://codereview.chromium.org/1877233005

  Cr-Commit-Position: refs/heads/master@{#35514}

v8/v8@09db540
  Reland of Rehash and clear deleted entries in weak collections during GC
  BUG=v8:4909
  [email protected],[email protected]
  LOG=n

  Review URL: https://codereview.chromium.org/1890123002

  Cr-Commit-Position: refs/heads/master@{#35538}

Ref: https://crbug.com/v8/4909
Ref: #7883
Fixes: #6180
PR-URL: #7689
Reviewed-By: Matt Loring <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
MylesBorins pushed a commit that referenced this issue Oct 10, 2016
Original commit message:

If we can't rehash the backing store for weak sets & maps, do a last
resort GC

BUG=v8:4909
[email protected]

Committed: https://crrev.com/b93c80a6039c757723e70420ae73375b5d277814
Cr-Commit-Position: refs/heads/master@{#37591}

Fixes: #6180
Ref: #7883
PR-URL: #7689
Reviewed-By: Matt Loring <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
MylesBorins pushed a commit that referenced this issue Oct 10, 2016
Original commit message:

  Fix incorrect parameter to HasSufficientCapacity

  It takes the number of additional elements, not the total target
  capacity.

  Also, avoid right-shifting a negative integer as this is undefined in
  general

  BUG=v8:4909
  [email protected]

Review-Url: https://codereview.chromium.org/2162333002
Cr-Commit-Position: refs/heads/master@{#37901}

Fixes: #6180
Ref: #7883
PR-URL: #7689
Reviewed-By: Matt Loring <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
rvagg pushed a commit that referenced this issue Oct 18, 2016
Original commit messages:
v8/v8@e093a04
  Rehash and clear deleted entries in weak collections during GC
  Otherwise, they'll just keep growing until we run out of memory or hit the FixedArray's maximum capacity.

  BUG=v8:4909
  [email protected]
  LOG=n

  Review URL: https://codereview.chromium.org/1877233005

  Cr-Commit-Position: refs/heads/master@{#35514}

v8/v8@09db540
  Reland of Rehash and clear deleted entries in weak collections during GC
  BUG=v8:4909
  [email protected],[email protected]
  LOG=n

  Review URL: https://codereview.chromium.org/1890123002

  Cr-Commit-Position: refs/heads/master@{#35538}

Ref: https://crbug.com/v8/4909
Ref: #7883
Fixes: #6180
PR-URL: #7689
Reviewed-By: Matt Loring <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
rvagg pushed a commit that referenced this issue Oct 18, 2016
Original commit message:

If we can't rehash the backing store for weak sets & maps, do a last
resort GC

BUG=v8:4909
[email protected]

Committed: https://crrev.com/b93c80a6039c757723e70420ae73375b5d277814
Cr-Commit-Position: refs/heads/master@{#37591}

Fixes: #6180
Ref: #7883
PR-URL: #7689
Reviewed-By: Matt Loring <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
rvagg pushed a commit that referenced this issue Oct 18, 2016
Original commit message:

  Fix incorrect parameter to HasSufficientCapacity

  It takes the number of additional elements, not the total target
  capacity.

  Also, avoid right-shifting a negative integer as this is undefined in
  general

  BUG=v8:4909
  [email protected]

Review-Url: https://codereview.chromium.org/2162333002
Cr-Commit-Position: refs/heads/master@{#37901}

Fixes: #6180
Ref: #7883
PR-URL: #7689
Reviewed-By: Matt Loring <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
MylesBorins pushed a commit that referenced this issue Oct 26, 2016
Original commit messages:
v8/v8@e093a04
  Rehash and clear deleted entries in weak collections during GC
  Otherwise, they'll just keep growing until we run out of memory or hit the FixedArray's maximum capacity.

  BUG=v8:4909
  [email protected]
  LOG=n

  Review URL: https://codereview.chromium.org/1877233005

  Cr-Commit-Position: refs/heads/master@{#35514}

v8/v8@09db540
  Reland of Rehash and clear deleted entries in weak collections during GC
  BUG=v8:4909
  [email protected],[email protected]
  LOG=n

  Review URL: https://codereview.chromium.org/1890123002

  Cr-Commit-Position: refs/heads/master@{#35538}

Ref: https://crbug.com/v8/4909
Ref: #7883
Fixes: #6180
PR-URL: #7689
Reviewed-By: Matt Loring <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
MylesBorins pushed a commit that referenced this issue Oct 26, 2016
Original commit message:

If we can't rehash the backing store for weak sets & maps, do a last
resort GC

BUG=v8:4909
[email protected]

Committed: https://crrev.com/b93c80a6039c757723e70420ae73375b5d277814
Cr-Commit-Position: refs/heads/master@{#37591}

Fixes: #6180
Ref: #7883
PR-URL: #7689
Reviewed-By: Matt Loring <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
MylesBorins pushed a commit that referenced this issue Oct 26, 2016
Original commit message:

  Fix incorrect parameter to HasSufficientCapacity

  It takes the number of additional elements, not the total target
  capacity.

  Also, avoid right-shifting a negative integer as this is undefined in
  general

  BUG=v8:4909
  [email protected]

Review-Url: https://codereview.chromium.org/2162333002
Cr-Commit-Position: refs/heads/master@{#37901}

Fixes: #6180
Ref: #7883
PR-URL: #7689
Reviewed-By: Matt Loring <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
BethGriggs pushed a commit to ibmruntimes/node that referenced this issue Nov 9, 2016
Original commit messages:
v8/v8@e093a04
  Rehash and clear deleted entries in weak collections during GC
  Otherwise, they'll just keep growing until we run out of memory or hit the FixedArray's maximum capacity.

  BUG=v8:4909
  [email protected]
  LOG=n

  Review URL: https://codereview.chromium.org/1877233005

  Cr-Commit-Position: refs/heads/master@{#35514}

v8/v8@09db540
  Reland of Rehash and clear deleted entries in weak collections during GC
  BUG=v8:4909
  [email protected],[email protected]
  LOG=n

  Review URL: https://codereview.chromium.org/1890123002

  Cr-Commit-Position: refs/heads/master@{#35538}

Ref: https://crbug.com/v8/4909
Ref: nodejs/node#7883
Fixes: nodejs/node#6180
PR-URL: nodejs/node#7689
Reviewed-By: Matt Loring <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
BethGriggs pushed a commit to ibmruntimes/node that referenced this issue Nov 9, 2016
Original commit message:

If we can't rehash the backing store for weak sets & maps, do a last
resort GC

BUG=v8:4909
[email protected]

Committed: https://crrev.com/b93c80a6039c757723e70420ae73375b5d277814
Cr-Commit-Position: refs/heads/master@{#37591}

Fixes: nodejs/node#6180
Ref: nodejs/node#7883
PR-URL: nodejs/node#7689
Reviewed-By: Matt Loring <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
BethGriggs pushed a commit to ibmruntimes/node that referenced this issue Nov 9, 2016
Original commit message:

  Fix incorrect parameter to HasSufficientCapacity

  It takes the number of additional elements, not the total target
  capacity.

  Also, avoid right-shifting a negative integer as this is undefined in
  general

  BUG=v8:4909
  [email protected]

Review-Url: https://codereview.chromium.org/2162333002
Cr-Commit-Position: refs/heads/master@{#37901}

Fixes: nodejs/node#6180
Ref: nodejs/node#7883
PR-URL: nodejs/node#7689
Reviewed-By: Matt Loring <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
bernardmcmanus added a commit to BitClock/bitclock-js that referenced this issue May 28, 2017
bernardmcmanus added a commit to BitClock/bitclock-js that referenced this issue May 28, 2017
abernix added a commit to abernix/node that referenced this issue Aug 14, 2017
Original commit messages:
v8/v8@09db540
  Reland of Rehash and clear deleted entries in weak collections during GC

  BUG=v8:4909
  [email protected],[email protected]
  LOG=n

  Review URL: https://codereview.chromium.org/1890123002

  Cr-Commit-Position: refs/heads/master@{nodejs#35538}

v8/v8@686558d
  Fix comment about when we rehash ObjectHashTables before growing them

  [email protected]
  BUG=

  Review-Url: https://codereview.chromium.org/1918403003
  Cr-Commit-Position: refs/heads/master@{nodejs#35853}

Refs: https://crbug.com/v8/4909
Refs: nodejs#6180
Refs: nodejs#7689
Refs: nodejs#6398
Fixes: nodejs#14228
MylesBorins pushed a commit that referenced this issue Aug 16, 2017
Original commit messages:
v8/v8@09db540
  Reland of Rehash and clear deleted entries in weak collections during GC

  BUG=v8:4909
  [email protected],[email protected]
  LOG=n

  Review URL: https://codereview.chromium.org/1890123002

  Cr-Commit-Position: refs/heads/master@{#35538}

v8/v8@686558d
  Fix comment about when we rehash ObjectHashTables before growing them

  [email protected]
  BUG=

  Review-Url: https://codereview.chromium.org/1918403003
  Cr-Commit-Position: refs/heads/master@{#35853}

Refs: https://crbug.com/v8/4909
Refs: #6180
Refs: #7689
Refs: #6398
Fixes: #14228

PR-URL: #14829
Reviewed-By: Ben Noordhuis <[email protected]>
MylesBorins pushed a commit that referenced this issue Oct 25, 2017
Original commit messages:
v8/v8@09db540
  Reland of Rehash and clear deleted entries in weak collections during GC

  BUG=v8:4909
  [email protected],[email protected]
  LOG=n

  Review URL: https://codereview.chromium.org/1890123002

  Cr-Commit-Position: refs/heads/master@{#35538}

v8/v8@686558d
  Fix comment about when we rehash ObjectHashTables before growing them

  [email protected]
  BUG=

  Review-Url: https://codereview.chromium.org/1918403003
  Cr-Commit-Position: refs/heads/master@{#35853}

Refs: https://crbug.com/v8/4909
Refs: #6180
Refs: #7689
Refs: #6398
Fixes: #14228

PR-URL: #14829
Reviewed-By: Ben Noordhuis <[email protected]>
gibfahn pushed a commit to ibmruntimes/node that referenced this issue Nov 24, 2017
Original commit messages:
v8/v8@09db540
  Reland of Rehash and clear deleted entries in weak collections during GC

  BUG=v8:4909
  [email protected],[email protected]
  LOG=n

  Review URL: https://codereview.chromium.org/1890123002

  Cr-Commit-Position: refs/heads/master@{#35538}

v8/v8@686558d
  Fix comment about when we rehash ObjectHashTables before growing them

  [email protected]
  BUG=

  Review-Url: https://codereview.chromium.org/1918403003
  Cr-Commit-Position: refs/heads/master@{#35853}

Refs: https://crbug.com/v8/4909
Refs: nodejs/node#6180
Refs: nodejs/node#7689
Refs: nodejs/node#6398
Fixes: nodejs/node#14228

PR-URL: nodejs/node#14829
Reviewed-By: Ben Noordhuis <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
memory Issues and PRs related to the memory management or memory footprint. v8 engine Issues and PRs related to the V8 dependency.
Projects
None yet
7 participants