Author Topic: How to determine available memory left?  (Read 2755 times)

0 Members and 1 Guest are viewing this topic.

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
How to determine available memory left?
« on: June 12, 2021, 08:36:43 pm »
With Windows 10 x64 - how to determine the available memory left in a QB64 program?

Ideally without say resizing an array until error condition - any API code available?
« Last Edit: June 12, 2021, 08:40:03 pm by Richard »

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Shuwatch!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: How to determine available memory left?
« Reply #2 on: June 12, 2021, 09:10:01 pm »
Code: QB64 $NOPREFIX: [Select]
  1.  
  2. Type MEMORYSTATUSEX
  3.     As Long dwLength, dwMemoryLoad
  4.     As Unsigned Integer64 ullTotalPhys, ullAvailPhys, ullTotalPageFile, ullAvailPageFile, ullTotalVirtual, ullAvailVirtual, ullAvailExtendedVirtual
  5.  
  6. Type FILETIME
  7.     As Long dwLowDateTime, dwHighDateTime
  8.  
  9. Screen NewImage(640, 480, 32)
  10.  
  11. Title "Memory API"
  12.     Limit 1
  13.     Cls
  14.  
  15.     Print Using "CPU used: ###.##%"; Int(GetCPULoad * 10000) / 100
  16.     Print
  17.  
  18.     Print Using "Memory used: ###%"; MemInUsePercent
  19.     Print
  20.  
  21.     Print "Total Physical Memory: "
  22.     Print Using "   ####################, bytes"; TotalPhysicalMem
  23.     Print
  24.  
  25.     Print "Free Physical Memory: "
  26.     Print Using "   ####################, bytes"; FreePhysicalMem
  27.     Print
  28.  
  29.     Print "Total Paging File: "
  30.     Print Using "   ####################, bytes"; TotalPagingFile
  31.     Print
  32.  
  33.     Print "Free Paging File: "
  34.     Print Using "   ####################, bytes"; FreePagingFile
  35.     Print
  36.  
  37.     Print "Total Virtual Memory: "
  38.     Print Using "   ####################, bytes"; TotalVirtualMem
  39.     Print
  40.  
  41.     Print "Free Virtual Memory:"
  42.     Print Using "   ####################, bytes"; FreeVirtualMem
  43.     Print
  44.  
  45.     Print "Free Extended Virtual Memory:"
  46.     Print Using "   ####################, bytes"; FreeExtendedMem
  47.     Display
  48.  
  49.     Sub GlobalMemoryStatus Alias "GlobalMemoryStatusEx" (ByVal lpBuffer As Offset)
  50.     Function GetSystemTimes%% (ByVal lpIdleTime As Offset, Byval lpKernelTime As Offset, Byval lpUserTime As Offset)
  51.  
  52.  
  53. Function MemInUsePercent~& ()
  54.     Dim As MEMORYSTATUSEX statex
  55.     statex.dwLength = Len(statex)
  56.     GlobalMemoryStatus Offset(statex)
  57.     MemInUsePercent = statex.dwMemoryLoad
  58.  
  59. Function TotalPhysicalMem~&& ()
  60.     Dim As MEMORYSTATUSEX statex
  61.     statex.dwLength = Len(statex)
  62.     GlobalMemoryStatus Offset(statex)
  63.     TotalPhysicalMem = statex.ullTotalPhys
  64.  
  65. Function FreePhysicalMem~&& ()
  66.     Dim As MEMORYSTATUSEX statex
  67.     statex.dwLength = Len(statex)
  68.     GlobalMemoryStatus Offset(statex)
  69.     FreePhysicalMem = statex.ullAvailPhys
  70.  
  71. Function TotalPagingFile~&& ()
  72.     Dim As MEMORYSTATUSEX statex
  73.     statex.dwLength = Len(statex)
  74.     GlobalMemoryStatus Offset(statex)
  75.     TotalPagingFile = statex.ullTotalPageFile
  76.  
  77. Function FreePagingFile~&& ()
  78.     Dim As MEMORYSTATUSEX statex
  79.     statex.dwLength = Len(statex)
  80.     GlobalMemoryStatus Offset(statex)
  81.     FreePagingFile = statex.ullAvailPageFile
  82.  
  83. Function TotalVirtualMem~&& ()
  84.     Dim As MEMORYSTATUSEX statex
  85.     statex.dwLength = Len(statex)
  86.     GlobalMemoryStatus Offset(statex)
  87.     TotalVirtualMem = statex.ullTotalVirtual
  88.  
  89. Function FreeVirtualMem~&& ()
  90.     Dim As MEMORYSTATUSEX statex
  91.     statex.dwLength = Len(statex)
  92.     GlobalMemoryStatus Offset(statex)
  93.     FreeVirtualMem = statex.ullAvailVirtual
  94.  
  95. Function FreeExtendedMem~&& ()
  96.     Dim As MEMORYSTATUSEX statex
  97.     statex.dwLength = Len(statex)
  98.     GlobalMemoryStatus Offset(statex)
  99.     FreeExtendedMem = statex.ullAvailExtendedVirtual
  100.  
  101. Function CalculateCPULoad## (idleTicks As Unsigned Integer64, totalTicks As Unsigned Integer64) Static
  102.     Static As Unsigned Integer64 previousTotalTicks, previousIdleTicks
  103.     Dim As Unsigned Integer64 totalTicksSinceLastTime, idleTicksSinceLastTime
  104.     totalTicksSinceLastTime = totalTicks - previousTotalTicks
  105.     idleTicksSinceLastTime = idleTicks - previousIdleTicks
  106.     Dim As Float ret
  107.     ret = 1.0
  108.     If totalTicksSinceLastTime > 0 Then
  109.         If 1.0 - (idleTicksSinceLastTime / totalTicksSinceLastTime) >= 0 Then
  110.             ret = 1.0 - (idleTicksSinceLastTime / totalTicksSinceLastTime)
  111.         End If
  112.     Else ret = 1.0
  113.     End If
  114.     previousTotalTicks = totalTicks
  115.     previousIdleTicks = idleTicks
  116.     CalculateCPULoad = ret
  117.  
  118. Function FileTimeToInt64~&& (ft As FILETIME) Static
  119.     FileTimeToInt64 = SHL(ft.dwHighDateTime, 32) Or ft.dwLowDateTime
  120.  
  121. Function GetCPULoad## ()
  122.     Dim As FILETIME idleTime, kernelTime, userTime
  123.     If GetSystemTimes(Offset(idleTime), Offset(kernelTime), Offset(userTime)) Then
  124.         GetCPULoad = CalculateCPULoad(FileTimeToInt64(idleTime), FileTimeToInt64(kernelTime) + FileTimeToInt64(userTime))
  125.     Else
  126.         GetCPULoad = -1.0
  127.     End If
« Last Edit: June 12, 2021, 09:26:13 pm by SpriggsySpriggs »
Shuwatch!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: How to determine available memory left?
« Reply #3 on: June 12, 2021, 09:55:23 pm »
Is there a limit, if you compile with the 64-bit version of QB64?  I guess if you max out ram + hard drive usage, but that’s either one massive program or one helluva memory leak!
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
Re: How to determine available memory left?
« Reply #4 on: June 12, 2021, 10:12:42 pm »
@SpriggsySpriggs

Just tried to use your code above into my program (Qb64 v1.5 stable release) but the IDE gives error:-

Expected variablename AS type or END TYPE on line 12

LINE#11     Type MEMORYSTATUSEX
LINE #12    As Long dwLength, dwMemoryLoad

(I had extra lines of my code at beginning)

I recalled a similar kind of problem when using your pipecom (but which worked OK I think in a dev build - but because of Compiler error with dev build that you and Fellippe already working on I had to go back to the v1.5 stable build).

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
Re: How to determine available memory left?
« Reply #5 on: June 12, 2021, 11:28:30 pm »
@moises1953

Thanks again for your reply in the link below (I thought I had asked the question before but for a totally different reason!)

https://www.qb64.org/forum/index.php?topic=3750.msg131094#msg131094






@SpriggsySpriggs

Thanks for your reply earlier on - if it is too difficult for you to fix at this stage, please place on hold. I located @moises1953 reply earlier that I had forgotten about. I am confused as to why the "millions" of IDE errors using your code in v1.5 stable build but for instance your pipecom I recall worked with a dev build. 

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: How to determine available memory left?
« Reply #6 on: June 13, 2021, 12:04:58 am »
Oh yeah I'm not supporting any one-off codes at all. They all come as-is
Shuwatch!