maxprocessmemory.memoryUsage这几个参数都是什么意思

JavaScript中内存泄漏的介绍与教程(推荐)
转载 & & 作者:阮一峰
内存泄露是指一块被分配的内存既不能使用,又不能回收,直到浏览器进程结束。下面这篇文章主要给的大家介绍了关于JavaScript中内存泄漏的相关资料,文中介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
本文主要给大家详细介绍了关于JavaScript中内存泄漏的相关内容,文中介绍的非常详细,对大家具有一定的参考学习价值,下面来一起看看详细的介绍:
一、什么是内存泄漏?
程序的运行需要内存。只要程序提出要求,操作系统或者运行时(runtime)就必须供给内存。
对于持续运行的服务进程(daemon),必须及时释放不再用到的内存。否则,内存占用越来越高,轻则影响系统性能,重则导致进程崩溃。
不再用到的内存,没有及时释放,就叫做内存泄漏(memory leak)。
有些语言(比如 C 语言)必须手动释放内存,程序员负责内存管理。
buffer = (char*) malloc(42);
// Do something with buffer
free(buffer);
上面是 C 语言代码,malloc方法用来申请内存,使用完毕之后,必须自己用free方法释放内存。
这很麻烦,所以大多数语言提供自动内存管理,减轻程序员的负担,这被称为"垃圾回收机制"(garbage collector)。
二、垃圾回收机制
垃圾回收机制怎么知道,哪些内存不再需要呢?
最常使用的方法叫做""(reference counting):语言引擎有一张"引用表",保存了内存里面所有的资源(通常是各种值)的引用次数。如果一个值的引用次数是0,就表示这个值不再用到了,因此可以将这块内存释放。
上图中,左下角的两个值,没有任何引用,所以可以释放。
如果一个值不再需要了,引用数却不为0,垃圾回收机制无法释放这块内存,从而导致内存泄漏。
const arr = [1, 2, 3, 4];
console.log('hello world');
上面代码中,数组[1, 2, 3, 4]是一个值,会占用内存。变量arr是仅有的对这个值的引用,因此引用次数为1。尽管后面的代码没有用到arr,它还是会持续占用内存。
如果增加一行代码,解除arr对[1, 2, 3, 4]引用,这块内存就可以被垃圾回收机制释放了。
let arr = [1, 2, 3, 4];
console.log('hello world');
上面代码中,arr重置为null,就解除了对[1, 2, 3, 4]的引用,引用次数变成了0,内存就可以释放出来了。
因此,并不是说有了垃圾回收机制,程序员就轻松了。你还是需要关注内存占用:那些很占空间的值,一旦不再用到,你必须检查是否还存在对它们的引用。如果是的话,就必须手动解除引用。
三、内存泄漏的识别方法
怎样可以观察到内存泄漏呢?
是,如果连续五次垃圾回收之后,内存占用一次比一次大,就有内存泄漏。这就要求实时查看内存占用。
3.1 浏览器
Chrome 浏览器查看内存占用,按照以下步骤操作。
打开开发者工具,选择 Timeline 面板
在顶部的Capture字段里面勾选 Memory
点击左上角的录制按钮。
在页面上进行各种操作,模拟用户的使用情况。
一段时间后,点击对话框的 stop 按钮,面板上就会显示这段时间的内存占用情况。
如果内存占用基本平稳,接近水平,就说明不存在内存泄漏。
反之,就是内存泄漏了。
3.2 命令行
命令行可以使用 Node 提供的方法。
console.log(process.memoryUsage());
// { rss: ,
// heapTotal: 5685248,
// heapUsed: 3449392,
// external: 8772 }
process.memoryUsage返回一个对象,包含了 Node 进程的内存占用信息。该对象包含四个字段,单位是字节,如下。
rss(resident set size):所有内存占用,包括指令区和堆栈。
heapTotal:"堆"占用的内存,包括用到的和没用到的。
heapUsed:用到的堆的部分。
external: V8 引擎内部的 C++ 对象占用的内存。
判断内存泄漏,以heapUsed字段为准。
四、WeakMap
前面说过,及时清除引用非常重要。但是,你不可能记得那么多,有时候一疏忽就忘了,所以才有那么多内存泄漏。
最好能有一种方法,在新建引用的时候就声明,哪些引用必须手动清除,哪些引用可以忽略不计,当其他引用消失以后,垃圾回收机制就可以释放内存。这样就能大大减轻程序员的负担,你只要清除主要引用就可以了。
ES6 考虑到了这一点,推出了两种新的数据结构:和 。它们对于值的引用都是不计入垃圾回收机制的,所以名字里面才会有一个"Weak",表示这是弱引用。
下面以 WeakMap 为例,看看它是怎么解决内存泄漏的。
const wm = new WeakMap();
const element = document.getElementById('example');
wm.set(element, 'some information');
wm.get(element) // "some information"
上面代码中,先新建一个 Weakmap 实例。然后,将一个 DOM 节点作为键名存入该实例,并将一些附加信息作为键值,一起存放在 WeakMap 里面。这时,WeakMap 里面对element的引用就是弱引用,不会被计入垃圾回收机制。
也就是说,DOM 节点对象的引用计数是1,而不是2。这时,一旦消除对该节点的引用,它占用的内存就会被垃圾回收机制释放。Weakmap 保存的这个键值对,也会自动消失。
基本上,如果你要往对象上添加数据,又不想干扰垃圾回收机制,就可以使用 WeakMap。
五、WeakMap 示例
WeakMap 的例子很难演示,因为无法观察它里面的引用会自动消失。此时,其他引用都解除了,已经没有引用指向 WeakMap 的键名了,导致无法证实那个键名是不是存在。
我一直想不出办法,直到有一天贺师俊老师,如果引用所指向的值占用特别多的内存,就可以通过process.memoryUsage方法看出来。
根据这个思路,网友 vtxf 补充了下面的。
首先,打开 Node 命令行。
$ node --expose-gc
上面代码中,--expose-gc参数表示允许手动执行垃圾回收机制。
然后,执行下面的代码。
// 手动执行一次垃圾回收,保证获取的内存使用状态准确
& global.gc();
// 查看内存占用的初始状态,heapUsed 为 4M 左右
& process.memoryUsage();
heapTotal: 7376896,
heapUsed: 4153936,
external: 9059 }
& let wm = new WeakMap();
& let b = new Object();
& global.gc();
// 此时,heapUsed 仍然为 4M 左右
& process.memoryUsage();
heapTotal: 9474048,
heapUsed: 3967272,
external: 8993 }
// 在 WeakMap 中添加一个键值对,
// 键名为对象 b,键值为一个 5* 的数组
& wm.set(b, new Array(5*));
WeakMap {}
// 手动执行一次垃圾回收
& global.gc();
// 此时,heapUsed 为 45M 左右
& process.memoryUsage();
heapTotal: ,
heapUsed: ,
external: 8951 }
// 解除对象 b 的引用
// 再次执行垃圾回收
& global.gc();
// 解除 b 的引用以后,heapUsed 变回 4M 左右
// 说明 WeakMap 中的那个长度为 5* 的数组被销毁了
& process.memoryUsage();
heapTotal: 8425472,
heapUsed: 3979792,
external: 8956 }
上面代码中,只要外部的引用消失,WeakMap 内部的引用,就会自动被垃圾回收清除。由此可见,有了它的帮助,解决内存泄漏就会简单很多。
六、参考链接
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具Node.js 全局对象_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
Node.js 全局对象
W3Cschool(www.w3cschool.cn)最大的技术知...|
总评分0.0|
阅读已结束,下载本文需要
想免费下载更多文档?
定制HR最喜欢的简历
下载文档到电脑,方便使用
还剩1页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢process.memoryUsage()这几个参数都是什么意思 - CNode技术社区
这家伙很懒,什么个性签名都没有留下。
如题:这几个参数都是什么意思,{ rss: , heapTotal: , heapUsed: 5316664 }这些数字的单位是什么, kb 么?
The process.memoryUsage() method returns an object describing the memory usage of the Node.js process measured in bytes.
rss: 进程常驻内存
heapTotal:已申请的堆内存
heapUsed:已使用的量
单位都是字节
CNode 社区为国内最专业的 Node.js 开源技术社区,致力于 Node.js 的技术研究。
服务器赞助商为
,存储赞助商为
,由提供应用性能服务。
新手搭建 Node.js 服务器,推荐使用无需备案的Process memory usage
svmon-P command to display the system all processes currently running the memory usage statistics.
The following is an example of svmon-P command:
# svmon -P
--------------------------------------------------------------------------------
Pid Command
Virtual 64-bit Mthrd 16MB
16264 IBM.ServiceRM
Esid Type Description
Pin Pgsp Virtual
d work shared library text
0 work kernel seg
2 work process private
f work shared library data
1 pers code,/dev/hd2:149841
- pers /dev/hd2:71733
4 work shared memory segment
- pers large file /dev/hd9var:243
3 mmap mapped to sid a03f4
- pers large file /dev/hd9var:247
--------------------------------------------------------------------------------
Pid Command
Virtual 64-bit Mthrd 16MB
17032 IBM.CSMAgentR
Esid Type Description
Pin Pgsp Virtual
d work shared library text
0 work kernel seg
2 work process private
f work shared library data
1 pers code,/dev/hd2:149840
- pers /dev/hd2:71733
- pers /dev/hd2:284985
- pers large file /dev/hd9var:186
- pers large file /dev/hd9var:204
3 mmap mapped to sid 5840b
detailed description of the command output of the overall memory usage of each process, as well as the process used in each report for each segment of the memory usage details. The default collation is the page count in descending order according to
Run svmon command with-u,-p,-g or-v flag, one can change the collation.
To get the system before using the memory process 15 Summary, please use the following command:
# svmon -Pt15 | perl -e 'while(&&){print if($.==2||$&&&!$s++);$.=0 if(/^-+$/)}'
--------------------------------------------------------------------------------
Pid Command
Virtual 64-bit Mthrd 16MB
16264 IBM.ServiceRM
17032 IBM.CSMAgentR
13684 getty
26590 perl5.8.0
7514 sendmail
14968 rmcd
4164 errdemon
11424 rpc.mountd
21564 rlogind
26704 rlogind
Pid 16264 is the maximum memory consumption of the process of identification. Command pointed out that the command name, in this case IBM (R). ServiceRM. Inuse column shows the 10 075, the column is used in the process section of the total number of pages in real memory. Each page size is 4 KB. Pin column shows the 3345, as the process used by the segment of the fixed total number of pages. Pgsp column shows the 3064, the column is the process used by the total number of paging space page. Virtual column (process virtual space in the total number of pages) Show 13 310.
Details of the area shows a summary of each process area to display information for each section. This includes virtual
and effective
section is marked.
reflect the corresponding page of the section used to access the register. Section also shows the type and description, the description text for the paragraph description, including the volume name and permanent section of the file inode. The report also details the support section of the page size (4 KB pages in which
MB pages), RAM Pages
RAM in a fixed number of pages
paging space pages
and the virtual pages
You can even use more options to get more details. -J option displays the file path to permanent section. -L option provides more detailed information on the section,-r option displays the scope of each section of memory used. The following is with-l,-r and-j option svmon command example:
# svmon -S f001e 400 e83dd -l -r -j
Esid Type Description
Pin Pgsp Virtual
d work shared library text
Addr Range: 0..60123
Shared library text segment
2 work process private
Addr Range: 0..969 : 6
pid(s)=17032
- pers /dev/hd2:71733
/usr/lib/nls/loc/uconvTable/ISO8859-1
Addr Range: 0..1
pid(s)=1, 1, 1
Address Range for the permanent section or paragraph to specify a range of client or for the work specified in paragraph two areas. Permanent section or paragraph of the scope of the client with a '0 .. x 'in the form that has been used where x is the maximum number of virtual pages. The scope of work section of the field can be '0 .. x: y. .65535 ', where 0 .. x contains global data, will increase, and y. .65535 contains stack areas will be reduced. For a working segment of the address range is to allocate space from the beginning until the middle of both sides. If the working section of a non-proprietary (kernel or shared library), space allocation is different.
In the example above, segment-specific work is identified in paragraph 400; its address range is 0 .. 969: 65305 .. 65535. Section logo f001e is shared library
its address range is 0 .. 60123.
A section of the process used by many. Each of such paragraph in a real memory page explains the process for each use that section of the Inuse field. Therefore, Inuse the total may exceed total number of pages in real memory. This Pgsp and Pin fields apply. Summary section shows the value from the process used by all segments of the Inuse, Pin, Pgsp and
counter of the total composition.
In the example
e83dd section used by a number of processes, these processes PID is ,,14968 and 9620.
Please enable JavaScript to view the
PL / SQL DEVELOPER basic Detailed description (proposal written after the first stored procedure, the initial hand must-read) Used the oracle of all complaints, in order to stabilize its operation to provide graphical slow sad ah, p4 +128 M to start
QString Detailed description QString The reason why QString out alone, is very common Because string is a data structure, and even in many languages, such as JavaScript, Du Shiba int string Zuowei one kind of the same basic data structures such as Yi
Detailed description of regular expression JScript www.diybl.com : Author: Anonymous Editor: Site hits: 198 [review] Article is wrong, I want to error to the Forum, the discussion to comment on this article, download the video into the VIP
Call the function module: CALL FUNCTION \ 'REUSE_ALV_GRID_DISPLAY \' EXPORTING i_interface_check = \ '\' Interface consistency checking i_callback_program = sy-repid the current program name is_layout = layout style output it_fieldcat = fieldcat [] f
hibernate mainly from the following aspects to optimize the query performance: 1, reducing the frequency of access to the database, reducing the number of select statements, means of achieving are: left outer join with an urgent or pressing the conne
A more detailed description of DOS commands !
13:00 A more detailed description of DOS commands ! 2007 In August 30 Thursday PM 09:11 Article from :cfan, Read . 1 echo And @ Echo commands @ # Off-line echo echo off # Starting from the next
A more detailed description of the DOS command !
13:00 A more detailed description of the DOS command ! 2007 August 30 Thursday Afternoon 09:11 Article from :cfan, View original . 1 echo And @ Echo command @ # Off-line echo echo off # Star
: The top command is commonly used under Linux performance analysis tools, it can display real-time system resource usage status of each process to know what processes take up much system resources, like under Windows Task Manager. The following deta
linux curl Detailed description curl are applied, one can directly through the command-line tools, and the other is to use the libcurl library to do the top of the development. In this chapter summarize the http command-line tools related application
① ntsd command description c: \& ntsd-cq-p PID Only System, SMSS.EXE and CSRSS.EXE can not kill. The first two are pure kernel mode, and the last that is the Win32 subsystem, ntsd themselves need it. ntsd beginning from 2000, the system comes with us
Copyright (C) , All Rights Reserved.
版权所有 闽ICP备号
processed in 0.039 (s). 13 q(s)

我要回帖

更多关于 process.memoryusage 的文章

 

随机推荐