tekartik / process_run.dart

Process run helper

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Out of memory when running stream

JSBmanD opened this issue · comments

I have a command running stream via ffmpeg
libcamera-vid --framerate 24 --nopreview --inline -t 0 --width 1920 --height 1080 --listen -o - | ffmpeg -f lavfi -i anullsrc -i - -profile:v high -pix_fmt yuvj420p -level:v 4.1 -preset ultrafast -framerate 24 -g 48 -tune zerolatency -vcodec libx264 -r 10 -s 1920x1080 -b:v 2M -f flv rtmp://a.rtmp.youtube.com/live2/YOUR_KEY

And after some time - I'm having out of memory error

Error
  Exhausted heap space, trying to allocate 8589934608 bytes.
Unhandled exception:
Out of Memory
#0      List._grow (dart:core-patch/growable_array.dart:376:19)
#1      List.addAll (dart:core-patch/growable_array.dart:304:9)
#2      runExecutableArguments.streamToResult (package:process_run/src/process_run.dart:132:12)
#3      _FutureListener.handleValue (dart:async/future_impl.dart:162:18)
#4      Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:846:45)
#5      Future._propagateToListeners (dart:async/future_impl.dart:875:13)
#6      Future._complete (dart:async/future_impl.dart:638:7)
#7      _StreamIterator._onData (dart:async/stream_impl.dart:1034:20)
#8      _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#9      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#10     _StreamController.add (dart:async/stream_controller.dart:606:5)
#11     runExecutableArguments.<anonymous closure> (package:process_run/src/process_run.dart:147:13)
#12     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#13     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#14     _StreamController.add (dart:async/stream_controller.dart:606:5)
#15     _Socket._onData (dart:io-patch/socket_patch.dart:2447:41)
#16     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#17     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#18     _StreamController.add (dart:async/stream_controller.dart:606:5)
#19     new _RawSocket.<anonymous closure> (dart:io-patch/socket_patch.dart:1936:33)
#20     _NativeSocket.issueReadEvent.issue (dart:io-patch/socket_patch.dart:1379:14)
#21     _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)
#22     _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:185:5)

Thanks for the report, indeed it seems that putting all the resulting output in memory is causing the error. I'll look into it.

To handle such case (i.e. it is trying to add 10GB of output here). I have added a way to discard putting the output in the resulting ProcessResult.stdout. I have added options noStdoutResult and noStderrResult to ShellOptions and runExecutableArguments. In this case the resulting ProcessResult will have a null stdout (or stderr). You can still stream the output as desired but the overall result is discarded and it should solve your issue. Let me know

usage example:

var shell = Shell(options: ShellOptions(noStdoutResult: true));
await shell.run('xxxx');