python
8 minPython Collections: Lists, Tuples, Sets, and Map/Reduce Trade-offs
Choose Python collection types by invariants and access patterns, then use map, comprehensions, and reductions without obscuring intent.
Loading page...
python
8 minChoose Python collection types by invariants and access patterns, then use map, comprehensions, and reductions without obscuring intent.
java
9 minDiagnose collection bugs caused by inconsistent equality, mutable keys, inheritance, and accidental identity comparisons.
Q&A
Q&AIt is no longer strongly reachable from GC roots, though collection timing is not guaranteed.
An object becomes eligible when no strong-reference path leads to it from roots such as live thread stacks, static fields, or JNI references. Eligibility does not guarantee immediate collection or finalization. To diagnose retention, inspect heap dumps and dominator paths; do not rely on System.gc() as a fix.
Q&A
Q&ACorrelate GC logs, allocation rate, live-set size, pause phases, and user latency before tuning.
Enable and inspect unified GC logs for pause duration, cause, heap before/after, promotion, and allocation behavior. Correlate with request latency and container memory. A large live set, allocation bursts, humongous objects, or memory pressure can imply different fixes; tune collector flags only after identifying which resource or workload pattern drives the pauses.
Q&A
Q&AHashMap stores entries in buckets, resolves collisions with linked lists or trees, and resizes when load crosses a threshold.
HashMap uses an array of buckets and computes an index from the hash of the key. Each bucket may hold one or more entries. When collisions happen, Java stores entries in a linked list and later in a tree when the bucket grows large enough. The table resizes when the load factor is exceeded so the map keeps operations efficient. The key idea is balancing fast lookup with controlled memory use.
Q&A
Q&ABase the choice on ordering, mutation, duplicates, and membership operations.
A list is ordered, mutable, and permits duplicates. A tuple is an immutable sequence suited to fixed records, though named structures can improve readability. A set stores unique hashable values and is useful for membership; do not use it when output order or duplicates are part of the contract.
Q&A
Q&ACompare post-GC live-set size and object retention paths over time.
High allocation can cause frequent collections while post-GC occupancy remains broadly stable. A leak tends to retain objects that should have become unreachable, so compare heap dumps and dominator paths across time. Check allocation rate, live set, GC logs, and process/container memory together before changing heap settings.
Q&A
Q&AThe keys share a bucket, then equality distinguishes entries within that bucket.
A hash helps select a bucket but does not establish key identity. HashMap checks equality among entries in the bucket; collisions can be stored in a list and, in sufficiently large bins, treeified under implementation conditions. Correct equals/hashCode behavior is essential, and resizing changes bucket placement without making hashes unique.