Massive requests + long-running memory control-all resources efficient recycling
1. V8 garbage collection mechanism and memory limit
When using memory with JavaScript in node, only part of the memory is used (approximately 0.7GB under the 64-bit system under the 1.4GB,32 bit system).
In V8, all JavaScript objects are allocated through the heap, and when the requested heap free memory is not sufficient to allocate new objects, the heap memory will continue to be requested until the heap size exceeds the limit of V8.
Why V8 to limit heap size?
(1) V8 was originally designed for browsers and is unlikely to encounter scenes with lots of memory.
(2) V8 garbage collection mechanism limitation. (1.5GB of garbage-collected heap memory, which takes about 50 milliseconds, which leads to a straight down in application performance and responsiveness)
Breakthrough V8 Limit:
$node--max-old-space-size = 1700 (in megabytes) or
$node--max-new-space-size = 1024 (in kilobytes)
Garbage collection mechanism of V8----The mechanism of generational garbage collection
Review the garbage collection mechanism----$node--TRACE_GC-E "var a = [];for (var i=0; i<1000000; i++) A.push (new Array); "> Gc.log
2. Efficient Use of memory
(1) Understanding scopes
(2) Understanding closures
The memory that cannot be reclaimed immediately is:
Closure Package
Global variable Reference
Therefore, be careful about the unrestricted addition of such variables
3. Memory Metrics
-There are some objects that we think will be recycled but not recycled, which can lead to an unlimited increase in memory usage. Once the growth reaches the V8 memory limit, a memory overflow error is obtained, which in turn causes the process to exit.
(1) View memory usage $node >process.memoryusage () {
{
RSS (resident the resident memory portion of the set size process): 132852672, (except in addition, the rest of the memory is in the Swap area (swap) or the file system (filesystem))
Heaptotal (total amount of memory requested in the heap): 6131200,
Heapused (the amount of memory currently in use in the heap): 2757120,
}
(2) Viewing system memory consumption
$node >os.totalmem () "Total memory of the system"
$node >os.freemem () "Idle memory of the system"
(3) out-of-stack memory
The garbage collection restricted by V8 is primarily V8 heap memory.
4. Memory leaks
Causes of memory leaks:
(1) Cache
(2) Queue consumption is not timely
(3) Scope not released
5. Troubleshooting Memory leaks
Use common tools:
(1) Node-heapdump
(2) Node-memwatch
6. Large Memory applications
"Reading Notes", "Nodejs" fifth chapter memory control