Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

need help, javacv 1.5.9 memory leak #2119

Closed
shaoneng111 opened this issue Nov 2, 2023 · 17 comments
Closed

need help, javacv 1.5.9 memory leak #2119

shaoneng111 opened this issue Nov 2, 2023 · 17 comments
Labels

Comments

@shaoneng111
Copy link

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
Copy link
Member

saudet commented Nov 2, 2023

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

@shaoneng111
Copy link
Author

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
Copy link
Member

saudet commented Nov 2, 2023

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

@shaoneng111
Copy link
Author

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
Copy link
Author

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

@saudet
Copy link
Member

saudet commented Nov 2, 2023

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
Copy link
Author

heap_68-69

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

@shaoneng111
Copy link
Author

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

@saudet
Copy link
Member

saudet commented Nov 7, 2023 via email

@saudet saudet added the bug label Nov 19, 2023
@jetamie
Copy link

jetamie commented Feb 26, 2024

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

@jetamie
Copy link

jetamie commented Feb 26, 2024

image
Maybe you need to call both

@Lwine
Copy link
Contributor

Lwine commented Mar 30, 2024

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
Copy link
Member

saudet commented Apr 21, 2024

@Lwine Thanks for the fix!

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

@Love122
Copy link

Love122 commented May 10, 2024

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

@xiaobei2019
Copy link

@saudet
When will the 1.5.11 version be released to solve the memory leakage problem?
#2214

@saudet
Copy link
Member

saudet commented Nov 17, 2024

JavaCV 1.5.11 has been released and should fix this issue, but please let me know if you still have problems! Thanks

@saudet saudet closed this as completed Nov 17, 2024
@saudet
Copy link
Member

saudet commented Nov 17, 2024

And thanks for @Lwine for the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants