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