Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Memory Usage Monitor
#1
I swore I had posted this before but I could not a record of it.
So, here is a small utility to monitor the memory usage:

Code: (Select All)
//mem.h memory function library.
#include<windows.h>
#include<stdio.h>
#include<tchar.h>

uint64 MemInUsePercent();
uint64 TotalPhysicalMem ();
uint64 FreePhysicalMem ();
uint64 TotalPagingFile ();
uint64 FreePagingFile ();
uint64 TotalVirtualMem ();
uint64 FreeVirtualMem ();
uint64 FreeExtendedMem ();

static float CalculateCPULoad();
static unsigned long long FileTimeToInt64();
float GetCPULoad();

uint64 MemInUsePercent () {
  MEMORYSTATUSEX statex;
  statex.dwLength = sizeof (statex);
  GlobalMemoryStatusEx (&statex);
  return  statex.dwMemoryLoad;
}

uint64 TotalPhysicalMem () {
  MEMORYSTATUSEX statex;
  statex.dwLength = sizeof (statex);
  GlobalMemoryStatusEx (&statex);
  return statex.ullTotalPhys;
}

uint64 FreePhysicalMem () {
  MEMORYSTATUSEX statex;
  statex.dwLength = sizeof (statex);
  GlobalMemoryStatusEx (&statex);
  return statex.ullAvailPhys;
}

uint64 TotalPagingFile () {
  MEMORYSTATUSEX statex;
  statex.dwLength = sizeof (statex);
  GlobalMemoryStatusEx (&statex);
  return statex.ullTotalPageFile;
}

uint64 FreePagingFile () {
  MEMORYSTATUSEX statex;
  statex.dwLength = sizeof (statex);
  GlobalMemoryStatusEx (&statex);
  return statex.ullAvailPageFile;
}

uint64 TotalVirtualMem () {
  MEMORYSTATUSEX statex;
  statex.dwLength = sizeof (statex);
  GlobalMemoryStatusEx (&statex);
  return statex.ullTotalVirtual;
}

uint64 FreeVirtualMem () {
  MEMORYSTATUSEX statex;
  statex.dwLength = sizeof (statex);
  GlobalMemoryStatusEx (&statex);
  return statex.ullAvailVirtual;
}

uint64 FreeExtendedMem () {
  MEMORYSTATUSEX statex;
  statex.dwLength = sizeof (statex);
  GlobalMemoryStatusEx (&statex);
  return statex.ullAvailExtendedVirtual;
}

static float CalculateCPULoad(unsigned long long idleTicks, unsigned long long totalTicks)
{
    static unsigned long long _previousTotalTicks = 0;
    static unsigned long long _previousIdleTicks = 0;

    unsigned long long totalTicksSinceLastTime = totalTicks - _previousTotalTicks;
    unsigned long long idleTicksSinceLastTime = idleTicks - _previousIdleTicks;

    float ret = 1.0f - ((totalTicksSinceLastTime > 0) ? ((float)idleTicksSinceLastTime) / totalTicksSinceLastTime : 0);

    _previousTotalTicks = totalTicks;
    _previousIdleTicks = idleTicks;
    return ret;
}

static unsigned long long FileTimeToInt64(const FILETIME & ft)
{
    return (((unsigned long long)(ft.dwHighDateTime)) << 32) | ((unsigned long long)ft.dwLowDateTime);
}

float GetCPULoad()
{
    FILETIME idleTime, kernelTime, userTime;
    return GetSystemTimes(&idleTime, &kernelTime, &userTime) ? CalculateCPULoad(FileTimeToInt64(idleTime), FileTimeToInt64(kernelTime) + FileTimeToInt64(userTime)) : -1.0f;
}
Code: (Select All)
Rem Memory Usage Monitor v1.0a QB64 PD 2025.

_Define A-Z As _UNSIGNED _INTEGER64

Declare Library "mem"
  Function MemInUsePercent~&&
  Function TotalPhysicalMem~&&
  Function FreePhysicalMem~&&
  Function TotalPagingFile~&&
  Function FreePagingFile~&&
  Function TotalVirtualMem~&&
  Function FreeVirtualMem~&&
  Function FreeExtendedMem~&&
  Function GetCPULoad#
End Declare

' byte suffix type
Dim Shared SuffixType As Integer

' setup screen
_ScreenMove _Middle
_Title "Memory Usage Monitor"
Width 80, 25

' start display loop
Do
  _Delay 1
  I$ = InKey$
  If Len(I$) Then
      If I$ = Chr$(27) Then Color 7: End
      If I$ = " " Then ' rotate suffix
        SuffixType = SuffixType + 1
        If SuffixType = 4 Then SuffixType = 0
      End If
  End If
  Cls
  Color 14, 0
  Print "Press <space> to cycle suffix, <escape> to quit."
  Print
  Color 15, 0

  ' GetCPULoad = 0 is idle, 1 is fully used.
  '  Multiply by 100 for a percentage
  Print "CPU used:";
  ' truncate 2 decimal places.
  V$ = Str$(Int(GetCPULoad * 10000) / 100)
  If InStr(V$, ".") Then
      V$ = Left$(V$, InStr(V$, ".") + 2)
  End If
  V$ = V$ + "%"
  Print V$
  _Title "Memory Usage Monitor - CPU " + V$

  Print "Memory used:"; Str$(MemInUsePercent); "%"
  Print

  Print "Total Physical Memory: "
  Print "  "; Suffix$(TotalPhysicalMem)
  Print

  Print "Free Physical Memory: "
  Print "  "; Suffix$(FreePhysicalMem)
  Print

  Print "Total Paging File: "
  Print "  "; Suffix$(TotalPagingFile)
  Print

  Print "Free Paging File: "
  Print "  "; Suffix$(FreePagingFile)
  Print

  Print "Total Virtual Memory: "
  Print "  "; Suffix$(TotalVirtualMem)
  Print

  Print "Free Virtual Memory:"
  Print "  "; Suffix$(FreeVirtualMem)
  Print

  ' locate and display statusline
  X = CsrLin
  Y = Pos(0)
  V$ = "Memory Usage Monitor " + Date$ + " " + Time$
  Locate 25, 1, 1
  Color 14
  Print V$;
  Locate X, Y
  Color 15
Loop
Color 7
End

' calculate byte suffix.
Function Suffix$ (Var)
  Dim Var3 As Double
  ' Var - input value
  ' SuffixType - 0 = bytes, 1 = kilo, 2 = mega, 3 = giga
  Rem B  (Byte) = 00x - 0FFx
  Rem KB (Kilobyte) = 1024 B
  Rem MB (Megabyte) = 1024 KB
  Rem GB (Gigabyte) = 1024 MB

  ' check exponent.
  Var3 = Var
  s$ = Str$(Var3)
  If InStr(s$, "D") Then
      Suffix$ = s$
      Exit Function
  End If

  ' check bytes.
  If SuffixType = 0 Then
      Suffix$ = FormatString$(Var)
      Exit Function
  End If

  ' calculate byte suffix.
  TempA = False
  Do
      If Var3 >= 1024 Then
        Var3 = Var3 / 1024
        TempA = TempA + 1
        If TempA = SuffixType Then
            Exit Do
        End If
      Else
        Exit Do
      End If
  Loop

  ' calculate byte string
  Var2$ = FormatString$(Var3)

  ' make byte suffix.
  Var$ = ""
  If TempA > 0 Then
      Select Case TempA
        Case 1
            Var$ = "KB"
        Case 2
            Var$ = "MB"
        Case 3
            Var$ = "GB"
      End Select
  End If

  ' get single precision decimal place.
  If Int(Var3) <> Var3 Then
      x$ = Str$(Var3)
      x = InStr(x$, ".")
      x$ = Mid$(x$, x)
      Select Case SuffixType
        Case 1 ' KB
            x$ = Left$(x$, 2)
        Case 2 ' MB
            x$ = Left$(x$, 3)
        Case 3 ' GB
            x$ = Left$(x$, 4)
      End Select
      Var2$ = Var2$ + x$
  Else
      Var2$ = Var2$ + ".0 "
  End If

  ' append suffix.
  Suffix$ = Var2$ + " " + Var$
End Function

' formats a numeric string.
Function FormatString$ (s)
  x$ = ""
  s$ = Str$(s)
  s$ = LTrim$(s$)
  For l = Len(s$) To 3 Step -3
      x$ = Mid$(s$, l - 2, 3) + "," + x$
  Next
  If l > 0 Then
      x$ = Mid$(s$, 1, l) + "," + x$
  End If
  If Len(s$) < 3 Then
      x$ = s$
  End If
  If Right$(x$, 1) = "," Then
      x$ = Left$(x$, Len(x$) - 1)
  End If
  FormatString$ = x$
End Function


Attached Files
.zip   MEMUSAGE.ZIP (Size: 2.24 KB / Downloads: 11)
Reply
#2
(02-17-2025, 04:25 AM)eoredson Wrote: I swore I had posted this before but I could not a record of it.
So, here is a small utility to monitor the memory usage:
A meal had also built something from fragments from this forum.
It is mainly intended to monitor the drives.
If someone wants it, I attach it below.


.7z   Drive-Info.7z (Size: 15.61 MB / Downloads: 12) Here is the whole program.


   
Reply
#3
The splash screen for this utility looks good.

But the source code requires WinReg.BI and WinReg.BM which I could not find in the zip.

Also cpu.h should be added.. Huh

-ejo
Reply
#4
To get a display like the one Steffan posted can be done in SCREEN 0, but it requires using hardware acceleration to make the lines that close to the letters in each row. I love the look with that technique, especially for apps like appointment books, but it does chew up a lot of CPU in the system.

Hey remember when QB used FRE to measure string memory, free memory, and stack space? Large apps, back then, needed to have all of those tracked.

Anyway, this looks neat, sort of an expansion on the memory component of Task Manager. 

+1

Pete
Reply
#5
(02-18-2025, 04:06 AM)eoredson Wrote: The splash screen for this utility looks good.
But the source code requires WinReg.BI and WinReg.BM which I could not find in the zip.
Also cpu.h should be added.. Huh
-ejo
Oh sorry, unfortunately I forgot to join in, since these files are in my main Ortner in the QB64pe 
And not in the Ortner program.
I changed the 7Z file in my post.

Oh one more, the file introa.bas has to be compiled first because the
[Drive Info4-3-RGB32.BAS and the Drive Info4-3-Ohnehintergrund-RGB32-Ontimer.Bas] expected an introa.exe.

And from 70% occupancy of the HDD, a warning voice output will take place, which may become a bit strange if more than 1 HDD is full.
With CD drives there is never a voice output because they are always full.
Reply
#6
Quote:Rem List drives and info v1.4a PD 12/10/2022 -ejo.
I wrote those drive functions myself! I can't believe my source actually being used by other programmers!

That's a good thing..
Reply
#7
Problem during install: Pops up a dialogbox containing:

Code: (Select All)
instroa.exe - system error.

libvorbisfile-3.dll not found.
Reply
#8
(Yesterday, 01:24 AM)eoredson Wrote: Problem during install: Pops up a dialogbox containing:
Code: (Select All)
instroa.exe - system error.
libvorbisfile-3.dll not found.
I assume that they mean 'introa.exe'.
That may be that the 'WinMm.dll' does not go with their system, this file would move elsewhere and see if its own is better.
I had tried the 7Z archive with me and created the programs from it, had no problems.
With the 'Libvorbisfile-3.dll' I already had my problems, the solution with me was to reinstall QB64PE.
Because the 'libvorbisfile-3.dll' was available with me but still the mistake that the compiler would not find it.
After that, this mistake was gone. However, I do not know what other mistakes this DLL causes.
I currently have QB64PE 4.0.0
Reply




Users browsing this thread: 2 Guest(s)