spacebudz / lucid

Lucid is a library, which allows you to create Cardano transactions and off-chain code for your Plutus contracts in JavaScript, Deno and Node.js.
https://lucid.spacebudz.io
MIT License
336 stars 133 forks source link

Potential External Memory Leak - External Resources not being collected. #222

Open dpbeaumont opened 10 months ago

dpbeaumont commented 10 months ago

I've been doing some testing and I have noticed memory usage proportionally increasing when Lucid is consumed in a long running application. Over time this memory usage compounds, and eventually uses a machines full memory allowance, causing various memory exceptions from here on.

This occurs when used in the context of long running application, or one with looping. I can see memory marked as external consistently increases and does not get garbage collected. I have tested using various looping approaches, while loops, for loops, interval loops & async loops in an attempt to try and force this external resource cleanup.

Replication steps are using the Data.from() method to showcase this. I can see the following: -External memory constantly increasing in each loop. mainly proportionally, until application end. -Heap used, increasing/decreasing (stable as expected). -Rss constantly increasing. -Array buffers increasing/decreasing (stable as expected).

image

When we reach the 100k loop, you can see the external memory in use is 209.05mb which is significant.
image

StackBlitz here demonstrates the code I used to test this and shows minimal replication steps, in a nodejs application: https://stackblitz.com/edit/node-wdg1xb?file=index.js

Heap performance profile of operation: image

yHSJ commented 8 months ago

Hey @dpbeaumont! You're absolutely right, there is a memory leak in Lucid. In fact, there are several. I left a brief explanation in the pull request I opened (#232) to fix this specific issue, and I will continue to open PRs to incrementally resolve memory management issues throughout the library whenever I can.

SebastienGllmt commented 8 months ago

This is already fixed in the CML develop branch, so it will go away once Lucid updates to the latest CML version