bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Memory leak in wait


From: Chet Ramey
Subject: Re: Memory leak in wait
Date: Thu, 06 Nov 2014 20:57:11 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:31.0) Gecko/20100101 Thunderbird/31.2.0

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    chet@case.edu    http://cnswww.cns.cwru.edu/~chet/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]