0

I'm making android app where I'm trying to generate video from bitmap images taken from TextureView. Here , I'm able to get the bitmap images from TextureView and able to save it in internal storage for later video generation. But Problem is as bitmap images are created and saved in local storage, App's Heap Size is also growing rapidly and later after saving around 800 images as .png file , it gives OutOfMemory Exception with Clamp target GC heap from 258MB to 256MB. So, How do i avoid Heap growing ?

I tried increasing app heap size but it's no use as heap is growing as new images are created and saved. I tried bitmap recycle and also setting null to bitmap, but no change in performance.

// code for getting bitmap from textureview

Bitmap bitmap = textureView.getBitmap(TF_OD_API_INPUT_SIZE, TF_OD_API_INPUT_SIZE);

//Bitmap that is displayed on screen

if(croppedBitmap!=null){
            croppedBitmap=null;
 }

 croppedBitmap = Bitmap.createBitmap((int) canvasWidth, (int) canvasHeight, Bitmap.Config.ARGB_8888);

above code are periodically ran and called using Runnable#run()

When User presses record button below code get executed and as Texture surface get updated that is saved as bitmap locally

@Override
                public void onSurfaceTextureUpdated(SurfaceTexture texture) {


                    if(Utils.isVideoRecordingStarted(activity)){
//if it already has bitmap recycle it to create room
                        if(mToVideo!=null){
                            mToVideo.recycle();
                            mToVideo=null;
                        }

                        if(newImage!=null){
                            newImage.recycle();
                            newImage=null;
                        }

//hold bitmap image
                        mToVideo = textureView.getBitmap(VideoManagerConfig.VIDEO_WIDTH,VideoManagerConfig.VIDEO_HEIGHT);

                        if(mToVideo!=null) {
                            if(activity.getResources().getConfiguration().orientation!=ORIENTATION_PORTRAIT){

                                mToVideo = textureView.getBitmap(VideoManagerConfig.VIDEO_HEIGHT,VideoManagerConfig.VIDEO_WIDTH);

                                if(mToVideo!=null){
                                    newImage = Utils.RotateBitmap(mToVideo.copy(mToVideo.getConfig(),true),90);
                                    Log.i(ObjectDetectionCameraImpl.TAG,"W: "+newImage.getWidth()+", H: "+newImage.getHeight());
                                    SaveToLocalStorage(newImage);
                                }
                            }else {
                                SaveToLocalStorage(mToVideo.copy(mToVideo.getConfig(),true));
                            }

                            mToVideo=null;
                        }
                    }else if(isInitilizedVideo && !Utils.isVideoRecordingStarted(activity)){
                        //video stopped and has something to encode
                        imageFromTextureListener.getBuffer(getBufferMetaData());
                    }
                }
            };

Following are the logs that are generated as images are saved.

VideoManager: images_924.png // means 925 images are already saved locally indexing started from 0

Buffer: 925 / 1800 //here 1800 => total images that it will be saved

Following the link to log file:  

https://drive.google.com/open?id=1GxFV-h2V68FSoWRs9K1AYhZChnlTFFqq
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Possible duplicate of [Strange out of memory issue while loading an image to a Bitmap object](https://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object) – Abdul Waheed Mar 26 '19 at 17:55
  • Thanks @AbdulWaheed , I read above link but I didn't understand, how could it be similar to mine problem? Here I am getting image from textureview using textureView.getBitmap() method. so how could I reduce memory consumption? – Pravinyo Mar 27 '19 at 15:11

1 Answers1

0

A similar issue that occurred while trying to zoom an image (in java though) was answered here as following: unless garbage collection occurs, the JVM (android uses ART though) keeps expanding heap as and when it needs to, all the way to the full heap size given to it. In short,

a Java process is free to use all the memory you gave it.

lineage
  • 792
  • 1
  • 8
  • 20
  • I tried setting null to variable and also recycling it but memory reclaimed is not much. Here code is generating multiple bitmap images of fixed size. How could it be optimized? – Pravinyo Mar 27 '19 at 15:21