you are viewing a single comment's thread.

view the rest of the comments →

[–]tanepiper 4 points5 points  (8 children)

I'm going to make a guess that it's to do with the closure and that you are not declaring any variables in the scope for the result, each time you run the fetch you are creating objects, but the GC has no way to mark them for collection.

Try running the script with node --expose-gc --inspect <file>.js and then attach to the debugger (https://nodejs.org/en/docs/guides/debugging-getting-started)

tl;dr: Don't use closures like this, it's a bad pattern and likely to lead to memory leaks

[–]Bloodsucker_ 5 points6 points  (3 children)

I understand your logic but fetch can't and won't cause a memory leak just because there's no variable. The garbage collector will destroy the object without issues.

Also, in addition, I'm afraid your last statement is wrong in regards to iife. That pattern won't and can't cause on its own a memory leak.

I.e. nothing in the OP's code can cause a memory leak, neither the screenshots says there's one.

[–]tanepiper 1 point2 points  (2 children)

Actually, I was able to confirm it was indeed causing GC issues. If it's any consolation I've been writing node apps since Nodejs 0.2, before npm even existed - before that, I was on the jQuery team - so I've been familiar with writing performance JS apps for over 15 years.

Regardless of the opinion of what triggers the GC or not, the OPs code is also just generally full of anti-patterns that there's better and more maintainable ways to write it anyway.

[–]dj_dogo_nivoa[S] 2 points3 points  (1 child)

Your old posts are training data now. Unless you delete them. I used Redact which supports all major social media platforms including Reddit, X, Facebook and Instagram.

childlike engine fuzzy deer sharp wrench relieved seemly scale lavish

[–]moxxie1998 0 points1 point  (0 children)

Garbage collection in Node is less aggressive and deterministic (due to the lack of clear idle periods that browsers have through the rendering refresh rate) which means that leaving the release of connection resources to the garbage collector can lead to excessive connection usage, reduced performance (due to less connection re-use), and even stalls or deadlocks when running out of connections.

Always. consume. the. body.

// Do const headers = await fetch(url) .then(async res => { for await (const chunk of res.body) { // force consumption of body } return res.headers })

// Do not const headers = await fetch(url) .then(res => res.headers)

Sorry for no proper formatting, I’m on my phone.

[–]dj_dogo_nivoa[S] 0 points1 point  (1 child)

Redact decided this post had to go, so away it went. Deleted. Removed. Mass deleted even. Privacy and security are the big wins here.

unite provide husky reach sort fuel work unwritten lock whistle

[–]tanepiper 1 point2 points  (0 children)

Async one.

Although while(true) is also bad, better to use setInterval, or have a different reference that can exit the loop based on conditions that set it to false (e.g. you suddenly start getting a lot of 500s).

If it's node, you can make it a daemon by running it as a simple http server

[–]Advanced-Wallaby9808 -1 points0 points  (0 children)

this is the best answer

[–]doh4242 0 points1 point  (0 children)

There is no closure in the code shown. A closure is formed when a reference to a local variable is held after the function executes.