buttercubz
buttercubz
DDeno
Created by buttercubz on 7/20/2024 in #help
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;
}
5 replies