erkschE
Denoβ€’16mo agoβ€’
3 replies
erksch

Embedding RustyV8: Fatal JavaScript out of memory: Reached heap limit

I created this Criterion benchmark to estimate the cost of serialization, but it runs out of memory.
Am I supposed to destroy json_v8_str and _json_obj explicitly?
let platform = v8::new_default_platform(0, false).make_shared();
v8::V8::initialize_platform(platform);
v8::V8::initialize();

let isolate = &mut v8::Isolate::new(Default::default());
let isolate_scope = &mut v8::HandleScope::new(isolate);
let context = v8::Context::new(isolate_scope, Default::default());
let scope = &mut v8::ContextScope::new(isolate_scope, context);
let global = context.global(scope);

// JSON
let json_str = r#"{"key": "value", "number": 42}"#;
let json_v8_str = v8::String::new(scope, json_str).unwrap();
let json_obj = v8::json::parse(scope, json_v8_str).unwrap();

let key = v8::String::new(scope, "key").unwrap();
let new_value = v8::String::new(scope, "newValue").unwrap();
let obj = json_obj.to_object(scope).unwrap();
obj.set(scope, key.into(), new_value.into());

let modified_json = v8::json::stringify(scope, obj.into()).unwrap();
let rust_str = modified_json.to_rust_string_lossy(scope);
assert_eq!(rust_str, r#"{"key":"newValue","number":42}"#);

c.bench_function("v8 json deserialize", |b| {
  b.iter(|| {
    let json_str = r#"{"key": "value", "number": 42}"#;
    let json_v8_str = v8::String::new(scope, json_str).unwrap();
    let _json_obj = v8::json::parse(scope, json_v8_str).unwrap();
  })
});



<--- Last few GCs --->

[417610:0xc03ef79a0000]    14221 ms: Mark-Compact (reduce) 1399.0 (1399.9) -> 1399.0 (1400.9) MB, pooled: 0 MB, 562.58 / 0.00 ms  (+ 0.0 ms in 0 steps since start of marking, biggest step 0.0 ms, walltime since start of marking 739 ms) (average mu = 0.809[417610:0xc03ef79a0000]    15192 ms: Mark-Compact 1400.0 (1400.9) -> 1400.0 (1404.9) MB, pooled: 0 MB, 969.39 / 0.00 ms  (average mu = 0.600, current mu = 0.002) allocation failure; scavenge might not succeed


<--- JS stacktrace --->



#
# Fatal JavaScript out of memory: Reached heap limit
Was this page helpful?