5
  1. How does printing to terminal affect memory usage? Would it just keep eating up memory until OOM?
  2. What about in a docker container?
  3. How does the OS handle the memory of prints to terminal?
  4. Does the OS flush the terminal at a certain point?
gunit
  • 3,700
  • 4
  • 31
  • 42
  • If you print wihtout linebreaks and the console is configured to only show something after one, you end up filling the internal buffer until something "gives". – Lothar Feb 15 '18 at 21:49
  • It depends on the terminal ([running out of memory](https://invisible-island.net/ncurses/ncurses-slang.html#compare_dots) is a bug, but some programs have bugs). – Thomas Dickey Feb 15 '18 at 22:32
  • this (https://stackoverflow.com/questions/67560357/can-writing-to-tensorboard-cause-memory-ram-oom-issues-especially-in-pytorch) is not 100% related but I am curious about different sources of OMM memory issues that make the OS kill my jobs. – Charlie Parker May 16 '21 at 18:45

1 Answers1

2

When you call printf for printing to the terminal, the standard library will use line buffering and wait until a newline character to write the output. The size will depend on the implementation (might be 8K). See: In C, what's the size of stdout buffer? . But this memory usage doesn't grow over time.

When written (via a write syscall), the buffer will get copied through pipes and ptys to end up in the terminal emulator, which then displays it on the screen. Aside from the scrollback buffer of the terminal emulator, it does not accumulate anywhere along this path.

Most terminal emulators will have a limit for the scrollback buffer, that defaults to a few thousand lines. The old lines would be presumably deallocated after this limit. Some terminal emulators provide an option to remove the limit, which means it could grow until OOM (I believe on macOS, the Terminal app actually handles this event to clear the scrollback buffer) and the terminal emulator might be killed by the OOM-killer. From the OS perspective, it is not different than any other inter-process communication.

A container may only affect the creation of the pipes. It is still the process calling the printf, sending the resulting buffer to the terminal emulator process, through the kernel.

mkayaalp
  • 2,631
  • 10
  • 17