@@ -720,6 +720,9 @@ Environment::Environment(IsolateData* isolate_data,
720720 inspector_host_port_ = std::make_shared<ExclusiveAccess<HostPort>>(
721721 options_->debug_options ().host_port );
722722
723+ heap_snapshot_near_heap_limit_ =
724+ static_cast <uint32_t >(options_->heap_snapshot_near_heap_limit );
725+
723726 if (!(flags_ & EnvironmentFlags::kOwnsProcessState )) {
724727 set_abort_on_uncaught_exception (false );
725728 }
@@ -834,9 +837,8 @@ Environment::~Environment() {
834837 // FreeEnvironment() should have set this.
835838 CHECK (is_stopping ());
836839
837- if (options_->heap_snapshot_near_heap_limit > heap_limit_snapshot_taken_) {
838- isolate_->RemoveNearHeapLimitCallback (Environment::NearHeapLimitCallback,
839- 0 );
840+ if (heapsnapshot_near_heap_limit_callback_added_) {
841+ remove_heapsnapshot_near_heap_limit_callback (0 );
840842 }
841843
842844 isolate ()->GetHeapProfiler ()->RemoveBuildEmbedderGraphCallback (
@@ -1952,8 +1954,7 @@ size_t Environment::NearHeapLimitCallback(void* data,
19521954 Debug (env,
19531955 DebugCategory::DIAGNOSTICS,
19541956 " Not generating snapshots because it's too risky.\n " );
1955- env->isolate ()->RemoveNearHeapLimitCallback (NearHeapLimitCallback,
1956- initial_heap_limit);
1957+ env->remove_heapsnapshot_near_heap_limit_callback (initial_heap_limit);
19571958 // The new limit must be higher than current_heap_limit or V8 might
19581959 // crash.
19591960 return current_heap_limit + 1 ;
@@ -1973,17 +1974,15 @@ size_t Environment::NearHeapLimitCallback(void* data,
19731974
19741975 // Remove the callback first in case it's triggered when generating
19751976 // the snapshot.
1976- env->isolate ()->RemoveNearHeapLimitCallback (NearHeapLimitCallback,
1977- initial_heap_limit);
1977+ env->remove_heapsnapshot_near_heap_limit_callback (initial_heap_limit);
19781978
19791979 heap::WriteSnapshot (env, filename.c_str ());
19801980 env->heap_limit_snapshot_taken_ += 1 ;
19811981
19821982 // Don't take more snapshots than the number specified by
19831983 // --heapsnapshot-near-heap-limit.
1984- if (env->heap_limit_snapshot_taken_ <
1985- env->options_ ->heap_snapshot_near_heap_limit ) {
1986- env->isolate ()->AddNearHeapLimitCallback (NearHeapLimitCallback, env);
1984+ if (env->heap_limit_snapshot_taken_ < env->heap_snapshot_near_heap_limit_ ) {
1985+ env->add_heapsnapshot_near_heap_limit_callback ();
19871986 }
19881987
19891988 FPrintF (stderr, " Wrote snapshot to %s\n " , filename.c_str ());
0 commit comments