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
--------------
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
ASKER
no short hand writing for 200000000 ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This two sites may assist the question-1 for nature log with bc command for 2e8
http://advantage-bash.blogspot.hk/2012/03/logarithm-and-anti-logarithm.htmlhttp://www.linuxfromscratch.org/blfs/view/7.4/general/bc.html
http://advantage-bash.blogspot.hk/2012/03/logarithm-and-anti-logarithm.htmlhttp://www.linuxfromscratch.org/blfs/view/7.4/general/bc.html
sysctl vm.overcommit_memory=1 >> /etc/sysctl.conf
will allow to allocate memory lazy way so it is not really allocated unless accessed.
will allow to allocate memory lazy way so it is not really allocated unless accessed.
ASKER
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Now it works for question-1 with bc command and question-2
Thanks for your reply and the code need to be checked for other value
#!/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
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.
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.
it would be much more memory efficient to use something like
for (( i=1; i<=200000000; ++i ));