在FreeBSD中,如何查看服务器安装的内存大小以及已使用内存大小?
Linux中如何查看物理内存和交换内存的大小?
使用top命令
top命令,可以查看系统进程的相关信息,包括"物理内存统计信息"。
- Active:活跃的字节数。
- Inact:无效的字节数。
- Wired:连线的字节数,包括BIO级缓存的文件数据页。
- Cache:可用于立即重新分配的干净字节缓存数据的数量。
- Buf:用于BIO级磁盘缓存的字节数。
- Free:可用字节数。
使用查看内存使用情况
使用Perl脚本查看内存使用情况
freemem.sh
#!/usr/bin/perl
##
## freebsd-memory -- List Total System Memory Usage
## Copyright (c) 2003-2004 Ralf S. Engelschall <rse@engelschall.com>
##
## 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->{} = ;
}
}
# 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"});
执行脚本
$ sudo chmod +x freemem.sh $ freemem.sh
输出示例:
SYSTEM MEMORY INFORMATION: mem_wire: 25341952 ( 24MB) [ 9%] Wired: disabled for paging out mem_active: + 47529984 ( 45MB) [ 18%] Active: recently referenced mem_inactive:+ 15605760 ( 14MB) [ 6%] Inactive: recently not referenced mem_cache: + 16384 ( 0MB) [ 0%] Cached: almost avail. for allocation mem_free: + 165556224 ( 157MB) [ 65%] Free: fully available for allocation mem_gap_vm: + 389120 ( 0MB) [ 0%] Memory gap: UNKNOWN -------------- ------------ ----------- ------ mem_all: = 254439424 ( 242MB) [100%] Total real memory managed mem_gap_sys: + 4988928 ( 4MB) Memory gap: Kernel?! -------------- ------------ ----------- mem_phys: = 259428352 ( 247MB) Total real memory available mem_gap_hw: + 9007104 ( 8MB) Memory gap: Segment Mappings?! -------------- ------------ ----------- mem_hw: = 268435456 ( 256MB) Total real memory installed SYSTEM MEMORY SUMMARY: mem_used: 87257088 ( 83MB) [ 32%] Logically used memory mem_avail: + 181178368 ( 172MB) [ 67%] Logically available memory -------------- ------------ ----------- ------ mem_total: = 268435456 ( 256MB) [100%] Logically total memory
使用shell脚本查看Linux内存使用情况
我基于FreeBSD的家庭路由器的输出示例:
$ fetch https://raw.githubusercontent.com/ocochard/myscripts/master/FreeBSD/freebsd-memory.sh 或者 ## $ curl -O https://raw.githubusercontent.com/ocochard/myscripts/master/FreeBSD/freebsd-memory.sh $ sh freebsd-memory.sh
输出示例:
SYSTEM MEMORY INFORMATION: mem_wire: 70152192 ( 66MB) [ 14%] Wired: disabled for paging out mem_active: + 44515328 ( 42MB) [ 9%] Active: recently referenced mem_inactive:+ 333316096 ( 317MB) [ 67%] Inactive: recently not referenced mem_cache: + 798720 ( 0MB) [ 0%] Cached: almost avail. for allocation mem_free: + 44724224 ( 42MB) [ 9%] Free: fully available for allocation mem_gap_vm: + -45056 ( 0MB) [ 0%] Memory gap: UNKNOWN ______________ ____________ ___________ ______ mem_all: = 493461504 ( 470MB) [100%] Total real memory managed mem_gap_sys: + 9297920 ( 8MB) Memory gap: Kernel?! ______________ ____________ ___________ mem_phys: = 502759424 ( 479MB) Total real memory available mem_gap_hw: + 34111488 ( 32MB) Memory gap: Segment Mappings?! ______________ ____________ ___________ mem_hw: = 536870912 ( 512MB) Total real memory installed SYSTEM MEMORY SUMMARY: mem_used: 158031872 ( 150MB) [ 29%] Logically used memory mem_avail: + 378839040 ( 361MB) [ 70%] Logically available memory ______________ ____________ __________ _______ mem_total: = 536870912 ( 512MB) [100%] Logically total memory
使用sysctl命令找出FreeBSD上安装了多少RAM。
执行以下命令:
$ sysctl hw.physmem $ sysctl hw | egrep 'hw.(phys|user|real)'
或者
$ grep memory /var/run/dmesg.boot
在FreeBSD中使用类似Linux free命令查看内存使用情况
Freecolor是类似free的软件
安装freecolor:
# cd /usr/ports/sysutils/freecolor # make install clean
或者
# pkg install freecolor
查看内存详细信息:
$ freecolor -m -o
输出示例:
total used free shared buffers cached Mem: 4082 825 3256 0 0 117 Swap: 2048 0 2047
$ freecolor -t -m -o
输出示例:
total used free shared buffers cached Mem: 4082 825 3256 0 0 117 Swap: 2048 0 2047 Total: 6130 = ( 826 (used) + 5421 (free))
日期:2019-11-20 08:53:58 来源:oir作者:oir
