Use of javascript library of custom utility functions with more number of VUs results in abnormal increase in memory usage. How to optimize memory usage with increasing VUs and scenarios?

I have a custom library (javascript) of functions created to be used for sending http requests and processing the response, creating scenarios. My javascript library size is 3.5 MB and when it is used with multiple VUs, memory usage increases and OOMKilled. How can I optimize the memory usage when the number of VUs and number of scenarios are increased without increasing in huge memory usage with the custom javascript library of 3.5MB being used. In my case, with 200 VUs initialized, the memory consumption is around 2.5GB and with 18 scenarios, 200 VUs, 30sec duration 2 steps and 1 second think time, the memory usage shoots up to 6.5GB.

When I run the same configuration of tests without using my custom javascript library functions, the memory usage is only around 300MB.

Hi @nagavelu :wave:

Welcome on the support forum :tada:

The issue you’re encountering is due to K6’s design, and unfortunately, we are not able to do much to address this specific occurrence at the moment. Each VU is a dedicated Javascript runtime, with its own copy of the dependencies/modules/resources used in the script’s execution.

Thus, as you have discovered, if you import a pretty big JS module, it gets loaded in the process memory many times, leading to high memory consumption. The only solution to that issue would be to reduce its size somehow.

Which leads me to question: why is the module you’re loading 3.5M? It sounds like a lot for Javascript code, do you happen to load files or static resources as part of it?

The reason I ask is that we might be able to optimize and move around some of the memory usage using other k6-specific techniques if that’s the case.

Cheers :bowing_man: