Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Used Memory #35

Open
DrunkenAlcoholic opened this issue Feb 7, 2023 · 0 comments
Open

Used Memory #35

DrunkenAlcoholic opened this issue Feb 7, 2023 · 0 comments

Comments

@DrunkenAlcoholic
Copy link

DrunkenAlcoholic commented Feb 7, 2023

Hi,
Just want to say you have an excellent job on nitch, But as you probably already know, there are different versions on how used memory should be displayed, I did notice on nitch that it takes TotalMem - AvailableMem to get UsedMem. I just wanted to make you aware of the formula that htop uses and which has derived from a write up on Kernel.org
https://www.kernel.org/doc/Documentation/filesystems/proc.txt

basically htop originally used MemUsed = MemTotal - MemFree - Cached - Buffers - SReclaimable + Shmem

Then some patches made it in the kernel changed the way SharedMem and Cached are calculated, I haven't personally read the patch notes https://lore.kernel.org/lkml/[email protected]/
but it basically comes down to Shmem being part of cache so the accepted formula and the one htop uses is
MemUsed = MemTotal - MemFree - Cached - Buffers - SReclaimable

I have implemented this is my fetch program written Pascal

// Get Memory and usage
function GetRamUsage(): string;
var
  strTmp, strMemTotal, strMemFree, strShmem, strBuffers, strCached,
  strSRclaimable: string;
  intMemUsage, intTotalMem: single;
  slMemInfo: TStringList;
begin
  Result := 'Error';
  // Set the name of the file that will be read
  slMemInfo := TStringList.Create;
  try
    slMemInfo.LoadFromFile('/proc/meminfo');
    try
      // Process MemTotal
      strTmp := slMemInfo.Strings[0];
      strMemTotal := Trim(ExtractString(strTmp, 'MemTotal:', 'kB'));

      // Process MemFree
      strTmp := slMemInfo.Strings[1];
      strMemFree := Trim(ExtractString(strTmp, 'MemFree:', 'kB'));

      // Process Buffers
      strTmp := slMemInfo.Strings[3];
      strBuffers := Trim(ExtractString(strTmp, 'Buffers:', 'kB'));

      // Process Cached
      strTmp := slMemInfo.Strings[4];
      strCached := Trim(ExtractString(strTmp, 'Cached:', 'kB'));

      // Process Shmem
      strTmp := slMemInfo.Strings[20];
      strShmem := Trim(ExtractString(strTmp, 'Shmem:', 'kB'));

      // Process SReclaimable
      strTmp := slMemInfo.Strings[25];
      strSRclaimable := Trim(ExtractString(strTmp, 'SReclaimable:', 'kB'));

      // Calc Mem Total
      intTotalMem := StrToFloat(strMemTotal);

      // Calc Mem Usage
      intMemUsage := (intTotalMem - StrToFloat(strMemFree) - StrToFloat(strBuffers) - 
        StrToFloat(strCached) - StrToFloat(strSRclaimable));
    finally
      slMemInfo.Free;
    end;

  except
    on E: EInOutError do
      writeln('File handling error occurred. Details: ', E.Message);
  end;

  Result := Format('%.2fGB / %.2fGB', [intMemUsage / (1024 * 1024), intTotalMem / (1024 * 1024)]);
end;

I am not saying you should change your formula, just making you aware of the accepted formula used at htop, everyone seems to have a different opinion on this particular topic, and it goes into a lot more discussion at kernel.org and htop github, hope this helps, if not disregard and deleted the topic.

fyi,
programs like btop uses the same formula as nitch, but they are aware of htop and the kernel.org discription
aristocratos/btop#161

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant