Page 1 of 2 12>
Topic Options
#133827 - 2005-02-15 07:56 PM Detect amount of RAM / memory
M_Kiewitz Offline
Fresh Scripter

Registered: 2005-02-15
Posts: 15
Here is a script I just wrote some hours ago.
It detects the total amount of RAM within a computer anyway it's "kinda" faulty and detects 256MB sometimes as being 248MB (note: this is NOT a script bug but a windows bug)

FUNCTION GETCOMPUTERMEMORY ()
$BaseKey = "HKEY_LOCAL_MACHINE\Hardware\ResourceMap\System Resources\Physical Memory"
$RAMTranslated = RIGHT(READVALUE($BaseKey, ".Translated"), 8)
IF @ERROR=0
$RAMAmount = (VAL("&"+RIGHT($RAMTranslated,2)+SUBSTR($RAMTranslated,5,2))+250+16)/16
IF ($RAMAmount mod 2)=1
$RAMAmount = $RAMAmount+1
ENDIF
$GETCOMPUTERMEMORY = ""+$RAMAmount+" MB"
ENDIF
ENDFUNCTION

UPDATE: Code got fixed, should now work even till some TBs.

Regards
M. Kiewitz



Edited by M_Kiewitz (2005-02-16 06:28 PM)

Top
#133828 - 2005-02-15 08:27 PM Re: Detect amount of RAM / memory
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11628
Loc: CA
Well, will one of these UDFs work for you any better?

Memory() - Get Physical RAM size - local or remote

MemSize() - returns installed memory size

MemSize2() - Retrieve Physical memory size

And I've placed your code into TAGS to preserved the formatting.

Code:
FUNCTION GETCOMPUTERMEMORY ()
Dim $BaseKey,$RAMTranslated,$RAMAmount
$BaseKey = "HKEY_LOCAL_MACHINE\Hardware\ResourceMap\System Resources\Physical Memory"
$RAMTranslated = RIGHT(READVALUE($BaseKey, ".Translated"), 8)
IF @ERROR=0
$RAMAmount = (VAL("&"+RIGHT($RAMTranslated,2)+SUBSTR($RAMTranslated,5,2)+SUBSTR($RAMTranslated,3,2)+LEFT($RAMTranslated,2))+16371712+1048576)/1048576
IF ($RAMAmount mod 2)=1
$RAMAmount = $RAMAmount+1
ENDIF
$GETCOMPUTERMEMORY = ""+$RAMAmount+" MB"
ENDIF
ENDFUNCTION



Top
#133829 - 2005-02-15 08:49 PM Re: Detect amount of RAM / memory
M_Kiewitz Offline
Fresh Scripter

Registered: 2005-02-15
Posts: 15
Quote:

Well, will one of these UDFs work for you any better?





No.
The first one uses the same technique, but contains huge amounts of bloated code.
The second one uses COM/OLE, which I don't use cause of compatibility reasons
The third one is badly coded. If the READVALUE operation doesn't succeed it bombs out instead of checking @ERROR.

All of them reply the same bad memory amount on some machines cause the bug is windows.


Edited by M_Kiewitz (2005-02-15 08:50 PM)

Top
#133830 - 2005-02-15 09:07 PM Re: Detect amount of RAM / memory
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11628
Loc: CA
No problem. Can you please provide either a Microsoft KB or other URL where there is documented proof of this error you speak of.

On another note, for UDF code please enable the code to support SETOPTION NoVarsInStrings On and Dim vars within the code. This enables your code to work for Admins that use NoVarsInStrings and those who do not. But without it your code will not function for those that do set it.

NOTE: Your current code does appear to support the NoVarsInStrings option, just thought I would mention it as you are a new member here and may not know.


Edited by NTDOC (2005-02-15 09:12 PM)

Top
#133831 - 2005-02-15 10:28 PM Re: Detect amount of RAM / memory
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
M_Kiewitz, would you mind posting the value of .Translated from the machine that is not working right?

As a side note, I did quite a bit of testing with the original code of the Memory() udf, and one of the things the other members found was that you had use Doubles in your math, or you would run into calculation problems once you passed 2 GBs of RAM. The last I heard, the Memory UDF was fixed, and no longer had this limititation.

Top
#133832 - 2005-02-15 10:53 PM Re: Detect amount of RAM / memory
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
and by the way... your code is much cleaner... I'm just sitting here trying to figure out the differences.
Top
#133833 - 2005-02-15 11:08 PM Re: Detect amount of RAM / memory
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
WOW... I can't believe how much easier this code is... the amazing thing is, both this and the MEMORY UDF use the same .Translated Value, but pull the data together from completely different places within it.

Anyway, might you try this and see if you get a better result for 256MB. Any takers to try it on the GB ranges?


Code:
 FUNCTION GETCOMPUTERMEMORY ()

Dim $BaseKey,$RAMTranslated,$RAMAmount
$BaseKey = "HKEY_LOCAL_MACHINE\Hardware\ResourceMap\System Resources\Physical Memory"
$RAMTranslated = RIGHT(READVALUE($BaseKey, ".Translated"), 8)
IF @ERROR=0
$RAMAmount = cdbl((VAL("&"+RIGHT($RAMTranslated,2)+SUBSTR($RAMTranslated,5,2)+SUBSTR($RAMTranslated,3,2)+LEFT($RAMTranslated,2))+16371712.0+1048576.0))/1048576.0
IF ($RAMAmount mod 2)=1
$RAMAmount = $RAMAmount+1.0
ENDIF
$GETCOMPUTERMEMORY = ""+round($RAMAmount)+" MB"
ENDIF
ENDFUNCTION



Top
#133834 - 2005-02-16 01:45 AM Re: Detect amount of RAM / memory
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11628
Loc: CA
A Server 2003 with 2GB of physical memory shows
-2032 MB

A Server 2003 with 3GB of physical memory shows
-1008 MB

Top
#133835 - 2005-02-16 02:10 AM Re: Detect amount of RAM / memory
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
W2K Server with 4gig reports as:
-256 MB
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#133836 - 2005-02-16 02:21 AM Re: Detect amount of RAM / memory
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11628
Loc: CA
Further testing.

Windows Server 2003 w/2GB RAM
Memory by KiXtart macro: 2047
Memory GETCOMPUTERMEMORY UDF by M_Kiewitz: -2032 MB
Memory GETCOMPUTERMEMORY UDF by M_Kiewitz moded by Al_Po: -2032 MB
Memory UDF by Glenn: 2048
Memory MemSize UDF by Lonkero: 2146947072
Memory MemSize2 UDF by Lonkero:

Windows Server 2003 w/3GB RAM
Memory by KiXtart macro: 2047
Memory GETCOMPUTERMEMORY UDF by M_Kiewitz: -1008 MB
Memory GETCOMPUTERMEMORY UDF by M_Kiewitz moded by Al_Po: -1008 MB
Memory UDF by Glenn: 3072
Memory MemSize UDF by Lonkero: 3220721664
Memory MemSize2 UDF by Lonkero:


Windows XP w/SP2 2GB RAM
Memory by KiXtart macro: 2047
Memory GETCOMPUTERMEMORY UDF by M_Kiewitz: -2047 MB
Memory GETCOMPUTERMEMORY UDF by M_Kiewitz moded by Al_Po: -2047 MB
Memory UDF by Glenn: 2048
Memory MemSize UDF by Lonkero: 2146938880
Memory MemSize2 UDF by Lonkero:


Test results show that bloat or no bloat Glenn and Lonkero's first UDF do appear to work on 2 and 3GB systems.

Not sure what is wrong with the code for Lonkero's 2nd UDF though it did not return results on any system.

Top
#133837 - 2005-02-16 03:31 AM Re: Detect amount of RAM / memory
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
Not that I'm asking for any "Glory" here, but I did have a hand in the Memory UDF that Glenn posted.
Memory() Testing

Anyway, I still think its interesting this code works in some instances using totally different segments of the .Translated value. But since it "appears" it has it limits, I'll leave it up to M Kiewitz to figure that out.


Top
#133838 - 2005-02-16 04:40 AM Re: Detect amount of RAM / memory
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11628
Loc: CA
Sorry Al.

I think there is a very trimmed down version of the one from you and glenn around somewhere. IIRC Richard posted some smaller method to do a portion of it.

I'm sure that it could be golfed down in size and it currently already appears to work correctly on 2GB or higher systems.

Top
#133839 - 2005-02-16 05:07 AM Re: Detect amount of RAM / memory
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
oh yeah... I was aware the Memory UDF could handle memory 2gb and above, I was referring to the code above and its limits.
Top
#133840 - 2005-02-16 09:36 AM Re: Detect amount of RAM / memory
M_Kiewitz Offline
Fresh Scripter

Registered: 2005-02-15
Posts: 15
Quote:

No problem. Can you please provide either a Microsoft KB or other URL where there is documented proof of this error you speak of.





If there would be a KB, there would be a fix.
I just got "some" computers in our network and some of them have 512MB RAM. Strangely some report 512MB and some of them report e.g. 504/503MB (!). I'm collecting that info via login-script currently and adjust our database accordingly. I'm now using a "round-up" algo that will fit badly reported RAM into the right amount, but that's REXX.

BTW. In fact actually they report 511MB and my code fixes those into 512MB. Also some are reported as BEING 512MB (without fixing them). Very strange.

[QUOTE]
NOTE: Your current code does appear to support the NoVarsInStrings option, just thought I would mention it as you are a new member here and may not know.




I'm using NoVarsInStrings myself. Doing without this option produces unreadable code (at least imho). I don't use DIMs anywhere though.

Also about that >2GB problem. I already know some (non bloated) solution. Will include that and post the new code then. Thanx for testing.


Edited by M_Kiewitz (2005-02-16 09:53 AM)

Top
#133841 - 2005-02-16 06:30 PM Re: Detect amount of RAM / memory
M_Kiewitz Offline
Fresh Scripter

Registered: 2005-02-15
Posts: 15
Quote:

Further testing.





Should work now till some Terabytes or even further.
Still not bloated

Top
#133842 - 2005-02-16 07:14 PM Re: Detect amount of RAM / memory
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11628
Loc: CA
So where is the new code that I can test guy?
Top
#133843 - 2005-02-23 04:32 PM Re: Detect amount of RAM / memory
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
Looks like he updated the topmost post with the new code. For readablity I've included it in this post using the code tags:

Code:
 
FUNCTION GETCOMPUTERMEMORY ()
$BaseKey = "HKEY_LOCAL_MACHINE\Hardware\ResourceMap\System Resources\Physical Memory"
$RAMTranslated = RIGHT(READVALUE($BaseKey, ".Translated"), 8)
IF @ERROR=0
$RAMAmount = (VAL("&"+RIGHT($RAMTranslated,2)+SUBSTR($RAMTranslated,5,2))+250+16)/16
IF ($RAMAmount mod 2)=1
$RAMAmount = $RAMAmount+1
ENDIF
$GETCOMPUTERMEMORY = ""+$RAMAmount+" MB"
ENDIF
ENDFUNCTION



I tested it with 512 MB with no problems... Doc you want to give it the GB test runs?

Top
#133844 - 2005-02-23 04:51 PM Re: Detect amount of RAM / memory
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
M Kiewitz: I hacked on your function, dimming the variables, removing the MB so that it just returns a number, and adding the option for checking remote pcs. Once everyone has tested this, you might consider adding your function to the UDF forum.

Code:
 

FUNCTION GETCOMPUTERMEMORY (optional $target)
Dim $Basekey,$RAMTranslated
If $Target <> ''
$Target = IIf(Left($Target, 2) <> '\\', '\\' + $Target, $Target)
$Target = IIf(Right($Target, 1) <> '\', $Target + '\', $Target)
EndIf
$BaseKey =$target + "HKEY_LOCAL_MACHINE\Hardware\ResourceMap\System Resources\Physical Memory"
$RAMTranslated = RIGHT(READVALUE($BaseKey, ".Translated"), 8)
IF @ERROR=0
$GetComputerMemory = (VAL("&"+RIGHT($RAMTranslated,2)+SUBSTR($RAMTranslated,5,2))+250+16)/16
IF ($GetComputerMemory mod 2)=1
$GetComputerMemory = $GetComputerMemory+1
ENDIF
ENDIF
ENDFUNCTION


Top
#133845 - 2005-02-23 09:12 PM Re: Detect amount of RAM / memory
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11628
Loc: CA
Al and M Kiewitz,

I've made some other minor modifications to the code as well and will try them out on 3GB system in a few minutes. Here at home so have to login to work to test.

Function GetComputerMemory(optional $sComputer)
Dim $Basekey,$RAMTranslated
$BaseKey = 'HKLM\Hardware\ResourceMap\System Resources\Physical Memory'
If Not $sComputer $sComputer = @WKSTA
Else
$BaseKey = '\\' + $sComputer + '\' + $BaseKey
EndIf
$RAMTranslated = Right(ReadValue($BaseKey, '.Translated'), 8)
If Not @ERROR
$GetComputerMemory = (Val('&amp;'+Right($RAMTranslated,2)+SubStr($RAMTranslated,5,2))+250+16)/16
If ($GetComputerMemory Mod 2)=1
$GetComputerMemory = $GetComputerMemory+1
EndIf
Else $GetComputerMemory=@ERROR Exit $GetComputerMemory
EndIf
EndFunction

Top
#133846 - 2005-02-23 09:59 PM Re: Detect amount of RAM / memory
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11628
Loc: CA
Al, seems the code you updated still has some issues.
Here are some test results. I've not looked at the difference between what MK and You are doing, but Glenn's code seems to be correct on each system.

1GB Server 2003 running on ESX 2.1 VMware Virtual Server
Memory by KiXtart macro: 1023
Memory GETCOMPUTERMEMORY UDF by M_Kiewitz: 18 MB
Memory GETCOMPUTERMEMORY UDF by M_Kiewitz moded by Al_Po: 18 MB
Memory GETCOMPUTERMEMORY UDF by M_Kiewitz moded by Al_Po Mod by NTDOC : 18
Memory UDF by Glenn: 1024
Memory MemSize UDF by Lonkero: 1073205248
Memory MemSize2 UDF by Lonkero:

2GB Server 2003
Memory by KiXtart macro: 2047
Memory GETCOMPUTERMEMORY UDF by M_Kiewitz: -2032 MB
Memory GETCOMPUTERMEMORY UDF by M_Kiewitz moded by Al_Po: -2032 MB
Memory GETCOMPUTERMEMORY UDF by M_Kiewitz moded by Al_Po Mod by NTDOC : 2064
Memory UDF by Glenn: 2048
Memory MemSize UDF by Lonkero: 2146947072
Memory MemSize2 UDF by Lonkero:

1GB Server 2000
Memory by KiXtart macro: 1023
Memory GETCOMPUTERMEMORY UDF by M_Kiewitz: 1024 MB
Memory GETCOMPUTERMEMORY UDF by M_Kiewitz moded by Al_Po: 1026 MB
Memory GETCOMPUTERMEMORY UDF by M_Kiewitz moded by Al_Po Mod by NTDOC : 1024
Memory UDF by Glenn: 1024
Memory MemSize UDF by Lonkero: 1073246208
Memory MemSize2 UDF by Lonkero:

2GB Server 2000
Memory by KiXtart macro: 2047
Memory GETCOMPUTERMEMORY UDF by M_Kiewitz: -2047 MB
Memory GETCOMPUTERMEMORY UDF by M_Kiewitz moded by Al_Po: -2047 MB
Memory GETCOMPUTERMEMORY UDF by M_Kiewitz moded by Al_Po Mod by NTDOC : 2048
Memory UDF by Glenn: 2048
Memory MemSize UDF by Lonkero: 2146979840
Memory MemSize2 UDF by Lonkero:

2.3GB Server 2000
Memory by KiXtart macro: 2047
Memory GETCOMPUTERMEMORY UDF by M_Kiewitz: -1791 MB
Memory GETCOMPUTERMEMORY UDF by M_Kiewitz moded by Al_Po: -1791 MB
Memory GETCOMPUTERMEMORY UDF by M_Kiewitz moded by Al_Po Mod by NTDOC : 2304
Memory UDF by Glenn: 2304
Memory MemSize UDF by Lonkero: 2415423488
Memory MemSize2 UDF by Lonkero:

3GB Server 2003
Memory by KiXtart macro: 2047
Memory GETCOMPUTERMEMORY UDF by M_Kiewitz: -1008 MB
Memory GETCOMPUTERMEMORY UDF by M_Kiewitz moded by Al_Po: -1008 MB
Memory GETCOMPUTERMEMORY UDF by M_Kiewitz moded by Al_Po Mod by NTDOC : 3088
Memory UDF by Glenn: 3072
Memory MemSize UDF by Lonkero: 3220721664
Memory MemSize2 UDF by Lonkero:

2GB Windows XP Pro
Memory by KiXtart macro: 2047
Memory GETCOMPUTERMEMORY UDF by M_Kiewitz: -2047 MB
Memory GETCOMPUTERMEMORY UDF by M_Kiewitz moded by Al_Po: -2047 MB
Memory GETCOMPUTERMEMORY UDF by M_Kiewitz moded by Al_Po Mod by NTDOC : 2048
Memory UDF by Glenn: 2048
Memory MemSize UDF by Lonkero: 2146938880
Memory MemSize2 UDF by Lonkero:

Top
Page 1 of 2 12>


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 601 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

Generated in 0.132 seconds in which 0.087 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org