Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

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

0
rwniceing
Asked:
rwniceing
  • 4
  • 3
  • 2
2 Solutions
 
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
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now