Operating System - HP-UX
1753964 Members
7392 Online
108811 Solutions
New Discussion

Re: Calculating Memory Use with awk

 
Don Morris_1
Honored Contributor

Re: Calculating Memory Use with Awk

Note that VSZ and SZ aren't at all the same thing. One (VSZ) is the virtual object size, the other is actual physical memory consumption. [If you malloc 500 pages but only touch 1, VSZ would be 500 * page size / 1024, SZ would be 1 (allowing for variable page size heuristic hints, etc.)].

Again -- I don't know enough awk (sorry, I think in C not shell scripts) to know exactly how to work it in... but I can't recommend hard-coding the expectation that the system page size is 4kb in scripts. The page size is tunable with 11.31.0809 and I'd expect it to remain so in the future. Your scripts doubtless work fine now -- but when and if you encounter a system with 16kb or 64kb pages, you will get the wrong answer. "getconf _SC_PAGE_SIZE" will always give you the right factor. (Divide that by 1024 first if you want Kb and are worried about overflows -- but you'd still run into problems on a 2Tb or beyond system, right? Assuming signed 32-bit math and all).
Dennis Handly
Acclaimed Contributor

Re: Calculating Memory Use with awk

>TTr: I used the awk statement from above code and it stopped counting at the above number. Maybe I should have said "It appears that awk is using 32-bit arithmetic...".

Ah, I used a giant number and had no problems:
awk 'BEGIN {print 2000000000 + 2000000000 + 2000000000 }' /dev/null
6e+09
Unfortunately this format gives a bogus answer:
printf "%lld\n"
4612175268889493503

So you would need: printf "%.0f\n"
6000000000