FFmpegRecorder icon indicating copy to clipboard operation
FFmpegRecorder copied to clipboard

OutOfMemoryError: Failed to allocate memory within limits

Open Ggsdl opened this issue 7 years ago • 11 comments

Dear CrazyOrr: I use this Project, after recording 1 minutes will be crash, the reason is java.lang.OutOfMemoryError: Failed to allocate memory within limits: totalBytes = 255M + 112K > maxBytes = 256M do you have some good suggestions to correct this error Thanks very much

Ggsdl avatar Jan 15 '17 05:01 Ggsdl

You can set PREFERRED_PREVIEW_WIDTH and PREFERRED_PREVIEW_HEIGHT to smaller values to try to get a smaller preview size to reduce the memory allocation of it.

CrazyOrr avatar Jan 16 '17 05:01 CrazyOrr

Thank you for your reply, I try to set PREFERRED_PREVIEW_WIDTH and PREFERRED_PREVIEW_HEIGHT to smaller like this : private static final int PREFERRED_PREVIEW_WIDTH = 320; private static final int PREFERRED_PREVIEW_HEIGHT = 240;

But it also crash at 1-2 minutes . Now , i want record a 10 minutes Video, if use your code , do you have some good suggestions to correct this error ? Thanks very much

Ggsdl avatar Jan 18 '17 05:01 Ggsdl

It could be memory leaks. If you use Android Studio, you can monitor memory usage by View -> Tool Windows -> Android Monitor -> Monitors tab. If you observe that the Allocated memory size gets larger and larger over time, you should Dump Java Heap and do Allocation Tracking to find out what is taking all the memory.

CrazyOrr avatar Jan 18 '17 08:01 CrazyOrr

I try to find memory leaks , but not find, can you help me to find this ? I just use your project only change the MAX_VIDEO_LENGTH to 10 minutes, Thanks !

Ggsdl avatar Jan 19 '17 01:01 Ggsdl

It works fine on my device. Please provide more info about your testing environment (device model, Android version etc) and upload error logs, .hprof, .alloc files around the crash point.

CrazyOrr avatar Jan 19 '17 02:01 CrazyOrr

error log is like this , my device is samsung ,android version is 5.0.1 01-19 10:38:02.442 22665-22873/com.github.crazyorr.ffmpegrecorder E/AndroidRuntime: FATAL EXCEPTION: Thread-17822 Process: com.github.crazyorr.ffmpegrecorder, PID: 22665 java.lang.OutOfMemoryError: Failed to allocate memory within limits: totalBytes = 255M + 600K > maxBytes = 256M at org.bytedeco.javacpp.Pointer.deallocator(Pointer.java:558) at org.bytedeco.javacpp.Pointer.init(Pointer.java:121) at org.bytedeco.javacpp.BytePointer.allocateArray(Native Method) at org.bytedeco.javacpp.BytePointer.<init>(BytePointer.java:82) at org.bytedeco.javacv.FFmpegFrameFilter.pushImage(FFmpegFrameFilter.java:265) at org.bytedeco.javacv.FFmpegFrameFilter.push(FFmpegFrameFilter.java:252) at org.bytedeco.javacv.FFmpegFrameFilter.push(FFmpegFrameFilter.java:248) at com.github.crazyorr.ffmpegrecorder.FFmpegRecordActivity$VideoRecordThread.run(FFmpegRecordActivity.java:702)

Ggsdl avatar Jan 19 '17 02:01 Ggsdl

It just point in project file : FFmpegRecordActivity this line frameFilter.push(recordedFrame.getFrame());

Ggsdl avatar Jan 19 '17 02:01 Ggsdl

This could be a bug upstream. See this issue. You can try set android:largeHeap to true for now.

CrazyOrr avatar Jan 19 '17 08:01 CrazyOrr

I set android:largeHeap to true ,but It does't work also , I see the Monitors ,this project works hightest memory is 50MB, then it crash,reason is : 01-19 17:35:19.502 31298-31597/com.github.crazyorr.ffmpegrecorder E/AndroidRuntime: FATAL EXCEPTION: Thread-24881 Process: com.github.crazyorr.ffmpegrecorder, PID: 31298 java.lang.OutOfMemoryError: Failed to allocate memory within limits: totalBytes = 511M + 600K > maxBytes = 512M at org.bytedeco.javacpp.Pointer.deallocator(Pointer.java:558) at org.bytedeco.javacpp.Pointer.init(Pointer.java:121) at org.bytedeco.javacpp.BytePointer.allocateArray(Native Method) at org.bytedeco.javacpp.BytePointer.<init>(BytePointer.java:82) at org.bytedeco.javacv.FFmpegFrameFilter.pushImage(FFmpegFrameFilter.java:265) at org.bytedeco.javacv.FFmpegFrameFilter.push(FFmpegFrameFilter.java:252) at org.bytedeco.javacv.FFmpegFrameFilter.push(FFmpegFrameFilter.java:248) at com.github.crazyorr.ffmpegrecorder.FFmpegRecordActivity$VideoRecordThread.run(FFmpegRecordActivity.java:702)

Ggsdl avatar Jan 19 '17 09:01 Ggsdl

Check if org.bytedeco.javacpp.DeallocatorThread is working properly.

CrazyOrr avatar Jan 19 '17 11:01 CrazyOrr

@Ggsdl @LoginGit I added capacity restraints on frame queues (mFrameToRecordQueue and mRecycledFrameQueue) in the latest commit. So the queues won't increase infinitely to the point of OOM, but frames may get dropped when a queue is full, so video quality may suffer.

CrazyOrr avatar Jan 22 '17 07:01 CrazyOrr