Author Topic: Memory in Use (API or something) QB64 code  (Read 2711 times)

0 Members and 1 Guest are viewing this topic.

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
Memory in Use (API or something) QB64 code
« on: March 20, 2021, 08:41:29 pm »
Just wondering if there is a Windows API (or something else) Qb64 code that could log output to a simple .txt file on the

Total memory in use and by application

Currently I can view the info with Windows Task Manager but would prefer to log the info to a text file. I have a Task Scheduler set up to 10 minute intervals to assist with debugging  10 minutes and earlier before my computer "crashes".

I have a regular problem of my computer, when running continuously for say 24 hours, that all of sudden, without warning, the display goes BLACK and nothing seems to bring the display back (eg Fn change display, Alt Ctrl Del, Esc, etc).  I am guessing that some app (Windows or 3rd party) is the culprit AND I think it MAY be a memory leak - I do not think that my QB64 programs are at fault as I have monitored them closely often. It is possible for me to arrange an auto-screenshot in my 10minute interval Task Scheduled program but I would rather simply have the numbers in a LOG.txt file of mine.

It is very hard to constantly watch Task Monitor (after a few hours tend to forget to watch - and then after about a day the computer display goes BLACK). The status LEDs for the disk drive does show activity but I have to manually POWER OFF and reset.

Offline moises1953

  • Newbie
  • Posts: 55
    • View Profile
Re: Memory in Use (API or something) QB64 code
« Reply #1 on: March 21, 2021, 01:59:48 pm »
Here is the Windows API for global memory status:
https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-globalmemorystatusex

And here the QB64 code:
Code: QB64: [Select]
  1. 'MemStat
  2. TYPE MEMORYSTATUSEX
  3.   dwLength AS LONG
  4.   dwMemoryLoad AS LONG
  5.   ullTotalPhys AS _UNSIGNED _INTEGER64
  6.   ullAvailPhys AS _UNSIGNED _INTEGER64
  7.   ullTotalPageFile AS _UNSIGNED _INTEGER64
  8.   ullAvailPageFile AS _UNSIGNED _INTEGER64
  9.   ullTotalVirtual AS _UNSIGNED _INTEGER64
  10.   ullAvailVirtual AS _UNSIGNED _INTEGER64
  11.   ullAvailExtendedVirtual AS _UNSIGNED _INTEGER64
  12.  
  13. DECLARE CUSTOMTYPE LIBRARY 'Memory Information using KERNEL32
  14.   FUNCTION GlobalMemoryStatusEx& (Mstat AS MEMORYSTATUSEX)
  15.  
  16. DIM MemStat AS MEMORYSTATUSEX
  17. DIM Result AS LONG
  18.  
  19. MemStat.dwLength = LEN(MemStat)
  20. Result = GlobalMemoryStatusEx&(MemStat)
  21.  
  22. 'PRINT MemStat.dwLength
  23. PRINT "% Memory in use......:"; MemStat.dwMemoryLoad
  24. PRINT "Total physical memory:"; USING "###,###,###,###"; MemStat.ullTotalPhys
  25. PRINT "Free physical memory.:"; USING "###,###,###,###"; MemStat.ullAvailPhys
  26. 'PRINT MemStat.ullTotalPageFile
  27. 'PRINT MemStat.ullAvailPageFile
  28. 'PRINT MemStat.ullTotalVirtual
  29. 'PRINT MemStat.ullAvailVirtual
  30. 'PRINT MemStat.ullAvailExtendedVirtual
  31.  
  32.  

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Memory in Use (API or something) QB64 code
« Reply #2 on: March 21, 2021, 02:23:48 pm »
In the old days, QBasic had the keyword FRE. It would track memory, including string memory, depending on how it was used. PRINT FRE(0), PRINT FRE(-1), etc. It's been too long for me to recall all the particulars, but I did use it on a couple of occasions, to warn me when my apps were getting too big to run in QB!

Nice to see an API example. Thanks for that. Just bringing this FRE stuff up for posterity.

Pete

- I left my hair in the Good 'Ol Days.
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
Re: Memory in Use (API or something) QB64 code
« Reply #3 on: March 21, 2021, 02:27:40 pm »
@moises1953

Thanks for reply.

I will integrate similar into my 10minute Task Scheduler program and save log to non-volatile drive.

One culprit I "caught" a while ago was a third party app that on some runs consumed about 2 Gbyte memory (usually say 100 mB) and after say an hour my computer "crashed".

So, for instance, when ever another program launches, the task scheduler program can warn me that say an extra Gbyte of memory is in use