[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Memory leak in 5.1 when redeclaring arrays
From: |
Alexander Mescheryakov |
Subject: |
Memory leak in 5.1 when redeclaring arrays |
Date: |
Tue, 12 Jan 2021 13:51:39 +0300 |
Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -march=x86-64 -mtune=generic -O2 -pipe -fno-plt
-DDEFAULT_PATH_VALUE='/usr/local/sbin:/usr/local/bin:/usr/bin'
-DSTANDARD_UTILS_PATH='/usr/bin' -DSYS_BASHRC='/etc/bash.bashrc'
-DSYS_BASH_LOGOUT='/etc/bash.bash_logout' -DNON_INTERACTIVE_LOGIN_SHELLS
uname output: Linux Valhalla 5.9.14-arch1-1 #1 SMP PREEMPT Sat, 12 Dec 2020
14:37:12 +0000 x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu
Bash Version: 5.1
Patch Level: 4
Release Status: release
Description:
Redeclaring array variable leads to leaked memory in bash 5.1
It is required to define var as `declare -a FOO=(a b c)`
defining it simply as `FOO=(a b c)` does not introduce memory leak.
The same actions in bash 5.0 do not lead to memory leak.
Repeat-By:
Here is the script to reproduce the issue:
```
#!/bin/bash
SOURCE_FILE=/tmp/strings
echo 'declare -a STRINGS=(' > "$SOURCE_FILE"
for i in {1..100}; do
echo 'qwertyuiopasdfghjklzxcvbnm' >> "$SOURCE_FILE"
done
echo ')' >> "$SOURCE_FILE"
for i in {1..10000}; do
if [ $((i%1000)) -eq 0 ]; then
printf '%d\t%s\n' "$((i/1000))" "$(grep VmRSS /proc/$$/status)"
fi
source "$SOURCE_FILE"
done
```
Sample output with bash 5.1:
1 VmRSS: 23276 kB
2 VmRSS: 41916 kB
3 VmRSS: 60688 kB
4 VmRSS: 79452 kB
5 VmRSS: 98088 kB
6 VmRSS: 116988 kB
7 VmRSS: 135628 kB
8 VmRSS: 154528 kB
9 VmRSS: 173296 kB
10 VmRSS: 191932 kB
For comparison the same script under bash 5.0 does not leak:
$ bash-5.0/bash ~/test_bash_memleak.sh
1 VmRSS: 6124 kB
2 VmRSS: 6204 kB
3 VmRSS: 6204 kB
4 VmRSS: 6204 kB
5 VmRSS: 6204 kB
6 VmRSS: 6204 kB
7 VmRSS: 6204 kB
8 VmRSS: 6204 kB
9 VmRSS: 6204 kB
10 VmRSS: 6204 kB
- Memory leak in 5.1 when redeclaring arrays,
Alexander Mescheryakov <=