Fix a memory leak with pending jank classifications
If the front of the pendingJankClassification deque is
stuck(FrameTimeline never received it) for some reason, the current
logic never releases it and the deque keeps growing in size until the
layer itself is destroyed. This has resulted in some memory leaks
recently.
Bug: 181328447
Test: heap_prof trace
Change-Id: If71225f52da3bbd9b0b0d46af75f933fd48d5bd5
diff --git a/services/surfaceflinger/BufferStateLayer.cpp b/services/surfaceflinger/BufferStateLayer.cpp
index 3137ee7..4724ef8 100644
--- a/services/surfaceflinger/BufferStateLayer.cpp
+++ b/services/surfaceflinger/BufferStateLayer.cpp
@@ -171,6 +171,14 @@
void BufferStateLayer::onSurfaceFrameCreated(
const std::shared_ptr<frametimeline::SurfaceFrame>& surfaceFrame) {
+ while (mPendingJankClassifications.size() >= kPendingClassificationMaxSurfaceFrames) {
+ // Too many SurfaceFrames pending classification. The front of the deque is probably not
+ // tracked by FrameTimeline and will never be presented. This will only result in a memory
+ // leak.
+ ALOGW("Removing the front of pending jank deque from layer - %s to prevent memory leak",
+ mName.c_str());
+ mPendingJankClassifications.pop_front();
+ }
mPendingJankClassifications.emplace_back(surfaceFrame);
}