google / tcmalloc

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

API enhancements for ReleaseMemoryToSystem

mbautin opened this issue · comments

tcmalloc::MallocExtension::ReleaseMemoryToSystem should ideally be improved as follows:

  • It should return the number of bytes actually released to the system. Currently it just ignores the return value of MallocExtension_Internal_ReleaseMemoryToSystem:
void MallocExtension::ReleaseMemoryToSystem(size_t num_bytes) {
#if ABSL_INTERNAL_HAVE_WEAK_MALLOCEXTENSION_STUBS
  if (&MallocExtension_Internal_ReleaseMemoryToSystem != nullptr) {
    MallocExtension_Internal_ReleaseMemoryToSystem(num_bytes);
  }
#endif
}
  • There should be a parameter to avoid invoking the logic based on extra_bytes_released, and simply release the requested number of bytes.
  // ReleaseMemoryToSystem() might release more than the requested bytes because
  // the page heap releases at the span granularity, and spans are of wildly
  // different sizes.  This keeps track of the extra bytes bytes released so
  // that the app can periodically call ReleaseMemoryToSystem() to release
  // memory at a constant rate.
  ABSL_CONST_INIT static size_t extra_bytes_released;

  absl::base_internal::SpinLockHolder rh(&release_lock);

  absl::base_internal::SpinLockHolder h(&pageheap_lock);
  if (num_bytes <= extra_bytes_released) {
    // We released too much on a prior call, so don't release any
    // more this time.
    extra_bytes_released = extra_bytes_released - num_bytes;
    num_bytes = 0;
  } else {
    num_bytes = num_bytes - extra_bytes_released;
  }