Triggering a minor GC cycle (aka scavenge)

56 views
Skip to first unread message

Sal Fuentes

unread,
Nov 20, 2015, 9:28:24 PM11/20/15
to v8-users
I posted this question on stackoverflow (http://stackoverflow.com/questions/33780781/when-does-the-v8-gc-trigger-its-first-minor-gc-cycle-scavenge) but I think I should have first posted it here. In any case, I'm confused as to why V8 triggers 4 scavenges when I have only allocated one MB and have explicitly set the min, max, and target new space to one MB. Now, I can imagine that v8 might startup with some 'hidden' objects already on the heap which might result in one scavenge being triggered before the user code allocates a full MB, but it should still not result in 4 scavenges. Either my math is wrong, or I'm missing something.

Here is the sample code that results in 4 GC cycles on the new space:
var BYTES_IN_MB = 1024 * 1024; // 1MB = 1024KB = 1024(KB/MB)*1024(B/KB)
var BYTES_IN_SMI = 4;
var NUM_SMIS_IN_MB = BYTES_IN_MB/BYTES_IN_SMI;

var y = [];
function allocateMB() {
  for(var i = 0; i < NUM_SMIS_IN_MB; i++) {
    y.push(i);
  }
}

allocateMB();


Here is the execution:

➜  ~  d8
V8 version 4.5.103.35 [console: readline]
➜  ~  d8 --trace_gc --trace_gc_verbose  --min_semi_space_size=1 --max_semi_space_size=1 --target_semi_space_size=1 --semi_space_growth_factor=0 allocateMB.js
[2853:0x7fa1fa012600]        6 ms: Scavenge 1.7 (36.9) -> 1.2 (36.9) MB, 0.3 / 0 ms [allocation failure].
[2853:0x7fa1fa012600] Memory allocator,   used:  37752 KB, available: 1461384 KB
[2853:0x7fa1fa012600] New space,          used:    281 KB, available:    726 KB, committed:   2015 KB
[2853:0x7fa1fa012600] Old space,          used:    674 KB, available:      0 KB, committed:    794 KB
[2853:0x7fa1fa012600] Code space,         used:    194 KB, available:      0 KB, committed:    261 KB
[2853:0x7fa1fa012600] Map space,          used:     34 KB, available:      0 KB, committed:     59 KB
[2853:0x7fa1fa012600] Large object space, used:      0 KB, available: 1460343 KB, committed:      0 KB
[2853:0x7fa1fa012600] All spaces,         used:   1185 KB, available: 1461069 KB, committed:   3130 KB
[2853:0x7fa1fa012600] External memory reported:      0 KB
[2853:0x7fa1fa012600] Total time spent in GC  : 0.3 ms
[2853:0x7fa1fa012600]        7 ms: Scavenge 1.5 (36.9) -> 1.3 (36.9) MB, 0.4 / 0 ms [allocation failure].
[2853:0x7fa1fa012600] Memory allocator,   used:  37752 KB, available: 1461384 KB
[2853:0x7fa1fa012600] New space,          used:    397 KB, available:    610 KB, committed:   2015 KB
[2853:0x7fa1fa012600] Old space,          used:    691 KB, available:      0 KB, committed:    794 KB
[2853:0x7fa1fa012600] Code space,         used:    194 KB, available:      0 KB, committed:    261 KB
[2853:0x7fa1fa012600] Map space,          used:     34 KB, available:      0 KB, committed:     59 KB
[2853:0x7fa1fa012600] Large object space, used:      0 KB, available: 1460343 KB, committed:      0 KB
[2853:0x7fa1fa012600] All spaces,         used:   1317 KB, available: 1460954 KB, committed:   3130 KB
[2853:0x7fa1fa012600] External memory reported:      0 KB
[2853:0x7fa1fa012600] Total time spent in GC  : 0.7 ms
[2853:0x7fa1fa012600]        8 ms: Scavenge 1.9 (36.9) -> 1.5 (36.9) MB, 0.2 / 0 ms [allocation failure].
[2853:0x7fa1fa012600] Memory allocator,   used:  37752 KB, available: 1461384 KB
[2853:0x7fa1fa012600] New space,          used:    596 KB, available:    411 KB, committed:   2015 KB
[2853:0x7fa1fa012600] Old space,          used:    691 KB, available:      0 KB, committed:    794 KB
[2853:0x7fa1fa012600] Code space,         used:    194 KB, available:      0 KB, committed:    261 KB
[2853:0x7fa1fa012600] Map space,          used:     34 KB, available:      0 KB, committed:     59 KB
[2853:0x7fa1fa012600] Large object space, used:      0 KB, available: 1460343 KB, committed:      0 KB
[2853:0x7fa1fa012600] All spaces,         used:   1516 KB, available: 1460755 KB, committed:   3130 KB
[2853:0x7fa1fa012600] External memory reported:      0 KB
[2853:0x7fa1fa012600] Total time spent in GC  : 0.9 ms
[2853] Limited new space size due to high promotion rate: 1 MB
[2853:0x7fa1fa012600]        8 ms: Scavenge 1.5 (36.9) -> 1.5 (37.9) MB, 0.7 / 0 ms [allocation failure].
[2853:0x7fa1fa012600] Memory allocator,   used:  38776 KB, available: 1460360 KB
[2853:0x7fa1fa012600] New space,          used:      0 KB, available:   1007 KB, committed:   2015 KB
[2853:0x7fa1fa012600] Old space,          used:   1287 KB, available:    102 KB, committed:   1801 KB
[2853:0x7fa1fa012600] Code space,         used:    194 KB, available:      0 KB, committed:    261 KB
[2853:0x7fa1fa012600] Map space,          used:     34 KB, available:      0 KB, committed:     59 KB
[2853:0x7fa1fa012600] Large object space, used:      0 KB, available: 1459319 KB, committed:      0 KB
[2853:0x7fa1fa012600] All spaces,         used:   1516 KB, available: 1460430 KB, committed:   4138 KB
[2853:0x7fa1fa012600] External memory reported:      0 KB
[2853:0x7fa1fa012600] Total time spent in GC  : 1.6 ms

Jakob Kummerow

unread,
Nov 21, 2015, 6:12:36 AM11/21/15
to v8-users
Firstly, I can't reproduce the behavior you're observing: when I run your command line, I only see two scavenges.

Also:
- you're running a 64bit build; Smis on x64 use 64 bits just like every other pointer
- storing a Smi into an existing array backing store doesn't allocate anything
- what do you think how growing an array one element at a time from size 0 to size N works under the hood?
- not all allocations go to new space
- in general, accurately predicting how many bytes will be allocated by some complex operation is somewhere between "extremely hard" and "outright impossible" on any advanced VM, because such VMs employ all kinds of tricks and heuristics (that may change as the program runs longer, or with different versions of the VM -- the build I happened to have around is much younger than 4.5.103).

--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups "v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages