2e8 from for loop and memory allocation in linux script

Dear Experts,

1- Could I use 2e8 for 200000000 for linux script ?
2- and I try the similar for loop in php, c,c++ and asp that is okay
no any memory allocation error.

But when run it on linux shell on my VPS server apache centos6, it came out error, Why ?
#./for
./for: xmalloc: cannot allocate 9 bytes (1873698816 bytes allocated)

How to solve this out

for file
--------------
#!/bin/bash
for i in {1..200000000}
do
   a=1234+5678+i
b=1234*5678+i
c=1234/2+i
#echo $i\n
done

Open in new window

LVL 1
rwniceingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
{1..200000000} will attempt to expand the entire list of 200000000 white space separated numbers
it would be much more memory efficient to use something like
for (( i=1; i<=200000000; ++i ));
0
rwniceingAuthor Commented:
no short hand writing for 200000000 ?
0
ozoCommented:
I'm not sure if you'd call this a short hand
e=`yes | head | tr -c 0 0`
for (( i=1; i<=2${e::8}; ++i )); do echo $i ; done

or
e=2$(for e in {1..8}; do echo -n 0 ; done)
for (( i=1; i<=$e; ++i )); do echo $i ; done

or
for (( i=1; i<=2$(yes 0 | head -8|tr -cd 0); ++i )); do echo $i ; done
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

rwniceingAuthor Commented:
0
gheistCommented:
sysctl vm.overcommit_memory=1 >> /etc/sysctl.conf
will allow to allocate memory lazy way so it is not really allocated unless accessed.
0
rwniceingAuthor Commented:
question-2 is solved by ozo after change for loop,for (( i=1; i<=200000000; ++i )); and the memory
allocation error is gone.

and it is interesting the memory usage for that for syntax is different  from "for i in (1 ..200000000)" that won't use back the memory from previous i, instead, it will create many copy until memory reach to the limit before i= 200000000.

And it seems solving question-1 needs more time
0
gheistCommented:
You cannot use 2e8

It is more like ${bc 2*10^8)
0
rwniceingAuthor Commented:
Now it works for question-1 with bc command  and question-2
#!/bin/bash
for ((i=1;i<=$[bc 2*10^6];i++))
do
a="$[1234+$5678+$i]"
b="$[1234*5678+$i]"
c="$[1234/2+$i]"
#echo Welcome $i\n
done
echo $a=$b=$c

Open in new window


Thanks for your reply and the code need to be checked for other value
0
ozoCommented:
I think
for ((i=1;i<=$[bc 2*10^6];i++))
would have to be
for ((i=1;i<=$(echo "2*10^6"|bc);i++))
But that would cause  an echo process and a bc process to be forked 2*10^6 times, so you probably wouldn't want to do it that way.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.