@@ -7770,6 +7770,7 @@ class V8_EXPORT Isolate {
77707770 friend class PersistentValueMapBase;
77717771
77727772 void ReportExternalAllocationLimitReached();
7773+ void CheckMemoryPressure();
77737774};
77747775
77757776class V8_EXPORT StartupData {
@@ -9019,6 +9020,8 @@ class Internals {
90199020 static const int kExternalMemoryOffset = 4 * kApiPointerSize;
90209021 static const int kExternalMemoryLimitOffset =
90219022 kExternalMemoryOffset + kApiInt64Size;
9023+ static const int kExternalMemoryAtLastMarkCompactOffset =
9024+ kExternalMemoryLimitOffset + kApiInt64Size;
90229025 static const int kIsolateRootsOffset = kExternalMemoryLimitOffset +
90239026 kApiInt64Size + kApiInt64Size +
90249027 kApiPointerSize + kApiPointerSize;
@@ -10244,13 +10247,28 @@ uint32_t Isolate::GetNumberOfDataSlots() {
1024410247
1024510248int64_t Isolate::AdjustAmountOfExternalAllocatedMemory(
1024610249 int64_t change_in_bytes) {
10250+ const int64_t kMemoryReducerActivationLimit = 1024 * 1024;
1024710251 typedef internal::Internals I;
1024810252 int64_t* external_memory = reinterpret_cast<int64_t*>(
1024910253 reinterpret_cast<uint8_t*>(this) + I::kExternalMemoryOffset);
1025010254 const int64_t external_memory_limit = *reinterpret_cast<int64_t*>(
1025110255 reinterpret_cast<uint8_t*>(this) + I::kExternalMemoryLimitOffset);
10256+ int64_t* external_memory_at_last_mc =
10257+ reinterpret_cast<int64_t*>(reinterpret_cast<uint8_t*>(this) +
10258+ I::kExternalMemoryAtLastMarkCompactOffset);
1025210259 const int64_t amount = *external_memory + change_in_bytes;
10260+
1025310261 *external_memory = amount;
10262+
10263+ int64_t allocation_diff_since_last_mc =
10264+ *external_memory_at_last_mc - *external_memory;
10265+ allocation_diff_since_last_mc = allocation_diff_since_last_mc < 0
10266+ ? -allocation_diff_since_last_mc
10267+ : allocation_diff_since_last_mc;
10268+ if (allocation_diff_since_last_mc > kMemoryReducerActivationLimit) {
10269+ CheckMemoryPressure();
10270+ }
10271+
1025410272 if (change_in_bytes > 0 && amount > external_memory_limit) {
1025510273 ReportExternalAllocationLimitReached();
1025610274 }
0 commit comments