性能相关

31 篇文章 1 订阅

1、top 命令

 top  -o PID,CMDLINE,%CPU,%MEM,TIME+    -s  3  -H   -d  time  -n  loop   -p pid

-o  显示的字段

-s  排序字段编号

-d 刷新delay 时间

-n 循环多少次退出

-p 针对特定process

 

 

2、ps 命令

 

Terms

  • VSS- Virtual Set Size 虚拟耗用内存(包含共享库占用的内存)
  • RSS- Resident Set Size 实际使用物理内存(包含共享库占用的内存)
  • PSS- Proportional Set Size 实际使用的物理内存(比例分配共享库占用的内存)
  • USS- Unique Set Size 进程独自占用的物理内存(不包含共享库占用的内存)
  • VSS= RSS+ 未分配实际物理内存

 

一般来说内存占用大小有如下规律:VSS >= RSS >= PSS >= USS

 

Overview

The aim of this post is to provide information that will assist in interpreting memory reports from various tools so the true memory usage for Linux processes and the system can be determined.

Android has a tool called procrank (/system/xbin/procrank), which lists out the memory usage of Linux processes in order from highest to lowest usage. The sizes reported per process are VSS, RSS, PSS, and USS.

For the sake of simplicity in this description, memory will be expressed in terms of pages, rather than bytes. Linux systems like ours manage memory in 4096 byte pages at the lowest level.

VSS (reported as VSZ from ps) is the total accessible address space of a process.This size also includes memory that may not be resident in RAM like mallocs that have been allocated but not written to. VSS is of very little use for determing real memory usage of a process.

RSS is the total memory actually held in RAM for a process.RSS can be misleading, because it reports the total all of the shared libraries that the process uses, even though a shared library is only loaded into memory once regardless of how many processes use it. RSS is not an accurate representation of the memory usage for a single process.

PSS differs from RSS in that it reports the proportional size of its shared libraries, i.e. if three processes all use a shared library that has 30 pages, that library will only contribute 10 pages to the PSS that is reported for each of the three processes. PSS is a very useful number because when the PSS for all processes in the system are summed together, that is a good representation for the total memory usage in the system. When a process is killed, the shared libraries that contributed to its PSS will be proportionally distributed to the PSS totals for the remaining processes still using that library. In this way PSS can be slightly misleading, because when a process is killed, PSS does not accurately represent the memory returned to the overall system.

USS is the total private memory for a process, i.e. that memory that is completely unique to that process.USS is an extremely useful number because it indicates the true incremental cost of running a particular process. When a process is killed, the USS is the total memory that is actually returned to the system. USS is the best number to watch when initially suspicious of memory leaksin a process.

 

PS 命令

 

sz          SZ        size in physical pages of the core image of the process.  This includes text, data, and stack space.  Device mappings are currently excluded; this is subject to change.  See vsz and rss.
  vsz         VSZ       virtual memory size of the process in KiB (1024-byte units).  Device mappings are currently excluded; this is subject to change.  (alias vsize).

size        SIZE      approximate amount of swap space that would be required if the process were to dirty all writable pages and then be swapped out.  This number is very rough!
%mem        %MEM      ratio of the process's resident set size  to the physical memory on the machine, expressed as a percentage.  (alias pmem).

 rss         RSS       resident set size, the non-swapped physical memory that a task has used (inkiloBytes).  (alias rssize, rsz).

 

 

ps -A  -o  CMD,PID,PPID,VSZ,SZ,RSS,%VSZ,%MEM,PCPU    -k  -PCPU | head -n 10

3、free 命令输出

                     total       used       free     shared    buffers     cached
Mem:                996M       525M       470M        56K        38M       118M
-/+ buffers/cache:            368M        627M
Swap:               1.5G       234M       1.2G

 

  • total: 内存总数
  • used: 已经使用内存数
  • free: 完全空闲内存
  • shared: 多个进程共享的内存
  • buffers: 用于块设备数据缓冲,记录文件系统metadata(目录,权限,属性等)
  • cached: 用于文件内容的缓冲
  • Mem: 物理内存
  • -/+ buffers/cache: 基于应用角度考虑(计算已使用内存时减去buffers/cache,计算可使用内存时加上buffers/cache)的内存情况,也可理解为真实的内存使用情况.
  • Swap: 交换分区

当我们获取系统内存用量的时候我们应该以“-/+ buffers/cached”行的used和free作为参考.因为第一行的buffers和cached被系统作为了缓存(这里包括缓冲了metadata数据和曾经打开过的内容,是为了加快我们系统处理的速度),而这部分缓存可以根据我们的应用内存使用情况随时释放掉(也可以手动释放).

这里的话我系统可用内存实际为:可用627M,已使用368M,而不是525M和470M.

 

手动释放cache  和buffer

echo 3 > /proc/sys/vm/drop_caches
使用find 命令让buffers增加
free -h;find .>/dev/null;free -h

 

使用cat 命令让cached增加

free -h;find /mydata/backup/python/ -type f|xargs cat>/dev/null 2>&1;free -h

 

内存参数项说明:

https://www.jianshu.com/p/9edfe9d5eb34

 

 

4、slab 是为了解决内存碎片引入的,如分配一个内存快需要连续的5页,这个时候就会存在内存页碎片,称为外部碎片;

当分配一个对象,其不足一页但是需要分配一页,从而造成内存碎片,称为内部碎片;slab 就可以解决这些碎片,通过记录对象大小信息很slab 页信息解决内存碎片。

 

 

4、blockIO

https://blog.csdn.net/u014175785/article/details/103581096

 

 

5、dumpsys meminfo

 

6、dumpsys cpuinfo

while true;do date >> /sdcard/mtklog/dumpsys_meminfo.txt;dumpsys meminfo >> /sdcard/mtklog/dumpsys_meminfo.txt;dumpsys cpuinfo >>/sdcard/mtklog/dumpsys_meminfo.txt;top  -o PID,CMDLINE,%CPU,%MEM,TIME+    -s  4  -H   -d  1  -n  1  | head -n 20 >>/sdcard/mtklog/dumpsys_meminfo.txt ;sleep 2;done


 

获取帧率:

 

systrace

https://developer.android.com/studio/command-line/systrace

 

 

4、cat /proc/buddyinfo

[https://blog.csdn.net/shenhuxi_yu/article/details/72355068]

linux buddy系统管理物理内存的debug信息。

在linux中使用buddy算法解决物理内存的碎片问题,其把所有空闲的内存,以2的幂次方的形式,分成11个块链表,分别对应为1、2、4、8、16、32、64、128、256、512、1024个页块。

而Linux支持NUMA技术,对于NUMA设备,NUMA系统的结点通常是由一组CPU和本地内存组成,每一个节点都有相应的本地内存,因此buddyinfo 中的Node0表示节点ID;而每一个节点下的内存设备,又可以划分为多个内存区域(zone),因此下面的显示中,对于Node0的内存,又划分类DMA、Normal、HighMem区域,而后面则是表示空闲的区域。

此处以Normal区域进行分析,第二列值为1328,表示当前系统中normal区域,可用的连续两页的内存大小为1318*2*PAGE_SIZE;第三列值为78,表示当前系统中normal区域,可用的连续四页的内存大小为78*2^2*PAGE_SIZE

cat /proc/buddyinfo 
Node 0, zone      DMA      23      15        4        5       2       3      3      2      3      1      0 
Node 0, zone   Normal   3231   1318     78     14     14      7      5      0      0      0      0 
Node 0, zone  HighMem    133    366     37     10      7      7      2      3      0      0      0 

 

[ cat /proc/zoneinfo ]

[https://blog.csdn.net/oujunli/article/details/12855859]

可以看到只有一个node,但有两个zone:Normal和HighMem,Android一般不使用DMA,所以只有两个zone。
User space usage =  nr_inactive_anon + nr_active_anon + nr_inactive_file + nr_active_file + nr_unevictable
Kernel usage =  present -  nr_free_pages - User space usage 
分别计算出两个zone的 User space usage 和Kernel usage,然后再通过
Reserved + 3D/HW buffer = Total memory - Kernel usage - User space usage
得到Reserved + 3D/HW buffer的大小。

 

 

5、[ vmstat ]
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
11  0 292288  40520   5384 362732 7275 9336 15470   763    1 10366 47 41 12 1

 

 

一、前言

vmstat命令:  用来获得有关进程、虚存、页面交换空间及 CPU活动的信息。这些信息反映了系统的负载情况

二、虚拟内存运行原理

在系统中运行的每个进程都需要使用到内存,但不是每个进程都需要每时每刻使用系统分配的内存空间。当系统运行所需内存超过实际的物理内存,内核会释放某些进程所占用但未使用的部分或所有物理内存,将这部分资料存储在磁盘上直到进程下一次调用,并将释放出的内存提供给有需要的进程使用。

在Linux内存管理中,主要是通过“调页Paging”和“交换Swapping”来完成上述的内存调度。调页算法是将内存中最近不常使用的页面换到磁盘上,把活动页面保留在内存中供进程使用。交换技术是将整个进程,而不是部分页面,全部交换到磁盘上。

分页(Page)写入磁盘的过程被称作Page-Out,分页(Page)从磁盘重新回到内存的过程被称作Page-In。当内核需要一个分页时,但发现此分页不在物理内存中(因为已经被Page-Out了),此时就发生了分页错误(Page Fault)。

当系统内核发现可运行内存变少时,就会通过Page-Out来释放一部分物理内存。经管Page-Out不是经常发生,但是如果Page-out频繁不断的发生,直到当内核管理分页的时间超过运行程式的时间时,系统效能会急剧下降。这时的系统已经运行非常慢或进入暂停状态,这种状态亦被称作thrashing(颠簸)。

三、使用vmstat

[https://www.cnblogs.com/tommyli/p/3746187.html]

1.用法

vmstat [-a] [-n] [-S unit] [delay [ count]]
vmstat [-s] [-n] [-S unit]
vmstat [-m] [-n] [delay [ count]]
vmstat [-d] [-n] [delay [ count]]
vmstat [-p disk partition] [-n] [delay [ count]]
vmstat [-f]
vmstat [-V]

-a:显示活跃和非活跃内存

-f显示从系统启动至今的fork数量 。

-m显示slabinfo

-n只在开始时显示一次各字段名称。

-s显示内存相关统计信息及多种系统活动数量。

delay刷新时间间隔。如果不指定,只显示一条结果。

count刷新次数。如果不指定刷新次数,但指定了刷新时间间隔,这时刷新次数为无穷。

-d显示磁盘相关统计信息。

-p显示指定磁盘分区统计信息

-S使用指定单位显示。参数有 k 、K 、m 、M ,分别代表1000、1024、1000000、1048576字节(byte)。默认单位为K(1024 bytes)

-V显示vmstat版本信息。

 

2.字段含义说明:

类别

项目

含义

说明

Procs(进程)

r

等待执行的任务数

展示了正在执行和等待cpu资源的任务个数。当这个值超过了cpu个数,就会出现cpu瓶颈。

B

等待IO的进程数量

 

Memory(内存)

swpd

正在使用虚拟的内存大小,单位k

 

free

空闲内存大小

 

buff

已用的buff大小,对块设备的读写进行缓冲

 

cache

已用的cache大小,文件系统的cache

 

inact

非活跃内存大小,即被标明可回收的内存,区别于free和active

具体含义见:概念补充(当使用-a选项时显示)

active

活跃的内存大小

具体含义见:概念补充(当使用-a选项时显示)

Swap

si

每秒从交换区写入内存的大小(单位:kb/s)

 

so

每秒从内存写到交换区的大小

 

IO

bi

每秒读取的块数(读磁盘)

现在的Linux版本块的大小为1024bytes

bo

每秒写入的块数(写磁盘)

 

system

in

每秒中断数,包括时钟中断

这两个值越大,会看到由内核消耗的cpu时间会越多

cs

每秒上下文切换数

CPU(以百分比表示)

Us

用户进程执行消耗cpu时间(user time)

us的值比较高时,说明用户进程消耗的cpu时间多,但是如果长期超过50%的使用,那么我们就该考虑优化程序算法或其他措施了

Sy

系统进程消耗cpu时间(system time)

sys的值过高时,说明系统内核消耗的cpu资源多,这个不是良性的表现,我们应该检查原因。

Id

空闲时间(包括IO等待时间)

 

wa

等待IO时间

Wa过高时,说明io等待比较严重,这可能是由于磁盘大量随机访问造成的,也有可能是磁盘的带宽出现瓶颈。

 

 

5、[ cat /proc/vmallocinfo ]

[https://blog.csdn.net/shenhuxi_yu/article/details/73149444]

 

6、[ cat /proc/pagetypeinfo ]

 

 

7、

https://source.android.google.cn/devices/tech/debug/jank_jitter

https://source.android.com/devices/tech/perf/boot-times

tune bootst

https://blog.csdn.net/omnispace/article/details/73320945

 

ION

https://source.android.com/devices/tech/perf/low-ram

 

 

 

内存泄露:

例子:

https://www.jianshu.com/p/bdfd2a6b2681   //

https://www.jianshu.com/p/7fae42861b8d  //代码下载

 

mat 和android studio profiler  查看hprof  

as :

https://www.jianshu.com/p/ae74e4dee78d

mat :

https://www.zybuluo.com/frank-shaw/note/206287

 

 

zram

http://www.wowotech.net/memory_management/zram.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值