use InputStream in FFmpegFrameGrabber cause memory keep fast rising and leak
#1,878 opened on Aug 25, 2022
Repository metrics
- Stars
- (6,985 stars)
- PR merge metrics
- (No merged PRs in 30d)
Description
I am using the followed code to convert video to audio. I am using javacv-platform with version 1.5.7 in Ubuntu 16.04.1 LTS (GNU/Linux 4.4.0-31-generic x86_64) the file 2.ts could be dowload from https://dh5.cntv.myalicdn.com/asp/h5e/hls/450/0303000a/3/default/83d9d8cae92a489783bd509a4589014b/2.ts
for (int i = 0; i < 5000; i++)
{
try (InputStream in = new FileInputStream("2.ts"))
{
try (ByteArrayOutputStream outMp3 = new ByteArrayOutputStream(10 * 1024))
{
boolean bSuccess = convertToMp3(in, outMp3);
if (bSuccess)
{
byte[] bytes = outMp3.toByteArray();
}
}
}
System.out.println(i + ":" + Pointer.physicalBytes());
}
private static boolean convertToMp3(InputStream in, OutputStream outMp3) throws Exception
{
boolean bSuccess = false;
try (PointerScope scope = new PointerScope())
{
FFmpegLogCallback.setLevel(avutil.AV_LOG_QUIET);
try (FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(in))
{
frameGrabber.setCloseInputStream(true);
frameGrabber.start();
if (frameGrabber.hasAudio())
{
try (FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(outMp3, 1))
{
recorder.setCloseOutputStream(true);
recorder.setFormat("mp3");
recorder.setSampleRate(16000);
recorder.setTimestamp(frameGrabber.getTimestamp());
recorder.setAudioQuality(0);
recorder.start();
while (true)
{
try (Frame frame = frameGrabber.grabSamples())
{
if (frame == null)
{
break;
}
if (frame.samples != null)
{
recorder.recordSamples(frame.sampleRate, frame.audioChannels, frame.samples);
}
}
}
bSuccess = true;
}
}
}
}
return bSuccess;
}
the used memory keep fast rising and leak, 100:510840832 200:532566016 300:564105216 400:606396416 500:653266944 600:693764096 700:738697216 800:776306688 900:830025728 1000:872636416 1100:911044608 1200:962277376 1300:1003679744 1400:1045749760 1500:1091256320 1600:1127120896 1700:1178357760 1800:1218584576 1900:1258377216 2000:1308553216 2100:1346076672 2200:1396592640 2300:1440198656 2400:1482502144 2500:1515053056 2600:1565724672 2700:1608908800 2800:1648803840 2900:1697247232 3000:1741213696 3100:1775853568 3200:1839468544 3300:1879273472 3400:1919549440 3500:1968091136 3600:2009092096 3700:2052624384 3800:2092404736 3900:2138992640 4000:2186575872 4100:2229788672 4200:2273472512 4300:2314903552 4400:2350354432 4500:2396594176 4600:2445889536 4700:2486898688 4800:2518294528 4900:2570506240
when I change not to use InputStream in try (FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(in)) , as to
try (FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber("2.ts")),
the used memory keep stable,
100: 276180992 200: 398761984 300: 513257472 400: 549097472 500: 556146688 600: 569921536 700: 601571328 800: 618332160 900: 626929664 1000: 638472192 1100: 668401664 1200: 679972864 1300: 679976960 1400: 680005632 1500: 682606592 1600: 683032576 1700: 683036672 1800: 683126784 1900: 684371968 2000: 684371968 2100: 684371968 2200: 684371968 2300: 685002752 2400: 685002752 2500: 685031424 2600: 685056000 2700: 688852992 2800: 688852992 2900: 688852992 3000: 688852992 3100: 690688000 3200: 698650624 3300: 698650624 3400: 698650624 3500: 699326464 3600: 699326464 3700: 699326464 3800: 699326464 3900: 699551744 4000: 699551744 4100: 699551744 4200: 699551744 4300: 699723776 4400: 699723776 4500: 699723776 4600: 699809792 4700: 699838464 4800: 699838464 4900: 699838464