Optimizing Android Memory Usage by Avoiding Autoboxing in Long Objects
This article discusses memory optimization in Android apps by addressing excessive object allocations caused by autoboxing Long values, leading to performance issues during feed scrolling. The solution involves implementing a custom data structure to reduce memory usage and improve application responsiveness.
This technical article from Facebook's product team analyzes memory inefficiencies in their Android feed platform caused by automatic object creation (autoboxing) when using primitive long values with HashSet data structures. The problem arises during feed scrolling operations, where frequent Long.valueOf() calls generate excessive garbage collection pressure, leading to app freezes.
The authors identified that HashSet operations on long primitives trigger unavoidable autoboxing, creating numerous Long objects that consume heap memory. This becomes particularly problematic on Dalvik-based devices lacking generational garbage collection.
Current solutions using existing Java libraries for primitive-based sets showed limited effectiveness. The team developed a custom LongArraySet implementation leveraging Android's LongSparseArray, which stores long values in a sorted array for O(log N) lookups while avoiding object allocations. Testing showed significant memory reduction (30% less allocation) and improved performance for typical use cases with hundreds of items.
The solution trades O(log N) lookup time for memory efficiency, which proved acceptable for their specific workload. The article includes detailed performance metrics comparing HashSet and LongArraySet implementations, along with source code references for the new data structure.
Beike Product & Technology
As Beike's official product and technology account, we are committed to building a platform for sharing Beike's product and technology insights, targeting internet/O2O developers and product professionals. We share high-quality original articles, tech salon events, and recruitment information weekly. Welcome to follow us.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.