bytedeco / javacv

Java interface to OpenCV, FFmpeg, and more
Other
7.53k stars 1.58k forks source link

need help, javacv 1.5.9 memory leak #2119

Open shaoneng111 opened 11 months ago

shaoneng111 commented 11 months ago

environment : centos 6.8 jdk: java8 problem: memory is growing slowly

mycode:

public static VideoComputeInfo computeVideoQuality(String videoContentId, InputStream videoInputStream) {
if (videoInputStream == null) {
return null;
}
long startTime = System.currentTimeMillis();
// 直接指定maximumSize
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(videoInputStream, 1000000);
grabber.setCloseInputStream(true);
try {
// 截取jpeg文件
grabber.start();
// 视频帧率
Integer frameRate = (int) grabber.getVideoFrameRate();
// 视频时长
int timeLen = (int) Math.ceil((double) grabber.getLengthInTime() / 1000000);
int frameWidth = grabber.getImageWidth();
int frameHeight = grabber.getImageHeight();
// 视频文件大小
int videoSize = videoInputStream.available();
long endTime = System.currentTimeMillis();
VideoComputeInfo videoComputeInfo = VideoComputeInfo.builder()
.frameRate(frameRate)
.height(frameHeight)
.width(frameWidth)
.timeLen(timeLen)
.videoSize(videoSize)
.build();
log.info("FfmpegUtil computeVideoQuality耗时:{}, videoContentId: {}, videoComputeInfo: {}"
, endTime - startTime, videoContentId, videoComputeInfo);
return videoComputeInfo;
} catch (Exception e) {
log.error("FfmpegUtil computeVideoQuality异常,异常信息:", e);
} finally {
try {
grabber.close();
} catch (Exception e) {
log.error("FfmpegUtil grabber.close()异常,异常信息:", e);
}
}
return null;
} 

i don't know what's wrong with it

saudet commented 11 months ago

If the problem doesn't occur without VideoComputeInfo, it's a problem with your VideoComputeInfo, not JavaCV.

shaoneng111 commented 11 months ago

thank you for your reply! but VideoComputeInfo is a simple java object , use java heap. i analysis the java heap memory, its looks like normal, so the leak occurs not in java heap?

saudet commented 11 months ago

If you can reproduce the issue without VideoComputeInfo, please provide such sample code.

shaoneng111 commented 11 months ago

thank you for your reply! but VideoComputeInfo is a simple java object , use java heap. i analysis the java heap memory, its looks like normal, so the leak occurs not in java heap?

and VideoComputeInfo should occupy very little memory, but actually it will eat 1G memory one day

shaoneng111 commented 11 months ago

i also want to know , Is the way I'm using javacv correct?

saudet commented 11 months ago

Looks alright, but the problem might also be caused by your InputStream. If you're not closing your InputStream, it can leak memory. If you want me to help, you'll need to come up with something that doesn't depend on your system.

shaoneng111 commented 11 months ago

heap_68-69

i use gperftools analyze the memory. it look like ffmpeg apply the av_malloc memory , is this means it leaks?

shaoneng111 commented 11 months ago

hi, i find where is bug is. when FFmpegFrameGrabber input param is inputstream,it don't realease avformat and occurs memory leak. when i change FFmpegFrameGrabber input param from inputstream to file, memory leak disapears.

image

it look like avformat_free_context is invalid, should use avformat_close_input?

otherwise, in this issue : https://github.com/bytedeco/javacv/issues/911, someone has mensioned it, looks like the same problem with me.

image
saudet commented 11 months ago

Sounds like a fix. Please open a pull request!

jetamie commented 7 months ago

image avio_context_free(avio) should be called here instead of av_free(avio) @saudet

jetamie commented 7 months ago

image Maybe you need to call both

Lwine commented 6 months ago

hi, i find where is bug is. when FFmpegFrameGrabber input param is inputstream,it don't realease avformat and occurs memory leak. when i change FFmpegFrameGrabber input param from inputstream to file, memory leak disapears. image it look like avformat_free_context is invalid, should use avformat_close_input?

otherwise, in this issue : #911, someone has mensioned it, looks like the same problem with me. image

hello,i had the same problem.How did you reslove it?

saudet commented 5 months ago

@Lwine Thanks for the fix!

@shaoneng111 Please give it a try with the snapshots: http://bytedeco.org/builds/

Love122 commented 4 months ago

How to fix this javacv 1.5.9 memory leak ? Base in javacv keep version 1.5.9,not change

xiaobei2019 commented 3 months ago

@saudet When will the 1.5.11 version be released to solve the memory leakage problem? https://github.com/bytedeco/javacv/pull/2214