snipt

Ctrl+h for KB shortcuts

Perl

FreeBSD find out RAM size including total amount of free and used memory size

#!/usr/bin/perl
##
##  freebsd-memory -- List Total System Memory Usage
##  Copyright (c) 2003-2004 Ralf S. Engelschall <[email protected]>
##  
##  Redistribution and use in source and binary forms, with or without
##  modification, are permitted provided that the following conditions
##  are met:
##  1. Redistributions of source code must retain the above copyright
##     notice, this list of conditions and the following disclaimer.
##  2. Redistributions in binary form must reproduce the above copyright
##     notice, this list of conditions and the following disclaimer in the
##     documentation and/or other materials provided with the distribution.
##  
##  THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
##  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
##  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
##  ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
##  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
##  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
##  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
##  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
##  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
##  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   query the system through the generic sysctl(8) interface
#   (this does not require special priviledges)
my $sysctl = {};
my $sysctl_output = `/sbin/sysctl -a`;
foreach my $line (split(/\n/, $sysctl_output)) {
    if ($line =~ m/^([^:]+):\s+(.+)\s*$/s) {
        $sysctl->{$1} = $2;
    }
}

#   round the physical memory size to the next power of two which is
#   reasonable for memory cards. We do this by first determining the
#   guessed memory card size under the assumption that usual computer
#   hardware has an average of a maximally eight memory cards installed
#   and those are usually of equal size.
sub mem_rounded {
    my ($mem_size) = @_;
    my $chip_size  = 1;
    my $chip_guess = ($mem_size / 8) - 1;
    while ($chip_guess != 0) {
        $chip_guess >>= 1;
        $chip_size  <<= 1;
    }
    my $mem_round = (int($mem_size / $chip_size) + 1) * $chip_size;
    return $mem_round;
}

#   determine the individual known information
#   NOTICE: forget hw.usermem, it is just (hw.physmem - vm.stats.vm.v_wire_count).
#   NOTICE: forget vm.stats.misc.zero_page_count, it is just the subset of
#           vm.stats.vm.v_free_count which is already pre-zeroed.
my $mem_hw        = &mem_rounded($sysctl->{"hw.physmem"});
my $mem_phys      = $sysctl->{"hw.physmem"};
my $mem_all       = $sysctl->{"vm.stats.vm.v_page_count"}      * $sysctl->{"hw.pagesize"};
my $mem_wire      = $sysctl->{"vm.stats.vm.v_wire_count"}      * $sysctl->{"hw.pagesize"};
my $mem_active    = $sysctl->{"vm.stats.vm.v_active_count"}    * $sysctl->{"hw.pagesize"};
my $mem_inactive  = $sysctl->{"vm.stats.vm.v_inactive_count"}  * $sysctl->{"hw.pagesize"};
my $mem_cache     = $sysctl->{"vm.stats.vm.v_cache_count"}     * $sysctl->{"hw.pagesize"};
my $mem_free      = $sysctl->{"vm.stats.vm.v_free_count"}      * $sysctl->{"hw.pagesize"};

#   determine the individual unknown information
my $mem_gap_vm    = $mem_all - ($mem_wire + $mem_active + $mem_inactive + $mem_cache + $mem_free);
my $mem_gap_sys   = $mem_phys - $mem_all;
my $mem_gap_hw    = $mem_hw   - $mem_phys;

#   determine logical summary information
my $mem_total = $mem_hw;
my $mem_avail = $mem_inactive + $mem_cache + $mem_free;
my $mem_used  = $mem_total - $mem_avail;

#   information annotations
my $info = {
    "mem_wire"     => 'Wired: disabled for paging out',
    "mem_active"   => 'Active: recently referenced',
    "mem_inactive" => 'Inactive: recently not referenced',
    "mem_cache"    => 'Cached: almost avail. for allocation',
    "mem_free"     => 'Free: fully available for allocation',
    "mem_gap_vm"   => 'Memory gap: UNKNOWN',
    "mem_all"      => 'Total real memory managed',
    "mem_gap_sys"  => 'Memory gap: Kernel?!',
    "mem_phys"     => 'Total real memory available',
    "mem_gap_hw"   => 'Memory gap: Segment Mappings?!',
    "mem_hw"       => 'Total real memory installed',
    "mem_used"     => 'Logically used memory',
    "mem_avail"    => 'Logically available memory',
    "mem_total"    => 'Logically total memory',
};

#   print system results
printf("SYSTEM MEMORY INFORMATION:\n");
printf("mem_wire:      %12d (%7dMB) [%3d%%] %s\n", $mem_wire,     $mem_wire     / (1024*1024), ($mem_wire     / $mem_all) * 100, $info->{"mem_wire"});
printf("mem_active:  + %12d (%7dMB) [%3d%%] %s\n", $mem_active,   $mem_active   / (1024*1024), ($mem_active   / $mem_all) * 100, $info->{"mem_active"});
printf("mem_inactive:+ %12d (%7dMB) [%3d%%] %s\n", $mem_inactive, $mem_inactive / (1024*1024), ($mem_inactive / $mem_all) * 100, $info->{"mem_inactive"});
printf("mem_cache:   + %12d (%7dMB) [%3d%%] %s\n", $mem_cache,    $mem_cache    / (1024*1024), ($mem_cache    / $mem_all) * 100, $info->{"mem_cache"});
printf("mem_free:    + %12d (%7dMB) [%3d%%] %s\n", $mem_free,     $mem_free     / (1024*1024), ($mem_free     / $mem_all) * 100, $info->{"mem_free"});
printf("mem_gap_vm:  + %12d (%7dMB) [%3d%%] %s\n", $mem_gap_vm,   $mem_gap_vm   / (1024*1024), ($mem_gap_vm   / $mem_all) * 100, $info->{"mem_gap_vm"});
printf("-------------- ------------ ----------- ------\n");
printf("mem_all:     = %12d (%7dMB) [100%%] %s\n", $mem_all,      $mem_all      / (1024*1024), $info->{"mem_all"});
printf("mem_gap_sys: + %12d (%7dMB)        %s\n",  $mem_gap_sys,  $mem_gap_sys  / (1024*1024), $info->{"mem_gap_sys"});
printf("-------------- ------------ -----------\n");
printf("mem_phys:    = %12d (%7dMB)        %s\n",  $mem_phys,     $mem_phys     / (1024*1024), $info->{"mem_phys"});
printf("mem_gap_hw:  + %12d (%7dMB)        %s\n",  $mem_gap_hw,   $mem_gap_hw   / (1024*1024), $info->{"mem_gap_hw"});     
printf("-------------- ------------ -----------\n");
printf("mem_hw:      = %12d (%7dMB)        %s\n",  $mem_hw,       $mem_hw       / (1024*1024), $info->{"mem_hw"});     

#   print logical results
printf("\n");
printf("SYSTEM MEMORY SUMMARY:\n");
printf("mem_used:      %12d (%7dMB) [%3d%%] %s\n", $mem_used,  $mem_used  / (1024*1024), ($mem_used  / $mem_total) * 100, $info->{"mem_used"});
printf("mem_avail:   + %12d (%7dMB) [%3d%%] %s\n", $mem_avail, $mem_avail / (1024*1024), ($mem_avail / $mem_total) * 100, $info->{"mem_avail"});
printf("-------------- ------------ ----------- ------\n");
printf("mem_total:   = %12d (%7dMB) [100%%] %s\n", $mem_total, $mem_total / (1024*1024), $info->{"mem_total"});
https://snipt.net/embed/2f0dc0d14148c9a73aacfa372022936d/
/raw/2f0dc0d14148c9a73aacfa372022936d/
2f0dc0d14148c9a73aacfa372022936d
perl
Perl
119
2019-08-26T04:12:16
True
False
False
/api/public/snipt/47659/
-885
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1"> 1</a> <a href="#L-2"> 2</a> <a href="#L-3"> 3</a> <a href="#L-4"> 4</a> <a href="#L-5"> 5</a> <a href="#L-6"> 6</a> <a href="#L-7"> 7</a> <a href="#L-8"> 8</a> <a href="#L-9"> 9</a> <a href="#L-10"> 10</a> <a href="#L-11"> 11</a> <a href="#L-12"> 12</a> <a href="#L-13"> 13</a> <a href="#L-14"> 14</a> <a href="#L-15"> 15</a> <a href="#L-16"> 16</a> <a href="#L-17"> 17</a> <a href="#L-18"> 18</a> <a href="#L-19"> 19</a> <a href="#L-20"> 20</a> <a href="#L-21"> 21</a> <a href="#L-22"> 22</a> <a href="#L-23"> 23</a> <a href="#L-24"> 24</a> <a href="#L-25"> 25</a> <a href="#L-26"> 26</a> <a href="#L-27"> 27</a> <a href="#L-28"> 28</a> <a href="#L-29"> 29</a> <a href="#L-30"> 30</a> <a href="#L-31"> 31</a> <a href="#L-32"> 32</a> <a href="#L-33"> 33</a> <a href="#L-34"> 34</a> <a href="#L-35"> 35</a> <a href="#L-36"> 36</a> <a href="#L-37"> 37</a> <a href="#L-38"> 38</a> <a href="#L-39"> 39</a> <a href="#L-40"> 40</a> <a href="#L-41"> 41</a> <a href="#L-42"> 42</a> <a href="#L-43"> 43</a> <a href="#L-44"> 44</a> <a href="#L-45"> 45</a> <a href="#L-46"> 46</a> <a href="#L-47"> 47</a> <a href="#L-48"> 48</a> <a href="#L-49"> 49</a> <a href="#L-50"> 50</a> <a href="#L-51"> 51</a> <a href="#L-52"> 52</a> <a href="#L-53"> 53</a> <a href="#L-54"> 54</a> <a href="#L-55"> 55</a> <a href="#L-56"> 56</a> <a href="#L-57"> 57</a> <a href="#L-58"> 58</a> <a href="#L-59"> 59</a> <a href="#L-60"> 60</a> <a href="#L-61"> 61</a> <a href="#L-62"> 62</a> <a href="#L-63"> 63</a> <a href="#L-64"> 64</a> <a href="#L-65"> 65</a> <a href="#L-66"> 66</a> <a href="#L-67"> 67</a> <a href="#L-68"> 68</a> <a href="#L-69"> 69</a> <a href="#L-70"> 70</a> <a href="#L-71"> 71</a> <a href="#L-72"> 72</a> <a href="#L-73"> 73</a> <a href="#L-74"> 74</a> <a href="#L-75"> 75</a> <a href="#L-76"> 76</a> <a href="#L-77"> 77</a> <a href="#L-78"> 78</a> <a href="#L-79"> 79</a> <a href="#L-80"> 80</a> <a href="#L-81"> 81</a> <a href="#L-82"> 82</a> <a href="#L-83"> 83</a> <a href="#L-84"> 84</a> <a href="#L-85"> 85</a> <a href="#L-86"> 86</a> <a href="#L-87"> 87</a> <a href="#L-88"> 88</a> <a href="#L-89"> 89</a> <a href="#L-90"> 90</a> <a href="#L-91"> 91</a> <a href="#L-92"> 92</a> <a href="#L-93"> 93</a> <a href="#L-94"> 94</a> <a href="#L-95"> 95</a> <a href="#L-96"> 96</a> <a href="#L-97"> 97</a> <a href="#L-98"> 98</a> <a href="#L-99"> 99</a> <a href="#L-100">100</a> <a href="#L-101">101</a> <a href="#L-102">102</a> <a href="#L-103">103</a> <a href="#L-104">104</a> <a href="#L-105">105</a> <a href="#L-106">106</a> <a href="#L-107">107</a> <a href="#L-108">108</a> <a href="#L-109">109</a> <a href="#L-110">110</a> <a href="#L-111">111</a> <a href="#L-112">112</a> <a href="#L-113">113</a> <a href="#L-114">114</a> <a href="#L-115">115</a> <a href="#L-116">116</a> <a href="#L-117">117</a> <a href="#L-118">118</a> <a href="#L-119">119</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="ch">#!/usr/bin/perl</span> </span><span id="L-2"><a name="L-2"></a><span class="c1">##</span> </span><span id="L-3"><a name="L-3"></a><span class="c1">## freebsd-memory -- List Total System Memory Usage</span> </span><span id="L-4"><a name="L-4"></a><span class="c1">## Copyright (c) 2003-2004 Ralf S. Engelschall &lt;[email protected]&gt;</span> </span><span id="L-5"><a name="L-5"></a><span class="c1">## </span> </span><span id="L-6"><a name="L-6"></a><span class="c1">## Redistribution and use in source and binary forms, with or without</span> </span><span id="L-7"><a name="L-7"></a><span class="c1">## modification, are permitted provided that the following conditions</span> </span><span id="L-8"><a name="L-8"></a><span class="c1">## are met:</span> </span><span id="L-9"><a name="L-9"></a><span class="c1">## 1. Redistributions of source code must retain the above copyright</span> </span><span id="L-10"><a name="L-10"></a><span class="c1">## notice, this list of conditions and the following disclaimer.</span> </span><span id="L-11"><a name="L-11"></a><span class="c1">## 2. Redistributions in binary form must reproduce the above copyright</span> </span><span id="L-12"><a name="L-12"></a><span class="c1">## notice, this list of conditions and the following disclaimer in the</span> </span><span id="L-13"><a name="L-13"></a><span class="c1">## documentation and/or other materials provided with the distribution.</span> </span><span id="L-14"><a name="L-14"></a><span class="c1">## </span> </span><span id="L-15"><a name="L-15"></a><span class="c1">## THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS&#39;&#39; AND</span> </span><span id="L-16"><a name="L-16"></a><span class="c1">## ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE</span> </span><span id="L-17"><a name="L-17"></a><span class="c1">## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE</span> </span><span id="L-18"><a name="L-18"></a><span class="c1">## ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE</span> </span><span id="L-19"><a name="L-19"></a><span class="c1">## FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL</span> </span><span id="L-20"><a name="L-20"></a><span class="c1">## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS</span> </span><span id="L-21"><a name="L-21"></a><span class="c1">## OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)</span> </span><span id="L-22"><a name="L-22"></a><span class="c1">## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT</span> </span><span id="L-23"><a name="L-23"></a><span class="c1">## LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY</span> </span><span id="L-24"><a name="L-24"></a><span class="c1">## OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF</span> </span><span id="L-25"><a name="L-25"></a><span class="c1">## SUCH DAMAGE.</span> </span><span id="L-26"><a name="L-26"></a><span class="c1">##</span> </span><span id="L-27"><a name="L-27"></a> </span><span id="L-28"><a name="L-28"></a><span class="c1"># query the system through the generic sysctl(8) interface</span> </span><span id="L-29"><a name="L-29"></a><span class="c1"># (this does not require special priviledges)</span> </span><span id="L-30"><a name="L-30"></a><span class="k">my</span> <span class="nv">$sysctl</span> <span class="o">=</span> <span class="p">{};</span> </span><span id="L-31"><a name="L-31"></a><span class="k">my</span> <span class="nv">$sysctl_output</span> <span class="o">=</span> <span class="sb">`/sbin/sysctl -a`</span><span class="p">;</span> </span><span id="L-32"><a name="L-32"></a><span class="k">foreach</span> <span class="k">my</span> <span class="nv">$line</span> <span class="p">(</span><span class="nb">split</span><span class="p">(</span><span class="sr">/\n/</span><span class="p">,</span> <span class="nv">$sysctl_output</span><span class="p">))</span> <span class="p">{</span> </span><span id="L-33"><a name="L-33"></a> <span class="k">if</span> <span class="p">(</span><span class="nv">$line</span> <span class="o">=~</span> <span class="sr">m/^([^:]+):\s+(.+)\s*$/s</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-34"><a name="L-34"></a> <span class="nv">$sysctl</span><span class="o">-&gt;</span><span class="p">{</span><span class="nv">$1</span><span class="p">}</span> <span class="o">=</span> <span class="nv">$2</span><span class="p">;</span> </span><span id="L-35"><a name="L-35"></a> <span class="p">}</span> </span><span id="L-36"><a name="L-36"></a><span class="p">}</span> </span><span id="L-37"><a name="L-37"></a> </span><span id="L-38"><a name="L-38"></a><span class="c1"># round the physical memory size to the next power of two which is</span> </span><span id="L-39"><a name="L-39"></a><span class="c1"># reasonable for memory cards. We do this by first determining the</span> </span><span id="L-40"><a name="L-40"></a><span class="c1"># guessed memory card size under the assumption that usual computer</span> </span><span id="L-41"><a name="L-41"></a><span class="c1"># hardware has an average of a maximally eight memory cards installed</span> </span><span id="L-42"><a name="L-42"></a><span class="c1"># and those are usually of equal size.</span> </span><span id="L-43"><a name="L-43"></a><span class="k">sub </span><span class="nf">mem_rounded</span> <span class="p">{</span> </span><span id="L-44"><a name="L-44"></a> <span class="k">my</span> <span class="p">(</span><span class="nv">$mem_size</span><span class="p">)</span> <span class="o">=</span> <span class="nv">@_</span><span class="p">;</span> </span><span id="L-45"><a name="L-45"></a> <span class="k">my</span> <span class="nv">$chip_size</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> </span><span id="L-46"><a name="L-46"></a> <span class="k">my</span> <span class="nv">$chip_guess</span> <span class="o">=</span> <span class="p">(</span><span class="nv">$mem_size</span> <span class="o">/</span> <span class="mi">8</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span> </span><span id="L-47"><a name="L-47"></a> <span class="k">while</span> <span class="p">(</span><span class="nv">$chip_guess</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-48"><a name="L-48"></a> <span class="nv">$chip_guess</span> <span class="o">&gt;&gt;=</span> <span class="mi">1</span><span class="p">;</span> </span><span id="L-49"><a name="L-49"></a> <span class="nv">$chip_size</span> <span class="o">&lt;&lt;=</span> <span class="mi">1</span><span class="p">;</span> </span><span id="L-50"><a name="L-50"></a> <span class="p">}</span> </span><span id="L-51"><a name="L-51"></a> <span class="k">my</span> <span class="nv">$mem_round</span> <span class="o">=</span> <span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="nv">$mem_size</span> <span class="o">/</span> <span class="nv">$chip_size</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="nv">$chip_size</span><span class="p">;</span> </span><span id="L-52"><a name="L-52"></a> <span class="k">return</span> <span class="nv">$mem_round</span><span class="p">;</span> </span><span id="L-53"><a name="L-53"></a><span class="p">}</span> </span><span id="L-54"><a name="L-54"></a> </span><span id="L-55"><a name="L-55"></a><span class="c1"># determine the individual known information</span> </span><span id="L-56"><a name="L-56"></a><span class="c1"># NOTICE: forget hw.usermem, it is just (hw.physmem - vm.stats.vm.v_wire_count).</span> </span><span id="L-57"><a name="L-57"></a><span class="c1"># NOTICE: forget vm.stats.misc.zero_page_count, it is just the subset of</span> </span><span id="L-58"><a name="L-58"></a><span class="c1"># vm.stats.vm.v_free_count which is already pre-zeroed.</span> </span><span id="L-59"><a name="L-59"></a><span class="k">my</span> <span class="nv">$mem_hw</span> <span class="o">=</span> <span class="o">&amp;</span><span class="n">mem_rounded</span><span class="p">(</span><span class="nv">$sysctl</span><span class="o">-&gt;</span><span class="p">{</span><span class="s">&quot;hw.physmem&quot;</span><span class="p">});</span> </span><span id="L-60"><a name="L-60"></a><span class="k">my</span> <span class="nv">$mem_phys</span> <span class="o">=</span> <span class="nv">$sysctl</span><span class="o">-&gt;</span><span class="p">{</span><span class="s">&quot;hw.physmem&quot;</span><span class="p">};</span> </span><span id="L-61"><a name="L-61"></a><span class="k">my</span> <span class="nv">$mem_all</span> <span class="o">=</span> <span class="nv">$sysctl</span><span class="o">-&gt;</span><span class="p">{</span><span class="s">&quot;vm.stats.vm.v_page_count&quot;</span><span class="p">}</span> <span class="o">*</span> <span class="nv">$sysctl</span><span class="o">-&gt;</span><span class="p">{</span><span class="s">&quot;hw.pagesize&quot;</span><span class="p">};</span> </span><span id="L-62"><a name="L-62"></a><span class="k">my</span> <span class="nv">$mem_wire</span> <span class="o">=</span> <span class="nv">$sysctl</span><span class="o">-&gt;</span><span class="p">{</span><span class="s">&quot;vm.stats.vm.v_wire_count&quot;</span><span class="p">}</span> <span class="o">*</span> <span class="nv">$sysctl</span><span class="o">-&gt;</span><span class="p">{</span><span class="s">&quot;hw.pagesize&quot;</span><span class="p">};</span> </span><span id="L-63"><a name="L-63"></a><span class="k">my</span> <span class="nv">$mem_active</span> <span class="o">=</span> <span class="nv">$sysctl</span><span class="o">-&gt;</span><span class="p">{</span><span class="s">&quot;vm.stats.vm.v_active_count&quot;</span><span class="p">}</span> <span class="o">*</span> <span class="nv">$sysctl</span><span class="o">-&gt;</span><span class="p">{</span><span class="s">&quot;hw.pagesize&quot;</span><span class="p">};</span> </span><span id="L-64"><a name="L-64"></a><span class="k">my</span> <span class="nv">$mem_inactive</span> <span class="o">=</span> <span class="nv">$sysctl</span><span class="o">-&gt;</span><span class="p">{</span><span class="s">&quot;vm.stats.vm.v_inactive_count&quot;</span><span class="p">}</span> <span class="o">*</span> <span class="nv">$sysctl</span><span class="o">-&gt;</span><span class="p">{</span><span class="s">&quot;hw.pagesize&quot;</span><span class="p">};</span> </span><span id="L-65"><a name="L-65"></a><span class="k">my</span> <span class="nv">$mem_cache</span> <span class="o">=</span> <span class="nv">$sysctl</span><span class="o">-&gt;</span><span class="p">{</span><span class="s">&quot;vm.stats.vm.v_cache_count&quot;</span><span class="p">}</span> <span class="o">*</span> <span class="nv">$sysctl</span><span class="o">-&gt;</span><span class="p">{</span><span class="s">&quot;hw.pagesize&quot;</span><span class="p">};</span> </span><span id="L-66"><a name="L-66"></a><span class="k">my</span> <span class="nv">$mem_free</span> <span class="o">=</span> <span class="nv">$sysctl</span><span class="o">-&gt;</span><span class="p">{</span><span class="s">&quot;vm.stats.vm.v_free_count&quot;</span><span class="p">}</span> <span class="o">*</span> <span class="nv">$sysctl</span><span class="o">-&gt;</span><span class="p">{</span><span class="s">&quot;hw.pagesize&quot;</span><span class="p">};</span> </span><span id="L-67"><a name="L-67"></a> </span><span id="L-68"><a name="L-68"></a><span class="c1"># determine the individual unknown information</span> </span><span id="L-69"><a name="L-69"></a><span class="k">my</span> <span class="nv">$mem_gap_vm</span> <span class="o">=</span> <span class="nv">$mem_all</span> <span class="o">-</span> <span class="p">(</span><span class="nv">$mem_wire</span> <span class="o">+</span> <span class="nv">$mem_active</span> <span class="o">+</span> <span class="nv">$mem_inactive</span> <span class="o">+</span> <span class="nv">$mem_cache</span> <span class="o">+</span> <span class="nv">$mem_free</span><span class="p">);</span> </span><span id="L-70"><a name="L-70"></a><span class="k">my</span> <span class="nv">$mem_gap_sys</span> <span class="o">=</span> <span class="nv">$mem_phys</span> <span class="o">-</span> <span class="nv">$mem_all</span><span class="p">;</span> </span><span id="L-71"><a name="L-71"></a><span class="k">my</span> <span class="nv">$mem_gap_hw</span> <span class="o">=</span> <span class="nv">$mem_hw</span> <span class="o">-</span> <span class="nv">$mem_phys</span><span class="p">;</span> </span><span id="L-72"><a name="L-72"></a> </span><span id="L-73"><a name="L-73"></a><span class="c1"># determine logical summary information</span> </span><span id="L-74"><a name="L-74"></a><span class="k">my</span> <span class="nv">$mem_total</span> <span class="o">=</span> <span class="nv">$mem_hw</span><span class="p">;</span> </span><span id="L-75"><a name="L-75"></a><span class="k">my</span> <span class="nv">$mem_avail</span> <span class="o">=</span> <span class="nv">$mem_inactive</span> <span class="o">+</span> <span class="nv">$mem_cache</span> <span class="o">+</span> <span class="nv">$mem_free</span><span class="p">;</span> </span><span id="L-76"><a name="L-76"></a><span class="k">my</span> <span class="nv">$mem_used</span> <span class="o">=</span> <span class="nv">$mem_total</span> <span class="o">-</span> <span class="nv">$mem_avail</span><span class="p">;</span> </span><span id="L-77"><a name="L-77"></a> </span><span id="L-78"><a name="L-78"></a><span class="c1"># information annotations</span> </span><span id="L-79"><a name="L-79"></a><span class="k">my</span> <span class="nv">$info</span> <span class="o">=</span> <span class="p">{</span> </span><span id="L-80"><a name="L-80"></a> <span class="s">&quot;mem_wire&quot;</span> <span class="o">=&gt;</span> <span class="s">&#39;Wired: disabled for paging out&#39;</span><span class="p">,</span> </span><span id="L-81"><a name="L-81"></a> <span class="s">&quot;mem_active&quot;</span> <span class="o">=&gt;</span> <span class="s">&#39;Active: recently referenced&#39;</span><span class="p">,</span> </span><span id="L-82"><a name="L-82"></a> <span class="s">&quot;mem_inactive&quot;</span> <span class="o">=&gt;</span> <span class="s">&#39;Inactive: recently not referenced&#39;</span><span class="p">,</span> </span><span id="L-83"><a name="L-83"></a> <span class="s">&quot;mem_cache&quot;</span> <span class="o">=&gt;</span> <span class="s">&#39;Cached: almost avail. for allocation&#39;</span><span class="p">,</span> </span><span id="L-84"><a name="L-84"></a> <span class="s">&quot;mem_free&quot;</span> <span class="o">=&gt;</span> <span class="s">&#39;Free: fully available for allocation&#39;</span><span class="p">,</span> </span><span id="L-85"><a name="L-85"></a> <span class="s">&quot;mem_gap_vm&quot;</span> <span class="o">=&gt;</span> <span class="s">&#39;Memory gap: UNKNOWN&#39;</span><span class="p">,</span> </span><span id="L-86"><a name="L-86"></a> <span class="s">&quot;mem_all&quot;</span> <span class="o">=&gt;</span> <span class="s">&#39;Total real memory managed&#39;</span><span class="p">,</span> </span><span id="L-87"><a name="L-87"></a> <span class="s">&quot;mem_gap_sys&quot;</span> <span class="o">=&gt;</span> <span class="s">&#39;Memory gap: Kernel?!&#39;</span><span class="p">,</span> </span><span id="L-88"><a name="L-88"></a> <span class="s">&quot;mem_phys&quot;</span> <span class="o">=&gt;</span> <span class="s">&#39;Total real memory available&#39;</span><span class="p">,</span> </span><span id="L-89"><a name="L-89"></a> <span class="s">&quot;mem_gap_hw&quot;</span> <span class="o">=&gt;</span> <span class="s">&#39;Memory gap: Segment Mappings?!&#39;</span><span class="p">,</span> </span><span id="L-90"><a name="L-90"></a> <span class="s">&quot;mem_hw&quot;</span> <span class="o">=&gt;</span> <span class="s">&#39;Total real memory installed&#39;</span><span class="p">,</span> </span><span id="L-91"><a name="L-91"></a> <span class="s">&quot;mem_used&quot;</span> <span class="o">=&gt;</span> <span class="s">&#39;Logically used memory&#39;</span><span class="p">,</span> </span><span id="L-92"><a name="L-92"></a> <span class="s">&quot;mem_avail&quot;</span> <span class="o">=&gt;</span> <span class="s">&#39;Logically available memory&#39;</span><span class="p">,</span> </span><span id="L-93"><a name="L-93"></a> <span class="s">&quot;mem_total&quot;</span> <span class="o">=&gt;</span> <span class="s">&#39;Logically total memory&#39;</span><span class="p">,</span> </span><span id="L-94"><a name="L-94"></a><span class="p">};</span> </span><span id="L-95"><a name="L-95"></a> </span><span id="L-96"><a name="L-96"></a><span class="c1"># print system results</span> </span><span id="L-97"><a name="L-97"></a><span class="nb">printf</span><span class="p">(</span><span class="s">&quot;SYSTEM MEMORY INFORMATION:\n&quot;</span><span class="p">);</span> </span><span id="L-98"><a name="L-98"></a><span class="nb">printf</span><span class="p">(</span><span class="s">&quot;mem_wire: %12d (%7dMB) [%3d%%] %s\n&quot;</span><span class="p">,</span> <span class="nv">$mem_wire</span><span class="p">,</span> <span class="nv">$mem_wire</span> <span class="sr">/ (1024*1024), ($mem_wire /</span> <span class="nv">$mem_all</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100</span><span class="p">,</span> <span class="nv">$info</span><span class="o">-&gt;</span><span class="p">{</span><span class="s">&quot;mem_wire&quot;</span><span class="p">});</span> </span><span id="L-99"><a name="L-99"></a><span class="nb">printf</span><span class="p">(</span><span class="s">&quot;mem_active: + %12d (%7dMB) [%3d%%] %s\n&quot;</span><span class="p">,</span> <span class="nv">$mem_active</span><span class="p">,</span> <span class="nv">$mem_active</span> <span class="sr">/ (1024*1024), ($mem_active /</span> <span class="nv">$mem_all</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100</span><span class="p">,</span> <span class="nv">$info</span><span class="o">-&gt;</span><span class="p">{</span><span class="s">&quot;mem_active&quot;</span><span class="p">});</span> </span><span id="L-100"><a name="L-100"></a><span class="nb">printf</span><span class="p">(</span><span class="s">&quot;mem_inactive:+ %12d (%7dMB) [%3d%%] %s\n&quot;</span><span class="p">,</span> <span class="nv">$mem_inactive</span><span class="p">,</span> <span class="nv">$mem_inactive</span> <span class="sr">/ (1024*1024), ($mem_inactive /</span> <span class="nv">$mem_all</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100</span><span class="p">,</span> <span class="nv">$info</span><span class="o">-&gt;</span><span class="p">{</span><span class="s">&quot;mem_inactive&quot;</span><span class="p">});</span> </span><span id="L-101"><a name="L-101"></a><span class="nb">printf</span><span class="p">(</span><span class="s">&quot;mem_cache: + %12d (%7dMB) [%3d%%] %s\n&quot;</span><span class="p">,</span> <span class="nv">$mem_cache</span><span class="p">,</span> <span class="nv">$mem_cache</span> <span class="sr">/ (1024*1024), ($mem_cache /</span> <span class="nv">$mem_all</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100</span><span class="p">,</span> <span class="nv">$info</span><span class="o">-&gt;</span><span class="p">{</span><span class="s">&quot;mem_cache&quot;</span><span class="p">});</span> </span><span id="L-102"><a name="L-102"></a><span class="nb">printf</span><span class="p">(</span><span class="s">&quot;mem_free: + %12d (%7dMB) [%3d%%] %s\n&quot;</span><span class="p">,</span> <span class="nv">$mem_free</span><span class="p">,</span> <span class="nv">$mem_free</span> <span class="sr">/ (1024*1024), ($mem_free /</span> <span class="nv">$mem_all</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100</span><span class="p">,</span> <span class="nv">$info</span><span class="o">-&gt;</span><span class="p">{</span><span class="s">&quot;mem_free&quot;</span><span class="p">});</span> </span><span id="L-103"><a name="L-103"></a><span class="nb">printf</span><span class="p">(</span><span class="s">&quot;mem_gap_vm: + %12d (%7dMB) [%3d%%] %s\n&quot;</span><span class="p">,</span> <span class="nv">$mem_gap_vm</span><span class="p">,</span> <span class="nv">$mem_gap_vm</span> <span class="sr">/ (1024*1024), ($mem_gap_vm /</span> <span class="nv">$mem_all</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100</span><span class="p">,</span> <span class="nv">$info</span><span class="o">-&gt;</span><span class="p">{</span><span class="s">&quot;mem_gap_vm&quot;</span><span class="p">});</span> </span><span id="L-104"><a name="L-104"></a><span class="nb">printf</span><span class="p">(</span><span class="s">&quot;-------------- ------------ ----------- ------\n&quot;</span><span class="p">);</span> </span><span id="L-105"><a name="L-105"></a><span class="nb">printf</span><span class="p">(</span><span class="s">&quot;mem_all: = %12d (%7dMB) [100%%] %s\n&quot;</span><span class="p">,</span> <span class="nv">$mem_all</span><span class="p">,</span> <span class="nv">$mem_all</span> <span class="o">/</span> <span class="p">(</span><span class="mi">1024</span><span class="o">*</span><span class="mi">1024</span><span class="p">),</span> <span class="nv">$info</span><span class="o">-&gt;</span><span class="p">{</span><span class="s">&quot;mem_all&quot;</span><span class="p">});</span> </span><span id="L-106"><a name="L-106"></a><span class="nb">printf</span><span class="p">(</span><span class="s">&quot;mem_gap_sys: + %12d (%7dMB) %s\n&quot;</span><span class="p">,</span> <span class="nv">$mem_gap_sys</span><span class="p">,</span> <span class="nv">$mem_gap_sys</span> <span class="o">/</span> <span class="p">(</span><span class="mi">1024</span><span class="o">*</span><span class="mi">1024</span><span class="p">),</span> <span class="nv">$info</span><span class="o">-&gt;</span><span class="p">{</span><span class="s">&quot;mem_gap_sys&quot;</span><span class="p">});</span> </span><span id="L-107"><a name="L-107"></a><span class="nb">printf</span><span class="p">(</span><span class="s">&quot;-------------- ------------ -----------\n&quot;</span><span class="p">);</span> </span><span id="L-108"><a name="L-108"></a><span class="nb">printf</span><span class="p">(</span><span class="s">&quot;mem_phys: = %12d (%7dMB) %s\n&quot;</span><span class="p">,</span> <span class="nv">$mem_phys</span><span class="p">,</span> <span class="nv">$mem_phys</span> <span class="o">/</span> <span class="p">(</span><span class="mi">1024</span><span class="o">*</span><span class="mi">1024</span><span class="p">),</span> <span class="nv">$info</span><span class="o">-&gt;</span><span class="p">{</span><span class="s">&quot;mem_phys&quot;</span><span class="p">});</span> </span><span id="L-109"><a name="L-109"></a><span class="nb">printf</span><span class="p">(</span><span class="s">&quot;mem_gap_hw: + %12d (%7dMB) %s\n&quot;</span><span class="p">,</span> <span class="nv">$mem_gap_hw</span><span class="p">,</span> <span class="nv">$mem_gap_hw</span> <span class="o">/</span> <span class="p">(</span><span class="mi">1024</span><span class="o">*</span><span class="mi">1024</span><span class="p">),</span> <span class="nv">$info</span><span class="o">-&gt;</span><span class="p">{</span><span class="s">&quot;mem_gap_hw&quot;</span><span class="p">});</span> </span><span id="L-110"><a name="L-110"></a><span class="nb">printf</span><span class="p">(</span><span class="s">&quot;-------------- ------------ -----------\n&quot;</span><span class="p">);</span> </span><span id="L-111"><a name="L-111"></a><span class="nb">printf</span><span class="p">(</span><span class="s">&quot;mem_hw: = %12d (%7dMB) %s\n&quot;</span><span class="p">,</span> <span class="nv">$mem_hw</span><span class="p">,</span> <span class="nv">$mem_hw</span> <span class="o">/</span> <span class="p">(</span><span class="mi">1024</span><span class="o">*</span><span class="mi">1024</span><span class="p">),</span> <span class="nv">$info</span><span class="o">-&gt;</span><span class="p">{</span><span class="s">&quot;mem_hw&quot;</span><span class="p">});</span> </span><span id="L-112"><a name="L-112"></a> </span><span id="L-113"><a name="L-113"></a><span class="c1"># print logical results</span> </span><span id="L-114"><a name="L-114"></a><span class="nb">printf</span><span class="p">(</span><span class="s">&quot;\n&quot;</span><span class="p">);</span> </span><span id="L-115"><a name="L-115"></a><span class="nb">printf</span><span class="p">(</span><span class="s">&quot;SYSTEM MEMORY SUMMARY:\n&quot;</span><span class="p">);</span> </span><span id="L-116"><a name="L-116"></a><span class="nb">printf</span><span class="p">(</span><span class="s">&quot;mem_used: %12d (%7dMB) [%3d%%] %s\n&quot;</span><span class="p">,</span> <span class="nv">$mem_used</span><span class="p">,</span> <span class="nv">$mem_used</span> <span class="sr">/ (1024*1024), ($mem_used /</span> <span class="nv">$mem_total</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100</span><span class="p">,</span> <span class="nv">$info</span><span class="o">-&gt;</span><span class="p">{</span><span class="s">&quot;mem_used&quot;</span><span class="p">});</span> </span><span id="L-117"><a name="L-117"></a><span class="nb">printf</span><span class="p">(</span><span class="s">&quot;mem_avail: + %12d (%7dMB) [%3d%%] %s\n&quot;</span><span class="p">,</span> <span class="nv">$mem_avail</span><span class="p">,</span> <span class="nv">$mem_avail</span> <span class="sr">/ (1024*1024), ($mem_avail /</span> <span class="nv">$mem_total</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100</span><span class="p">,</span> <span class="nv">$info</span><span class="o">-&gt;</span><span class="p">{</span><span class="s">&quot;mem_avail&quot;</span><span class="p">});</span> </span><span id="L-118"><a name="L-118"></a><span class="nb">printf</span><span class="p">(</span><span class="s">&quot;-------------- ------------ ----------- ------\n&quot;</span><span class="p">);</span> </span><span id="L-119"><a name="L-119"></a><span class="nb">printf</span><span class="p">(</span><span class="s">&quot;mem_total: = %12d (%7dMB) [100%%] %s\n&quot;</span><span class="p">,</span> <span class="nv">$mem_total</span><span class="p">,</span> <span class="nv">$mem_total</span> <span class="o">/</span> <span class="p">(</span><span class="mi">1024</span><span class="o">*</span><span class="mi">1024</span><span class="p">),</span> <span class="nv">$info</span><span class="o">-&gt;</span><span class="p">{</span><span class="s">&quot;mem_total&quot;</span><span class="p">});</span> </span></pre></div> </td></tr></table>
free, freebsd, memory, perl