diff --git a/src/Common/MemoryTracker.cpp b/src/Common/MemoryTracker.cpp index 03bd8be94f3e2a5665d30753247bac3274fd45c9..ca21cec0212e89125df2e6efbb7a679eee28f742 100644 --- a/src/Common/MemoryTracker.cpp +++ b/src/Common/MemoryTracker.cpp @@ -77,6 +77,21 @@ void MemoryTracker::alloc(Int64 size) Int64 current_hard_limit = hard_limit.load(std::memory_order_relaxed); Int64 current_profiler_limit = profiler_limit.load(std::memory_order_relaxed); + /// Cap the limit to the total_memory_tracker, since it may include some drift. + /// + /// And since total_memory_tracker is reseted to the process resident + /// memory peridically (in AsynchronousMetrics::update()), any limit can be + /// capped to it, to avoid possible drift. + if (unlikely(current_hard_limit && will_be > current_hard_limit)) + { + Int64 total_amount = total_memory_tracker.get(); + if (amount > total_amount) + { + set(total_amount); + will_be = size + total_amount; + } + } + std::bernoulli_distribution fault(fault_probability); if (unlikely(fault_probability && fault(thread_local_rng))) {