// Callback for mspace_inspect_all that will madvise(2) unused pages back to // the kernel. void DlmallocMadviseCallback(void* start, void* end, size_t used_bytes, void* arg) { // Is this chunk in use? if (used_bytes != 0) { return;
} // Do we have any whole pages to give back?
start = reinterpret_cast<void*>(art::RoundUp(reinterpret_cast<uintptr_t>(start), art::gPageSize));
end = reinterpret_cast<void*>(art::RoundDown(reinterpret_cast<uintptr_t>(end), art::gPageSize)); if (end > start) {
size_t length = reinterpret_cast<uint8_t*>(end) - reinterpret_cast<uint8_t*>(start); int rc = madvise(start, length, MADV_DONTNEED); if (UNLIKELY(rc != 0)) {
errno = rc;
PLOG(FATAL) << "madvise failed during heap trimming";
}
size_t* reclaimed = reinterpret_cast<size_t*>(arg);
*reclaimed += length;
}
}
// Callback for mspace_inspect_all that will count the number of bytes // allocated. void DlmallocBytesAllocatedCallback([[maybe_unused]] void* start,
[[maybe_unused]] void* end,
size_t used_bytes, void* arg) { if (used_bytes == 0) { return;
}
size_t* bytes_allocated = reinterpret_cast<size_t*>(arg);
*bytes_allocated += used_bytes + sizeof(size_t);
}
// Callback for mspace_inspect_all that will count the number of objects // allocated. void DlmallocObjectsAllocatedCallback([[maybe_unused]] void* start,
[[maybe_unused]] void* end,
size_t used_bytes, void* arg) { if (used_bytes == 0) { return;
}
size_t* objects_allocated = reinterpret_cast<size_t*>(arg);
++(*objects_allocated);
}
// Memory we promise to dlmalloc before it asks for morecore. // Note: making this value large means that large allocations are unlikely to succeed as dlmalloc // will ask for this memory from sys_alloc which will fail as the footprint (this value plus the // size of the large allocation) will be greater than the footprint limit.
size_t starting_size = gPageSize;
MemMap mem_map = CreateMemMap(name, starting_size, &initial_size, &growth_limit, &capacity); if (!mem_map.IsValid()) {
LOG(ERROR) << "Failed to create mem map for alloc space (" << name << ") of size "
<< PrettySize(capacity); return nullptr;
}
DlMallocSpace* space = CreateFromMemMap(std::move(mem_map),
name,
starting_size,
initial_size,
growth_limit,
capacity,
can_move_objects); // We start out with only the initial size possibly containing objects. if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
LOG(INFO) << "DlMallocSpace::Create exiting (" << PrettyDuration(NanoTime() - start_time)
<< " ) " << *space;
} return space;
}
void* DlMallocSpace::CreateMspace(void* begin, size_t morecore_start, size_t initial_size) { // clear errno to allow PLOG on error
errno = 0; // create mspace using our backing storage starting at begin and with a footprint of // morecore_start. Don't use an internal dlmalloc lock (as we already hold heap lock). When // morecore_start bytes of memory is exhaused morecore will be called. void* msp = create_mspace_with_base(begin, morecore_start, 0/*locked*/); if (msp != nullptr) { // Do not allow morecore requests to succeed beyond the initial size of the heap
mspace_set_footprint_limit(msp, initial_size);
} else {
PLOG(ERROR) << "create_mspace_with_base failed";
} return msp;
}
mirror::Object* DlMallocSpace::AllocWithGrowth(Thread* self, size_t num_bytes,
size_t* bytes_allocated, size_t* usable_size,
size_t* bytes_tl_bulk_allocated) {
mirror::Object* result;
{
MutexLock mu(self, lock_); // Grow as much as possible within the space.
size_t max_allowed = Capacity();
mspace_set_footprint_limit(mspace_, max_allowed); // Try the allocation.
result = AllocWithoutGrowthLocked(self, num_bytes, bytes_allocated, usable_size,
bytes_tl_bulk_allocated); // Shrink back down as small as possible.
size_t footprint = mspace_footprint(mspace_);
mspace_set_footprint_limit(mspace_, footprint);
} if (result != nullptr) { // Zero freshly allocated memory, done while not holding the space's lock.
memset(result, 0, num_bytes); // Check that the result is contained in the space.
CHECK_IMPLIES(kDebugSpaces, Contains(result));
} return result;
}
// Don't need the lock to calculate the size of the freed pointers.
size_t bytes_freed = 0; for (size_t i = 0; i < num_ptrs; i++) {
mirror::Object* ptr = ptrs[i]; const size_t look_ahead = 8; if (kPrefetchDuringDlMallocFreeList && i + look_ahead < num_ptrs) { // The head of chunk for the allocation is sizeof(size_t) behind the allocation.
__builtin_prefetch(reinterpret_cast<char*>(ptrs[i + look_ahead]) - sizeof(size_t));
}
bytes_freed += AllocationSizeNonvirtual(ptr, nullptr);
}
if (kRecentFreeCount > 0) {
MutexLock mu(self, lock_); for (size_t i = 0; i < num_ptrs; i++) {
RegisterRecentFree(ptrs[i]);
}
}
if (kDebugSpaces) {
size_t num_broken_ptrs = 0; for (size_t i = 0; i < num_ptrs; i++) { if (!Contains(ptrs[i])) {
num_broken_ptrs++;
LOG(ERROR) << "FreeList[" << i << "] (" << ptrs[i] << ") not in bounds of heap " << *this;
} else {
size_t size = mspace_usable_size(ptrs[i]);
memset(ptrs[i], 0xEF, size);
}
}
CHECK_EQ(num_broken_ptrs, 0u);
}
size_t DlMallocSpace::Trim() {
MutexLock mu(Thread::Current(), lock_); // Trim to release memory at the end of the space.
mspace_trim(mspace_, 0); // Visit space looking for page-sized holes to advise the kernel we don't need.
size_t reclaimed = 0;
mspace_inspect_all(mspace_, DlmallocMadviseCallback, &reclaimed); return reclaimed;
}
void DlMallocSpace::Walk(void(*callback)(void *start, void *end, size_t num_bytes, void* callback_arg), void* arg) {
MutexLock mu(Thread::Current(), lock_);
mspace_inspect_all(mspace_, callback, arg);
callback(nullptr, nullptr, 0, arg); // Indicate end of a space.
}
void DlMallocSpace::SetFootprintLimit(size_t new_size) {
MutexLock mu(Thread::Current(), lock_);
VLOG(heap) << "DlMallocSpace::SetFootprintLimit " << PrettySize(new_size); // Compare against the actual footprint, rather than the Size(), because the heap may not have // grown all the way to the allowed size yet.
size_t current_space_size = mspace_footprint(mspace_); if (new_size < current_space_size) { // Don't let the space grow any more.
new_size = current_space_size;
}
mspace_set_footprint_limit(mspace_, new_size);
}
bool DlMallocSpace::LogFragmentationAllocFailure(std::ostream& os,
size_t failed_alloc_bytes) {
Thread* const self = Thread::Current();
MspaceCbArgs mspace_cb_args = {0, 0}; // To allow the Walk/InspectAll() to exclusively-lock the mutator // lock, temporarily release the shared access to the mutator // lock here by transitioning to the suspended state.
Locks::mutator_lock_->AssertSharedHeld(self);
ScopedThreadSuspension sts(self, ThreadState::kSuspended);
Walk(MSpaceChunkCallback, &mspace_cb_args); if (failed_alloc_bytes > mspace_cb_args.max_contiguous) {
os << "; failed due to malloc_space fragmentation (largest possible contiguous allocation "
<< mspace_cb_args.max_contiguous << " bytes, space in use " << mspace_cb_args.used
<< " bytes, capacity = " << Capacity() << ")"; returntrue;
} returnfalse;
}
} // namespace space
namespace allocator {
// Implement the dlmalloc morecore callback. void* ArtDlMallocMoreCore(void* mspace, intptr_t increment) REQUIRES_SHARED(Locks::mutator_lock_) {
Runtime* runtime = Runtime::Current();
Heap* heap = runtime->GetHeap();
::art::gc::space::DlMallocSpace* dlmalloc_space = heap->GetDlMallocSpace(); // Support for multiple DlMalloc provided by a slow path. if (UNLIKELY(dlmalloc_space == nullptr || dlmalloc_space->GetMspace() != mspace)) { if (LIKELY(runtime->GetJitCodeCache() != nullptr)) {
jit::JitCodeCache* code_cache = runtime->GetJitCodeCache(); if (code_cache->OwnsSpace(mspace)) { return code_cache->MoreCore(mspace, increment);
}
}
dlmalloc_space = nullptr; for (space::ContinuousSpace* space : heap->GetContinuousSpaces()) { if (space->IsDlMallocSpace()) {
::art::gc::space::DlMallocSpace* cur_dlmalloc_space = space->AsDlMallocSpace(); if (cur_dlmalloc_space->GetMspace() == mspace) {
dlmalloc_space = cur_dlmalloc_space; break;
}
}
}
CHECK(dlmalloc_space != nullptr) << "Couldn't find DlmMallocSpace with mspace=" << mspace;
} return dlmalloc_space->MoreCore(increment);
}
} // namespace allocator
} // namespace gc
} // namespace art
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet am 2026-06-29)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.