void ReferenceTable::Remove(ObjPtr<mirror::Object> obj) { // We iterate backwards on the assumption that references are LIFO. for (int i = entries_.size() - 1; i >= 0; --i) {
ObjPtr<mirror::Object> entry = entries_[i].Read(); if (entry == obj) {
entries_.erase(entries_.begin() + i); return;
}
}
}
// If "obj" is an array, return the number of elements in the array. // Otherwise, return zero. static size_t GetElementCount(ObjPtr<mirror::Object> obj) REQUIRES_SHARED(Locks::mutator_lock_) { // We assume the special cleared value isn't an array in the if statement below.
DCHECK(!Runtime::Current()->GetClearedJniWeakGlobal()->IsArrayInstance()); if (obj == nullptr || !obj->IsArrayInstance()) { return0;
} return obj->AsArray()->GetLength();
}
// Log an object with some additional info. // // Pass in the number of elements in the array (or 0 if this is not an // array object), and the number of additional objects that are identical // or equivalent to the original. staticvoid DumpSummaryLine(std::ostream& os, ObjPtr<mirror::Object> obj, size_t element_count, int identical, int equiv)
REQUIRES_SHARED(Locks::mutator_lock_) { if (obj == nullptr) {
os << " null reference (count=" << equiv << ")\n"; return;
} if (Runtime::Current()->IsClearedJniWeakGlobal(obj)) {
os << " cleared jweak (count=" << equiv << ")\n"; return;
}
std::string className(obj->PrettyTypeOf()); if (obj->IsClass()) { // We're summarizing multiple instances, so using the exemplar // Class' type parameter here would be misleading.
className = "java.lang.Class";
} if (element_count != 0) {
StringAppendF(&className, " (%zd elements)", element_count);
}
size_t total = identical + equiv + 1;
std::string msg(StringPrintf("%5zd of %s", total, className.c_str())); if (identical + equiv != 0) {
StringAppendF(&msg, " (%d unique instances)", equiv + 1);
}
os << " " << msg << "\n";
}
void ReferenceTable::Dump(std::ostream& os, Table& entries) { // Compare GC roots, first by class, then size, then address. struct GcRootComparator { booloperator()(GcRoot<mirror::Object> root1, GcRoot<mirror::Object> root2) const // TODO: enable analysis when analysis can work with the STL.
NO_THREAD_SAFETY_ANALYSIS {
Locks::mutator_lock_->AssertSharedHeld(Thread::Current()); // These GC roots are already forwarded in ReferenceTable::Dump. We sort by class since there // are no suspend points which can happen during the sorting process. This works since // we are guaranteed that the addresses of obj1, obj2, obj1->GetClass, obj2->GetClass wont // change during the sorting process. The classes are forwarded by ref->GetClass().
ObjPtr<mirror::Object> obj1 = root1.Read<kWithoutReadBarrier>();
ObjPtr<mirror::Object> obj2 = root2.Read<kWithoutReadBarrier>();
DCHECK(obj1 != nullptr);
DCHECK(obj2 != nullptr);
Runtime* runtime = Runtime::Current();
DCHECK(!runtime->IsClearedJniWeakGlobal(obj1));
DCHECK(!runtime->IsClearedJniWeakGlobal(obj2)); // Sort by class... if (obj1->GetClass() != obj2->GetClass()) { return obj1->GetClass() < obj2->GetClass();
} // ...then by size... const size_t size1 = obj1->SizeOf(); const size_t size2 = obj2->SizeOf(); if (size1 != size2) { return size1 < size2;
} // ...and finally by address. return obj1.Ptr() < obj2.Ptr();
}
};
if (entries.empty()) {
os << " (empty)\n"; return;
}
// Dump the most recent N entries. const size_t kLast = 10;
size_t count = entries.size(); int first = count - kLast; if (first < 0) {
first = 0;
}
os << " Last " << (count - first) << " entries (of " << count << "):\n";
Runtime* runtime = Runtime::Current(); for (int idx = count - 1; idx >= first; --idx) {
ObjPtr<mirror::Object> ref = entries[idx].Read(); if (ref == nullptr) { continue;
} if (runtime->IsClearedJniWeakGlobal(ref)) {
os << StringPrintf(" %5d: cleared jweak\n", idx); continue;
} if (ref->GetClass() == nullptr) { // should only be possible right after a plain dvmMalloc().
size_t size = ref->SizeOf();
os << StringPrintf(" %5d: %p (raw) (%zd bytes)\n", idx, ref.Ptr(), size); continue;
}
gc::AllocRecordObjectMap* records = runtime->GetHeap()->GetAllocationRecords();
DCHECK(records != nullptr); // It's annoying that this is a list. But this code should be very uncommon to be executed.
auto print_stack = [&](ObjPtr<mirror::Object> to_print, const std::string& msg)
REQUIRES_SHARED(Locks::mutator_lock_)
REQUIRES(Locks::alloc_tracker_lock_) { for (auto it = records->Begin(), end = records->End(); it != end; ++it) {
GcRoot<mirror::Object>& stack_for_object = it->first;
gc::AllocRecord& record = it->second; if (stack_for_object.Read() == to_print.Ptr()) {
os << " " << msg << "\n"; const gc::AllocRecordStackTrace* trace = record.GetStackTrace();
size_t depth = trace->GetDepth(); if (depth == 0) {
os << " (No managed frames)\n";
} else { for (size_t i = 0; i < depth; ++i) { const gc::AllocRecordStackTraceElement& frame = trace->GetStackElement(i);
os << " "; if (frame.GetMethod() == nullptr) {
os << "(missing method data)\n"; continue;
}
os << frame.GetMethod()->PrettyMethod(true)
<< ":"
<< frame.ComputeLineNumber()
<< "\n";
}
} break;
}
}
}; // Print the stack trace of the ref.
print_stack(ref, "Allocated at:");
// If it's a reference, see if we have data about the referent. if (ref->IsReferenceInstance()) {
ObjPtr<mirror::Object> referent = ref->AsReference()->GetReferent(); if (referent != nullptr) {
print_stack(referent, "Referent allocated at:");
}
}
}
}
// Make a copy of the table and sort it, only adding non null and not cleared elements.
Table sorted_entries; for (GcRoot<mirror::Object>& root : entries) { if (!root.IsNull() && !runtime->IsClearedJniWeakGlobal(root.Read())) {
sorted_entries.push_back(root);
}
} if (sorted_entries.empty()) { return;
}
std::sort(sorted_entries.begin(), sorted_entries.end(), GcRootComparator());
class SummaryElement { public:
GcRoot<mirror::Object> root;
size_t equiv;
size_t identical;
for (GcRoot<mirror::Object>& root : sorted_entries) {
ObjPtr<mirror::Object> current = root.Read<kWithoutReadBarrier>();
if (UNLIKELY(prev.root.IsNull())) {
prev.Reset(root); continue;
}
ObjPtr<mirror::Object> prevObj = prev.root.Read<kWithoutReadBarrier>(); if (current == prevObj) { // Same reference, added more than once.
++prev.identical;
} elseif (current->GetClass() == prevObj->GetClass() &&
GetElementCount(current) == GetElementCount(prevObj)) { // Same class / element count, different object.
++prev.equiv;
} else {
sorted_summaries.push_back(prev);
prev.Reset(root);
}
prev.root = root;
}
sorted_summaries.push_back(prev);
// Compare summary elements, first by combined count, then by identical (indicating leaks), // then by class (and size and address). struct SummaryElementComparator {
GcRootComparator gc_root_cmp;
¤ 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.0.14Bemerkung:
(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.