buttercubzB
Denoβ€’2y agoβ€’
4 replies
buttercubz

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;
}
Was this page helpful?