bug-bash
[Top][All Lists]
Advanced

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

Re: Severe memleak in sequence expressions?


From: Greg Wooledge
Subject: Re: Severe memleak in sequence expressions?
Date: Wed, 30 Nov 2011 08:28:07 -0500
User-agent: Mutt/1.4.2.3i

On Wed, Nov 30, 2011 at 12:37:36AM +0100, Marc Schiffbauer wrote:
> echo {0..10000000}>/dev/null
> 
> This makes my system starting to swap as bash will use several GiB of
> memory.

Brace expansion is just a short way of typing a longer list of words.
If you type {0..9} bash still has to expand it out to 0 1 2 3 4 5 6 7 8 9
and then pass all of those words as arguments to echo.  (Granted, echo
is a builtin, so there *could* be some sort of shortcut there....)

> If I choose a way bigger number bash "just" seems to crash:
> 
> mschiff@lisa ~ $ echo {1..1000000000000000000}>/dev/null
> bash: xmalloc: cannot allocate 11892949016 bytes (135168 bytes allocated)
> mschiff@lisa ~ $
> 
> Is this a bug?

In my opinion, no.  You're asking bash to generate a list of words from 0
to 1000000000000000000 all at once.  It faithfully attempts to do so.

If you want to loop over integers, "for" is much better suited:

for ((i=1; i<=1000000000000000000; i++)); do
  whatever
done

This will not require that all of the words from 1 to 1000000000000000000
be generated in advance.  It will simply keep one word at a time in the
loop variable.

(Bear in mind that bash can only handle 64-bit integer arithmetic; and
bash 2.05 and earlier can only handle 32-bit integer arithmetic.)



reply via email to

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