Link to home
Start Free TrialLog in
Avatar of rwniceing
rwniceing

asked on

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

Avatar of ozo
ozo
Flag of United States of America image

{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 ));
Avatar of rwniceing
rwniceing

ASKER

no short hand writing for 200000000 ?
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sysctl vm.overcommit_memory=1 >> /etc/sysctl.conf
will allow to allocate memory lazy way so it is not really allocated unless accessed.
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.