viewpoint-particle

Author Topic: Getting Memory Used -OR- Free  (Read 268 times)

KeithW

  • Full Member
  • ***
  • Posts: 174
Getting Memory Used -OR- Free
« on: March 28, 2023, 04:55:38 pm »
Greetings,

I was wondering if anyone has developed code to retrieve the amount of Memory In-Use or Memory Free and would be willing share such?

I know the CPUextender allows you to get Total Memory but not the  Used -vs- UnUsed portions.

I have a couple of programs with Memory Leaks apparently as I have been running out of Physical Memory and having a couple of System Hangs but by the time it happens I have enough stuff running BUT am never exactly sure what exactly as these happen randomly and always catch me off guard.  I would like to get something to warn me when things are getting tight BEFORE my system locks up.

Thanx,
Keith

KeithW

  • Full Member
  • ***
  • Posts: 174
Re: Getting Memory Used -OR- Free
« Reply #1 on: March 28, 2023, 09:26:28 pm »
OK, I am way over my head into areas of Windows I truly have no desire to be in... one of you systems programmers please take some pity on me and tell me where I am going wrong here... thanx

Which generates an Error On Line 7
1258: COM: Unknown name  -   lngTotalMemory = objOperatingSystem.TotalVisibleMemorySize

Code: [Select]
strComputer = "."
objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" : strComputer : "\root\cimv2")
objRefresher  = CreateObject("WbemScripting.Swbemrefresher")
objOperatingSystem = objRefresher.AddEnum(objWMIService, "Win32_OperatingSystem").ObjectSet

; Retrieve the total physical memory and the amount of free physical memory (in KB)
lngTotalMemory = objOperatingSystem.TotalVisibleMemorySize              <=============
Message("TotalMemory ", lngTotalMemory)

lngFreeMemory = objOperatingSystem.FreePhysicalMemory
Message("FreeMemory ", lngFreeMemory)
EXIT

stanl

  • Pundit
  • *****
  • Posts: 1777
Re: Getting Memory Used -OR- Free
« Reply #2 on: March 29, 2023, 06:11:22 am »
Definitely not an answer, but had just posted a thread concerning WMI Win Operating System.  Maybe one of these:
Win32_Processor
Win32_PhysicalMemory


can help enumerate..

JTaylor

  • Pundit
  • *****
  • Posts: 1914
    • Data & Stuff Inc.
Re: Getting Memory Used -OR- Free
« Reply #3 on: March 29, 2023, 06:15:25 am »
This has more than you want but for what it is worth.    Also, as Stan noted, there is some more info you might find useful under this thread.

https://forum.winbatch.com/index.php?topic=2915.0

Jim


Code: Winbatch

;use WMI to query OperatingSystemSKU
strComputer = "."
oWMI = GetObject( "winmgmts:\\" : strComputer : "\root\cimv2")
pro = oWMI.Get("Win32_OperatingSystem")
sku = oWMI.ExecQuery( "Select * from Win32_OperatingSystem" )
ForEach sitem in sku
   txt = ""
Next
IntControl(73,2,0,0,0)

ForEach item in pro.Properties_
  txt = txt:item.Name:"  -  "
  pname = item.Name
  val = sitem.%pname%:""
  txt = txt:val:@CRLF
Next
    clipput(txt)
    Message("List",txt)

Exit
:WBERRORHANDLER
val = "NonText"
IntControl(73,2,0,0,0) ; Re-arm error handler
Return

 

td

  • Tech Support
  • *****
  • Posts: 4293
    • WinBatch
Re: Getting Memory Used -OR- Free
« Reply #4 on: March 29, 2023, 08:08:08 am »
OK, I am way over my head into areas of Windows I truly have no desire to be in... one of you systems programmers please take some pity on me and tell me where I am going wrong here... thanx

Which generates an Error On Line 7
1258: COM: Unknown name  -   lngTotalMemory = objOperatingSystem.TotalVisibleMemorySize

WMI usage can be a bit of a convoluted and not well-documented challenge but a little trial and error helps. This is far from a complete example and may not be the best approach but perhaps it gets you pointed in the right direction.

Code: Winbatch
strComputer = "."
objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" : strComputer : "\root\cimv2")
objRefresher  = CreateObject("WbemScripting.Swbemrefresher")
objOperatingSystem = objRefresher.AddEnum(objWMIService, "Win32_OperatingSystem").ObjectSet

FreeMem = 0
TotMem  = 0
objRefresher.Refresh
ForEach RefreshItem In objRefresher
   ForEach object In RefreshItem.ObjectSet
      FreeMem = object.FreePhysicalMemory
      TotMem  = object.TotalVisibleMemorySize
   next
next

Message("Free Memory Snapshot", FreeMem:" KB free memory":@lf: TotMem:" KB total memory")
exit
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

  • Pundit
  • *****
  • Posts: 1777
Re: Getting Memory Used -OR- Free
« Reply #5 on: March 29, 2023, 12:43:23 pm »
and maybe (freeMem*100/Totmem) gives percentage free. And maybe LoadPercentage from WMI Win32_Processor.

KeithW

  • Full Member
  • ***
  • Posts: 174
Re: Getting Memory Used -OR- Free
« Reply #6 on: March 29, 2023, 04:14:34 pm »
Thanx Guys, you got me off the rails, now if I could only remember how to nicely format numbers greater than a thousand... my mind is one of the things I miss the most....

KeithW

  • Full Member
  • ***
  • Posts: 174
Re: Getting Memory Used -OR- Free
« Reply #7 on: March 29, 2023, 05:05:14 pm »
Thankfully, I finally found my mind and remembered the number formatting had to be done thru a Function Call... searched my code lib... I found it and all is well again, subject to change...

KeithW

  • Full Member
  • ***
  • Posts: 174
Re: Getting Memory Used -OR- Free
« Reply #8 on: March 29, 2023, 05:27:53 pm »

Initial Working Script, more to go but this DOES work... thanx to those that assisted...
BTW, it does Compile cleanly without any other than the default Extender, too.
Using 2021D at the moment.


Code: [Select]
; SCRIPT to find Total & Available Memory on a System
; not the prettiest or best implementation but on my Win10pro System it works just fine.

#DefineFunction AddCommas(passednumstring)
    ; call as:  AddCommas(number_to_format)
    ; Grab everything to the left of the decimal (if any)
    intpart=ItemExtract(1,passednumstring, ".")

    ;Grab everything to the right of the decimal
    fractpart=ItemExtract(2,passednumstring,".")

    ;Compute how many comma seperated groups we need
    groups= ((StrCharCount(intpart)-1) / 3 ) +1

    ;Initialize an answer variable to a null string
    answer=""

    ;Figure out what each group looks like and add each
    ;group to the answer variable with a comma.
    For y = 1 To groups
       If y == 1
          ptr=( (StrCharCount(intpart)-1) mod 3) +1
          answer=StrSub(intpart,1, ptr )
          ptr=ptr +1
       Else
          answer=StrCat(answer, ",", StrSub(intpart, ptr , 3))
          ptr=ptr +3
       EndIf
    Next

    ;Now that we have the numbers to the left of the decimal point
    ;"comma-ized" properly, see if there is any fractional part to
;worry about.

    ;If there is a fractional part, glue it onto the end of the
    ;"comma-ized" number along with a decimal point.
    If fractpart != ""
       answer=StrCat(answer,".",fractpart)
    EndIf

    ;Return the comma-ized answer
    Return (answer)
#EndFunction

;---

strComputer = "."
objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" : strComputer : "\root\cimv2")
objRefresher  = CreateObject("WbemScripting.Swbemrefresher")
objOperatingSystem = objRefresher.AddEnum(objWMIService, "Win32_OperatingSystem").ObjectSet

FreeMem = 0
TotMem  = 0
FreePercent = 0

objRefresher.Refresh
ForEach RefreshItem In objRefresher
   ForEach object In RefreshItem.ObjectSet
      FreeMem = object.FreePhysicalMemory
      TotMem  = object.TotalVisibleMemorySize

   next
next
FreePercent = (Freemem * 100/TotMem)

Message("Free Memory Snapshot", StrFixCharsL(AddCommas(TotMem), " ",12):" KB Total memory":@lf: StrFixCharsL(AddCommas(FreeMem)," ",12):" KB Free memory":@lf: StrFixCharsL(FreePercent," ",12):"%% Available")

Exit

stanl

  • Pundit
  • *****
  • Posts: 1777
Re: Getting Memory Used -OR- Free
« Reply #9 on: March 30, 2023, 05:15:20 am »
Glad it worked. Now I'm really interested in formatting numerics with commas.

JTaylor

  • Pundit
  • *****
  • Posts: 1914
    • Data & Stuff Inc.
Re: Getting Memory Used -OR- Free
« Reply #10 on: March 30, 2023, 06:30:24 am »
For a Blast from the past for something that might be useful...

Look under Strings menu for udfStrNumThousands.


http://winbatch.hpdd.de/

Also, if you happen to be using my wbOmnibus Extender there is a snFormatNumber() function in it.

Jim

stanl

  • Pundit
  • *****
  • Posts: 1777
Re: Getting Memory Used -OR- Free
« Reply #11 on: April 02, 2023, 05:17:02 am »
For a Blast from the past for something that might be useful...
Look under Strings menu for udfStrNumThousands.
http://winbatch.hpdd.de/
Also, if you happen to be using my wbOmnibus Extender there is a snFormatNumber() function in it.
Jim


Thanks Jim. We often forget Detlev's functions were a gold-mine. I want to go the CLR route and use ToString(#format#), for example


value = 128586256.9
newvalue = value.ToString("###,###,###.00")  => 128,586,256.90


or


value = 4000

newvalue = value.ToString("###,###,###")  => 4,000


Haven't got the assembly figured out yet. Perhaps Tony has a WB numberFormat() similar to the timeformat() which is excellent for datetimes.