We should use stream output instead of directly calling println which can avoid this oom issue.
The patch did not add any unit test cases given that we did not have any functional unit tests for fetching logs from running container. But I did the manual test to make sure the fix works.
1) Run sleep job
2) override YARN_CLIENT_OPTS and set a very low Xmx value
3) run yarn logs command (without the fix), and we could see the exact oom exception as shown in the description
4) run yarn logs command (with the fix), and we could not see the oom exception anymore.