WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: KeithW on March 28, 2023, 04:55:38 PM

Title: Getting Memory Used -OR- Free
Post by: KeithW 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
Title: Re: Getting Memory Used -OR- Free
Post by: KeithW 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


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

Title: Re: Getting Memory Used -OR- Free
Post by: stanl 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..
Title: Re: Getting Memory Used -OR- Free
Post by: JTaylor 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) Select


;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

Title: Re: Getting Memory Used -OR- Free
Post by: td on March 29, 2023, 08:08:08 AM
Quote from: KeithW 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

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) Select
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

Title: Re: Getting Memory Used -OR- Free
Post by: stanl on March 29, 2023, 12:43:23 PM
and maybe (freeMem*100/Totmem) gives percentage free. And maybe LoadPercentage from WMI Win32_Processor.
Title: Re: Getting Memory Used -OR- Free
Post by: KeithW 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....
Title: Re: Getting Memory Used -OR- Free
Post by: KeithW 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...
Title: Re: Getting Memory Used -OR- Free
Post by: KeithW 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.



; 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

Title: Re: Getting Memory Used -OR- Free
Post by: stanl on March 30, 2023, 05:15:20 AM
Glad it worked. Now I'm really interested in formatting numerics with commas.
Title: Re: Getting Memory Used -OR- Free
Post by: JTaylor 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
Title: Re: Getting Memory Used -OR- Free
Post by: stanl on April 02, 2023, 05:17:02 AM
Quote from: JTaylor 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/ (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.