Closed orgads closed 3 years ago
Is it possible to avoid using ReplayableAudioNode
? Maybe by adding a config parameter?
@orgads I'm not seeing such a tremendous leap in the arrayBuffers usage. Can you reproduce this with version 1.16? From my testing, there does seem to be an 8K memory leak per recognizer every five minutes, when a new "turn.start" message is received while the current turn is still ongoing.
Looks the same here:
{rss: 186736640, heapTotal: 149004288, heapUsed: 125181248, external: 3022839, arrayBuffers: 836000}
(sessionStarted) SessionId: FC29E204AE684F62A1B9667C3D89A7D8
{rss: 190607360, heapTotal: 152141824, heapUsed: 124819632, external: 2860998, arrayBuffers: 671040}
{rss: 192126976, heapTotal: 152141824, heapUsed: 126369480, external: 3075558, arrayBuffers: 885600}
{rss: 193228800, heapTotal: 152141824, heapUsed: 128012104, external: 3296518, arrayBuffers: 1106560}
{rss: 194678784, heapTotal: 152141824, heapUsed: 129574816, external: 3517478, arrayBuffers: 1327520}
{rss: 200122368, heapTotal: 156073984, heapUsed: 122084960, external: 3209965, arrayBuffers: 1020007}
{rss: 200957952, heapTotal: 156073984, heapUsed: 123647312, external: 3430925, arrayBuffers: 1240967}
(speechEndDetected) SessionId: FC29E204AE684F62A1B9667C3D89A7D8
(recognized) Reason: NoMatch Text: undefined
{rss: 202702848, heapTotal: 156073984, heapUsed: 125761488, external: 3689512, arrayBuffers: 1499554}
{rss: 161112064, heapTotal: 113606656, heapUsed: 107412840, external: 2511650, arrayBuffers: 321692}
{rss: 163704832, heapTotal: 114655232, heapUsed: 108065656, external: 2675339, arrayBuffers: 485381}
{rss: 163422208, heapTotal: 113606656, heapUsed: 107763992, external: 2788221, arrayBuffers: 598263}
{rss: 164478976, heapTotal: 113868800, heapUsed: 107617496, external: 2892978, arrayBuffers: 703020}
{rss: 165842944, heapTotal: 113868800, heapUsed: 108316520, external: 3056830, arrayBuffers: 866872}
{rss: 166924288, heapTotal: 113868800, heapUsed: 108298864, external: 3176574, arrayBuffers: 986616}
{rss: 168509440, heapTotal: 113868800, heapUsed: 108047544, external: 3281363, arrayBuffers: 1091405}
(speechEndDetected) SessionId: FC29E204AE684F62A1B9667C3D89A7D8
(recognized) Reason: NoMatch Text: undefined
{rss: 170139648, heapTotal: 113868800, heapUsed: 108391488, external: 3466497, arrayBuffers: 1276539}
{rss: 171790336, heapTotal: 113868800, heapUsed: 109019392, external: 3630317, arrayBuffers: 1440359}
{rss: 173051904, heapTotal: 113868800, heapUsed: 108890472, external: 3743560, arrayBuffers: 1553602}
{rss: 174604288, heapTotal: 114130944, heapUsed: 108718752, external: 3856426, arrayBuffers: 1666468}
{rss: 175919104, heapTotal: 114130944, heapUsed: 109345448, external: 4012054, arrayBuffers: 1822096}
{rss: 176922624, heapTotal: 114130944, heapUsed: 109035128, external: 4124920, arrayBuffers: 1934962}
{rss: 178262016, heapTotal: 114130944, heapUsed: 108907112, external: 4238147, arrayBuffers: 2048189}
(speechEndDetected) SessionId: FC29E204AE684F62A1B9667C3D89A7D8
(recognized) Reason: NoMatch Text: undefined
{rss: 180621312, heapTotal: 114298880, heapUsed: 109549640, external: 4414438, arrayBuffers: 2224480}
{rss: 181919744, heapTotal: 114298880, heapUsed: 109602656, external: 4550576, arrayBuffers: 2360618}
{rss: 183586816, heapTotal: 114298880, heapUsed: 109632976, external: 4686966, arrayBuffers: 2497008}
{rss: 184246272, heapTotal: 114298880, heapUsed: 109426136, external: 4859125, arrayBuffers: 2610120}
{rss: 186159104, heapTotal: 114298880, heapUsed: 110064944, external: 4963799, arrayBuffers: 2773841}
{rss: 187502592, heapTotal: 114298880, heapUsed: 109790256, external: 5076780, arrayBuffers: 2886822}
{rss: 189140992, heapTotal: 114298880, heapUsed: 110434072, external: 5235992, arrayBuffers: 3046034}
{rss: 192016384, heapTotal: 114298880, heapUsed: 110274880, external: 5345520, arrayBuffers: 3155562}
(speechEndDetected) SessionId: FC29E204AE684F62A1B9667C3D89A7D8
(recognized) Reason: NoMatch Text: undefined
{rss: 191909888, heapTotal: 114298880, heapUsed: 110041176, external: 5507395, arrayBuffers: 3317437}
{rss: 194125824, heapTotal: 114298880, heapUsed: 110824080, external: 5677762, arrayBuffers: 3487804}
{rss: 195186688, heapTotal: 114298880, heapUsed: 110776376, external: 5799328, arrayBuffers: 3609370}
{rss: 198926336, heapTotal: 114466816, heapUsed: 110764552, external: 5906040, arrayBuffers: 3716082}
{rss: 199491584, heapTotal: 114466816, heapUsed: 111389744, external: 6076161, arrayBuffers: 3886203}
{rss: 199397376, heapTotal: 114466816, heapUsed: 111161960, external: 6181166, arrayBuffers: 3991208}
{rss: 200654848, heapTotal: 114466816, heapUsed: 110879440, external: 6294147, arrayBuffers: 4104189}
(speechEndDetected) SessionId: FC29E204AE684F62A1B9667C3D89A7D8
(recognized) Reason: NoMatch Text: undefined
{rss: 202444800, heapTotal: 114466816, heapUsed: 111201528, external: 6464083, arrayBuffers: 4274125}
{rss: 203780096, heapTotal: 114466816, heapUsed: 111044640, external: 6595052, arrayBuffers: 4405094}
{rss: 205156352, heapTotal: 114466816, heapUsed: 111679600, external: 6756981, arrayBuffers: 4567023}
{rss: 206725120, heapTotal: 114798592, heapUsed: 111722960, external: 6863562, arrayBuffers: 4673604}
{rss: 208826368, heapTotal: 114798592, heapUsed: 112361872, external: 7025475, arrayBuffers: 4835517}
{rss: 209686528, heapTotal: 114798592, heapUsed: 112268712, external: 7147041, arrayBuffers: 4957083}
{rss: 211161088, heapTotal: 114798592, heapUsed: 111964752, external: 7245299, arrayBuffers: 5055341}
{rss: 212758528, heapTotal: 114798592, heapUsed: 112566992, external: 7415404, arrayBuffers: 5225446}
(speechEndDetected) SessionId: FC29E204AE684F62A1B9667C3D89A7D8
(recognized) Reason: NoMatch Text: undefined
{rss: 214552576, heapTotal: 114798592, heapUsed: 112829496, external: 7593741, arrayBuffers: 5403783}
{rss: 215728128, heapTotal: 114798592, heapUsed: 112777440, external: 7706853, arrayBuffers: 5516895}
{rss: 216666112, heapTotal: 114798592, heapUsed: 112753656, external: 7843243, arrayBuffers: 5653285}
{rss: 218247168, heapTotal: 114630656, heapUsed: 105191352, external: 2671650, arrayBuffers: 481692}
{rss: 218804224, heapTotal: 114630656, heapUsed: 105785760, external: 2835240, arrayBuffers: 645282}
{rss: 218558464, heapTotal: 114630656, heapUsed: 105507128, external: 2941821, arrayBuffers: 751863}
{rss: 219201536, heapTotal: 114630656, heapUsed: 105168480, external: 3046578, arrayBuffers: 856620}
(speechEndDetected) SessionId: FC29E204AE684F62A1B9667C3D89A7D8
(recognized) Reason: NoMatch Text: undefined
{rss: 220229632, heapTotal: 114630656, heapUsed: 105528352, external: 3220370, arrayBuffers: 1030412}
{rss: 221487104, heapTotal: 114630656, heapUsed: 105596608, external: 3345348, arrayBuffers: 1155390}
{rss: 221880320, heapTotal: 114630656, heapUsed: 105547456, external: 3466914, arrayBuffers: 1276956}
{rss: 223936512, heapTotal: 114630656, heapUsed: 106278336, external: 3639073, arrayBuffers: 1449115}
{rss: 224788480, heapTotal: 114798592, heapUsed: 106431688, external: 3760639, arrayBuffers: 1570681}
{rss: 225828864, heapTotal: 114798592, heapUsed: 106330296, external: 3882205, arrayBuffers: 1692247}
{rss: 226545664, heapTotal: 114798592, heapUsed: 106180704, external: 3995448, arrayBuffers: 1805490}
{rss: 228048896, heapTotal: 114798592, heapUsed: 106741648, external: 4159153, arrayBuffers: 1969195}
(speechEndDetected) SessionId: FC29E204AE684F62A1B9667C3D89A7D8
(recognized) Reason: NoMatch Text: undefined
{rss: 229310464, heapTotal: 114798592, heapUsed: 106920752, external: 4349623, arrayBuffers: 2159665}
{rss: 230948864, heapTotal: 115326976, heapUsed: 107102928, external: 4445827, arrayBuffers: 2255869}
{rss: 234590208, heapTotal: 116375552, heapUsed: 107039448, external: 4565702, arrayBuffers: 2375744}
{rss: 234676224, heapTotal: 115326976, heapUsed: 107593680, external: 4729407, arrayBuffers: 2539449}
{rss: 235032576, heapTotal: 115326976, heapUsed: 107171968, external: 4825742, arrayBuffers: 2635784}
{rss: 237068288, heapTotal: 115658752, heapUsed: 108057816, external: 4989447, arrayBuffers: 2799489}
{rss: 238333952, heapTotal: 115658752, heapUsed: 107655008, external: 5094105, arrayBuffers: 2904147}
(speechEndDetected) SessionId: FC29E204AE684F62A1B9667C3D89A7D8
(recognized) Reason: NoMatch Text: undefined
{rss: 239501312, heapTotal: 115658752, heapUsed: 107534472, external: 5185309, arrayBuffers: 2995351}
{rss: 241917952, heapTotal: 115658752, heapUsed: 108299256, external: 5346666, arrayBuffers: 3156708}
{rss: 242470912, heapTotal: 115658752, heapUsed: 107822424, external: 5362066, arrayBuffers: 3172108}
{rss: 244039680, heapTotal: 115658752, heapUsed: 108331232, external: 5474666, arrayBuffers: 3284708}
{rss: 244764672, heapTotal: 115658752, heapUsed: 107871880, external: 5490181, arrayBuffers: 3300223}
{rss: 247271424, heapTotal: 115658752, heapUsed: 108479456, external: 5609181, arrayBuffers: 3419223}
{rss: 247566336, heapTotal: 115658752, heapUsed: 108062752, external: 5628130, arrayBuffers: 3438172}
{rss: 249192448, heapTotal: 115658752, heapUsed: 108575032, external: 5743813, arrayBuffers: 3553855}
(speechEndDetected) SessionId: FC29E204AE684F62A1B9667C3D89A7D8
(recognized) Reason: NoMatch Text: undefined
{rss: 250318848, heapTotal: 115658752, heapUsed: 107686752, external: 5841329, arrayBuffers: 3526022}
{rss: 251498496, heapTotal: 115658752, heapUsed: 108199992, external: 5836772, arrayBuffers: 3646814}
{rss: 253243392, heapTotal: 115658752, heapUsed: 108704952, external: 5955657, arrayBuffers: 3765699}
{rss: 254357504, heapTotal: 115658752, heapUsed: 108315552, external: 5968204, arrayBuffers: 3778246}
{rss: 255098880, heapTotal: 115658752, heapUsed: 107799008, external: 5983604, arrayBuffers: 3793646}
{rss: 256655360, heapTotal: 115658752, heapUsed: 108305728, external: 6096089, arrayBuffers: 3906131}
{rss: 257634304, heapTotal: 115658752, heapUsed: 107942040, external: 6115036, arrayBuffers: 3925078}
{rss: 259321856, heapTotal: 115658752, heapUsed: 108444168, external: 6235713, arrayBuffers: 4045755}
(speechEndDetected) SessionId: FC29E204AE684F62A1B9667C3D89A7D8
(recognized) Reason: NoMatch Text: undefined
{rss: 260444160, heapTotal: 115658752, heapUsed: 108480136, external: 6302939, arrayBuffers: 4112981}
{rss: 261578752, heapTotal: 115658752, heapUsed: 108086952, external: 6318803, arrayBuffers: 4128845}
{rss: 262758400, heapTotal: 115658752, heapUsed: 107979360, external: 6367747, arrayBuffers: 4177789}
{rss: 265039872, heapTotal: 115990528, heapUsed: 108914456, external: 6498371, arrayBuffers: 4308413}
@orgads After correcting my setup, I'm getting similar numbers now, and while continuous recognition with SpeechRecognizer does not appear to leak memory (it cleans up sent audio data every 8MB), DialogServiceConnector does appear to leak about 70KB every two seconds. I'll start taking heap snapshots and looking for the culprit. Thanks for submitting this!
@orgads I believe I see the issue.
The following line will correct this issue:
reco.listenOnceAsync((res) => audioStream.push(null), (e) => console.error(e));
Does that make sense?
@glharper Do you mean pushStream.write(null)
? Can it accept null
?
@glharper Do you mean
pushStream.write(null)
? Can it acceptnull
?
Yes, and yes. See this line. The code in my comment from May of last year could presumably also be:
reco.listenOnceAsync((res) => audioStream.close(), (e) => console.error(e));
, it appears, but there may be some reasoning I'm not remembering from nine months ago.
I'm sorry for the late reply. We recently got feedback about memory leaks, so we're back to these investigations :/
I suppose we can call close()
also on recognizer.recognized
callback, right?
I see that we do call pushStream.close()
when the input stream emits close
event. Shouldn't this be enough?
I see that we do call
pushStream.close()
when the input stream emitsclose
event. Shouldn't this be enough?
If the input stream somehow closes exactly when the recognizer is finished recognizing, then that would be enough. Otherwise, without closing the pushStream on recognition end, the recognition process will go (assuming everything is initialized properly):
Does that make sense?
I see. So if you stop reading from it, can you call resume()
on recognition instead of moving the responsibility to the calling code?
And shouldn't the buffered chunks be cleaned up on close anyway?
I see. So if you stop reading from it, can you call
resume()
on recognition instead of moving the responsibility to the calling code?
You write 'Stop reading from "it"'. Is "it" an input stream or a speech SDK push stream?
Speech SDK push streams do not have a resume() API, only write() and close().
You should be able to use the same push stream across multiple recognitions, using listenOnceAsync() multiple times, but the input stream would have to be managed to account for pausing and resuming, since the push stream only has a write() API.
Once you start thinking about pausing and resuming, I'd recommend just closing the old pushAudioStream and setting up a new pushAudioStream.
And shouldn't the buffered chunks be cleaned up on close anyway?
Yes, on pushAudioStream.close(), the buffered chunks should be cleaned up. Before then, however, a memory profile will show an increase in memory usage if no recognition is happening and the input stream is still writing to pushAudioStream.
This was the problem with the original code for this GitHub issue, an input stream was still writing to an open pushAudioStream after recognition ended, and the memory profiler showed the pushAudioStream increasing in size. Calling pushAudioStream.close() at the end of recognition should correct this memory issue.
Hi,
We're experiencing a significant memory leak when the input stream is silent for a long time.
Can be reproduced with the following script. Requires
npm i dev-zero-stream
.Happens with both
SpeechRecognizer
andDialogServiceConnector
. You can switch between them by [re]settingisDirect
.Sample outputs after the script.
Sample outputs. Notice how rss keeps growing all the time. I also saw on heap timeline that some
BufferEntry
s are retained byReplayableAudioNode
on non-direct, and manyArrayBuffer
s are retained byChunkedArrayBufferStream
on direct.SpeechRecognizer
Direct