On 11/6/14 8:09 AM, Jean Delvare wrote:
> Hi all,
> 
> A memory leak has been reported in a bash script I maintain [1]. After
> investigation, I was able to shrink the test case down to:
> 
> while true
> do
>       sleep 1 &
>       wait $!
> done

This isn't a memory leak, and the memory use is bounded.  The shell, as
per Posix, keeps the statues of the last child_max processes.  It gets
child_max from the process's resource limit (ulimit -v, 709 (?) on my
system).  The list is FIFO, so when the number of background statuses
equals child_max, the oldest statuses are discarded.

Posix allows the shell to discard saved status values if $! isn't
referenced before another async job is started, but bash doesn't try
to do that.

> The above loop has an always-rising memory consumption (RSS value as
> reported by ps -o rss.) This one OTOH:
> 
> while true
> do
>       sleep 1 &
>       wait
> done

When you wait for all background processes, the shell, as per Posix,
discards all saved statuses.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    c...@case.edu    http://cnswww.cns.cwru.edu/~chet/

Reply via email to