buttercubz
buttercubz5mo ago

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?
export async function getBatteryInfo(): Promise<BatteryInfo> {
const command = `
& {
$battery = Get-WmiObject Win32_Battery
if ($battery) {
$output = @{
isCharging = $battery.BatteryStatus -eq 2
chargeLevel = $battery.EstimatedChargeRemaining
timeRemaining = $battery.EstimatedRunTime
}
$output | ConvertTo-Json
} else {
Write-Output "null"
}
}`;

const abort = new AbortController();

const cmd = new Deno.Command("powershell", {
args: [
"-Command",
command,
],
signal: abort.signal,
});

const { code, stdout, stderr } = await cmd.output();

if (code) {
const errorString = decoder.decode(stderr);
throw new Error(`Failed to execute PowerShell command: ${errorString}`);
}

const output = decoder.decode(stdout);
const stats: BatteryInfo = parse(output);

abort.abort();

return stats;
}
export async function getBatteryInfo(): Promise<BatteryInfo> {
const command = `
& {
$battery = Get-WmiObject Win32_Battery
if ($battery) {
$output = @{
isCharging = $battery.BatteryStatus -eq 2
chargeLevel = $battery.EstimatedChargeRemaining
timeRemaining = $battery.EstimatedRunTime
}
$output | ConvertTo-Json
} else {
Write-Output "null"
}
}`;

const abort = new AbortController();

const cmd = new Deno.Command("powershell", {
args: [
"-Command",
command,
],
signal: abort.signal,
});

const { code, stdout, stderr } = await cmd.output();

if (code) {
const errorString = decoder.decode(stderr);
throw new Error(`Failed to execute PowerShell command: ${errorString}`);
}

const output = decoder.decode(stdout);
const stats: BatteryInfo = parse(output);

abort.abort();

return stats;
}
4 Replies
Deno AI Helper
Deno AI Helper5mo ago
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:
const { code, stdout, stderr } = await cmd.output();

// After you're done with the command, close it to free resources
cmd.close();

if (code) {
const errorString = decoder.decode(stderr);
throw new Error(`Failed to execute PowerShell command: ${errorString}`);
}

const output = decoder.decode(stdout);
const stats: BatteryInfo = parse(output);

return stats;
const { code, stdout, stderr } = await cmd.output();

// After you're done with the command, close it to free resources
cmd.close();

if (code) {
const errorString = decoder.decode(stderr);
throw new Error(`Failed to execute PowerShell command: ${errorString}`);
}

const output = decoder.decode(stdout);
const stats: BatteryInfo = parse(output);

return stats;
Remember to ensure that decoder is defined
Deno AI Helper
Deno AI Helper5mo ago
and 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.
marvinh.
marvinh.5mo ago
@!Buttercubz that sounds like a bug. Can you open an issue here https://github.com/denoland/deno/issues/new/choose ?
buttercubz
buttercubzOP5mo ago
sure