0

I use a script that consumes a lot of ram memory to such an extent that it freezes my computer and gives an error.

How could you limit the memory usage of this particular script?

I use debian 9 (linux)

thanks.

this is the basic script

path="/home/xxx"
mesMenosUnDia=$(date +%m --date='-1 month')
fecha=$(date +"%Y-$mesMenosUnDia-%d")

echo "find"
list=$(find /home/xxx -type f)
listArray=($list)


for i in "${listArray[@]}"
do
        onlyDate=$(echo $i | grep -P '\d{4}\-\d{2}\-\d{2}' -o)
        if [[  $onlyDate < $fecha ]];then

                rm $i
        else

        fi
done
5
  • Add your script to your question. Commented Sep 3, 2018 at 10:46
  • its added, its basic Commented Sep 3, 2018 at 10:50
  • By the way, limiting the usable memory with ulimit does not solve your real problem. Commented Sep 3, 2018 at 11:26
  • Instead of saving the result of find in a variable (and an array), couldn’t you pipe it to a while read …? Commented Sep 3, 2018 at 11:56
  • @Cyrus so ulimit doesn't limit the memory usage of a script like op asked? When does ulimit work and when it doesn't? Commented Jul 18, 2022 at 5:08

1 Answer 1

1

You can limit the maximum memory to be used with ulimit. The drawback is that the script will hit the limit and die. But your computer will not hang, because when the script asks for too much memory, it will be killed.

You script is using too much memory because you are storing too many things in memory. Specifically, you grab the output of the find command into a variable and the you copy all of this data into an array, so the whole content gets duplicated.

Instead of keeping everything in memory, put it in disk.

path="/home/xxx"
mesMenosUnDia=$(date +%m --date='-1 month')
fecha=$(date +"%Y-$mesMenosUnDia-%d")

echo "find"
find /home/xxx -type f > tmp


for i in $(<tmp)
do
        onlyDate=$(echo $i | grep -P '\d{4}\-\d{2}\-\d{2}' -o)
        if [[  $onlyDate < $fecha ]];then

                rm $i
        else

        fi
done

rm tmp
Sign up to request clarification or add additional context in comments.

1 Comment

thank you, it was that, I have already solved it. thank you very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.