kareman / SwiftShell

A Swift framework for shell scripting.
https://kareman.github.io/SwiftShell
MIT License
1.03k stars 87 forks source link

memory leak and too many open files #39

Closed jimmyzzzzzzz closed 7 years ago

jimmyzzzzzzz commented 7 years ago

In a new project, I'm using a command line tool project. I'm using source code in my project

for i in 1...99999999 {
    run("ls")
}

It will crash at around 2000th iteration on my machine because the OS cannot open more files. During its execution, the memory increased from 5MB to 50MB and it had more than 6000 open files before crashing. Can anyone reproduce this? or am I using it incorrectly?

kareman commented 7 years ago

I assume this is for stress testing purposes?

Each run opens 2 Pipes (for stdout and stderror), and each of those has 2 FileHandles. I assume this is where the open files come from. To test this, try using runAndPrint instead, it uses your application's own stdout and stderror.

Even so, I don't think the files should be kept open after the output from run has been discarded. I'm on Easter vacation for 1,5 weeks now, but I'll take a look at it when I get back.

jimmyzzzzzzz commented 7 years ago

After more investigation on this issue recently, the memory leak seems to be inevitable. Even if I create a simple Process without a Pipe and set its stdin, stdout and stderr to nil, it still leaks after its termination. The problem seems to be related to apple's own Process Api. However the open files can be fixed by closing all file handles after process terminates and it resolves the main source of memory leaks in my case.

kareman commented 7 years ago

Thanks, I'll look into having swiftshell close all file handles manually after a Process finishes.