Memory leak
I have this function to extract the information on the battery status of Windows using Power Shell, but I could notice that while periodically executing the function the ram memory usage increased, so it is possible that I am doing something wrong and causing this or it may be a bug in deno?
4 Replies
I wasn't able to find the answer to your question in the official Deno documentation. However, I can provide some general advice based on the code snippet you've shared. Please note that this advice is not based on the Deno documentation, and you should double-check the accuracy of the advice given.
Looking at your code, I see that you are creating a new
AbortController
and calling abort.abort()
after executing the PowerShell command. This is unnecessary because the AbortController
is used to cancel a request before it completes, and you are calling abort()
after the command has already finished. This will not contribute to a memory leak, but it is redundant.
Additionally, you are using a Deno.Command
to execute the PowerShell script. Make sure that you are properly handling the lifecycle of the resources used by Deno.Command
. In Deno, you should close resources like files and processes when you're done with them. If you're creating new commands in a loop or periodically without closing them, this could lead to a memory leak.
In your code, you are not explicitly closing the process resource. After you have finished with the command, you should close it by calling cmd.close()
. This is important to ensure that Deno can clean up any resources associated with the command.
Here's the part where you should add the cmd.close()
call:
Remember to ensure that decoder
is definedand that
parse
is a function that can correctly handle the JSON output from PowerShell.
Please verify this advice and test your code thoroughly to ensure that it resolves the memory leak issue. If the problem persists, it may be beneficial to look into more detailed profiling of your Deno application to identify the source of the memory leak.@!Buttercubz that sounds like a bug. Can you open an issue here https://github.com/denoland/deno/issues/new/choose ?
sure