Skip to content

Commit

Permalink
Fix the 'heap_limits' test (denoland#430)
Browse files Browse the repository at this point in the history
After upgrading to V8 8.6.337, with a 20 MB heap limit, the
near-heap-limit callback never gets called before V8 runs out of memory.

It turns out that this test exhibits memory allocation behavior which
produces so little actual garbage that 'scavenge' type garbage
collections make memory usage go up rather than down. Because of this,
V8 runs out of memory in the middle of a garbage collection cycle, after
it has already decided that there's no need to run the near-heap-limit
callback.

The issue is fixed by making sure that some actual garbage is produced
alongside with the retained objects that will eventually fill up the
heap.
  • Loading branch information
piscisaureus committed Aug 6, 2020
1 parent 24fa76a commit 954163a
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions tests/test_api.rs
Expand Up @@ -3334,18 +3334,31 @@ extern "C" fn heap_limit_callback(
fn heap_limits() {
let _setup_guard = setup();

let params = v8::CreateParams::default().heap_limits(0, 20 * 1024 * 1024); // 20 MB
let params = v8::CreateParams::default().heap_limits(0, 10 << 20); // 10 MB.
let isolate = &mut v8::Isolate::new(params);

let mut test_state = TestHeapLimitState::default();
let state_ptr = &mut test_state as *mut _ as *mut c_void;
isolate.add_near_heap_limit_callback(heap_limit_callback, state_ptr);

let scope = &mut v8::HandleScope::new(isolate);
let context = v8::Context::new(scope);
let scope = &mut v8::ContextScope::new(scope, context);

// Allocate some strings; 20 MB is reached at about 800k iterations.
// Allocate JavaScript arrays until V8 calls the near-heap-limit callback.
// It takes about 100-200k iterations of this loop to get to that point.
for _ in 0..1_000_000 {
v8::String::new(scope, "HelloWorld").unwrap();
eval(
scope,
r#"
"hello 🦕 world"
.repeat(5)
.split("🦕", 2)
.map((s) => s.split(""))
.shift()
"#,
)
.unwrap();
if test_state.near_heap_limit_callback_calls > 0 {
break;
}
Expand Down

0 comments on commit 954163a

Please sign in to comment.