From 04a935162c0af649987108058f81405930408014 Mon Sep 17 00:00:00 2001 From: Eino-Ville Talvala Date: Mon, 2 May 2016 13:24:40 -0700 Subject: [PATCH] ImageReader/Writer: Only register 1 buffer for native allocation Registering with the maximum potential memory made by visible by an ImageReader/Writer can cause the VM to try to heavily garbage-collect the rest of the application. This can have significant impact on camera applications, since they often have large ImageReader/Writer queues. Bug: 28454727 Change-Id: I2ba43635f93da66655be024165a15631b3b421d3 --- media/java/android/media/ImageReader.java | 7 +++++-- media/java/android/media/ImageWriter.java | 5 ++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/media/java/android/media/ImageReader.java b/media/java/android/media/ImageReader.java index 81cc03561d3fe..ec2d4bc9170fa 100644 --- a/media/java/android/media/ImageReader.java +++ b/media/java/android/media/ImageReader.java @@ -157,8 +157,11 @@ public class ImageReader implements AutoCloseable { // Estimate the native buffer allocation size and register it so it gets accounted for // during GC. Note that this doesn't include the buffers required by the buffer queue // itself and the buffers requested by the producer. - mEstimatedNativeAllocBytes = ImageUtils.getEstimatedNativeAllocBytes(width, height, format, - maxImages); + // Only include memory for 1 buffer, since actually accounting for the memory used is + // complex, and 1 buffer is enough for the VM to treat the ImageReader as being of some + // size. + mEstimatedNativeAllocBytes = ImageUtils.getEstimatedNativeAllocBytes( + width, height, format, /*buffer count*/ 1); VMRuntime.getRuntime().registerNativeAllocation(mEstimatedNativeAllocBytes); } diff --git a/media/java/android/media/ImageWriter.java b/media/java/android/media/ImageWriter.java index 83a4f17ffccb2..b142ddd9fbff5 100644 --- a/media/java/android/media/ImageWriter.java +++ b/media/java/android/media/ImageWriter.java @@ -138,11 +138,14 @@ public class ImageWriter implements AutoCloseable { // Estimate the native buffer allocation size and register it so it gets accounted for // during GC. Note that this doesn't include the buffers required by the buffer queue // itself and the buffers requested by the producer. + // Only include memory for 1 buffer, since actually accounting for the memory used is + // complex, and 1 buffer is enough for the VM to treat the ImageWriter as being of some + // size. Size surfSize = SurfaceUtils.getSurfaceSize(surface); int format = SurfaceUtils.getSurfaceFormat(surface); mEstimatedNativeAllocBytes = ImageUtils.getEstimatedNativeAllocBytes(surfSize.getWidth(),surfSize.getHeight(), - format, maxImages); + format, /*buffer count*/ 1); VMRuntime.getRuntime().registerNativeAllocation(mEstimatedNativeAllocBytes); }