bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bash memory leak when querying associative array for nonexisting element


From: Ralf Oehler
Subject: bash memory leak when querying associative array for nonexisting element
Date: Sun, 20 Nov 2022 14:02:52 +0100

Configuration Information [Automatically generated, do not change]:
Machine: aarch64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -mbranch-protection=standard -O2 -Wall -U_FORTIFY_SOURCE 
-D_FORTIFY_SOURCE=3 -fstack-protector-strong -funwind-tables 
-fasynchronous-unwind-tables -fstack-clash-protection -Werror=return-type 
-flto=auto -g  -D_GNU_SOURCE -DRECYCLES_PIDS -Wall -g -Wuninitialized -Wextra 
-Wno-switch-enum -Wno-unused-variable -Wno-unused-parameter -Wno-parentheses 
-ftree-loop-linear -pipe -DBNC382214=0 -DIMPORT_FUNCTIONS_DEF=0
uname output: Linux gate3 6.0.3-1-default #1 SMP PREEMPT_DYNAMIC Sat Oct 22 
07:43:26 UTC 2022 (f00a35f) aarch64 aarch64 aarch64 GNU/Linux
Machine Type: aarch64-suse-linux-gnu

Bash Version: 5.2
Patch Level: 2
Release Status: release

Description:
        [Detailed description of the problem, suggestion, or complaint.]
        I want to read an associative array. Like so: y="${aa[$i]}"
        If aa contains the element queried, then everything works as expected
        If aa does not contain such an element, the result is the empty string, 
which is expected, but the query permanently increases the memory consumption 
of the executing bash, which is not expected. The program below demonstrates 
this behaviour. When run, the bash process increases its memory indefinitely.

        I did not find a fix.
        The only workaround I could think of is to somehow avoid accessing 
nonexistant keys, but this would induce an ugly programmatic overhead and 
destroy the beauty of the associative arrays.
        I think, the ability to write
                if [ -z ${aa[$i]} ]; then
                        aa[$i]="new value"
                fi
        is something that really should be possible without causing an 
ever-increasing memory footprint.
        What is happening here? Unneccessarily increasing hash tables?

Repeat-By:
        [Describe the sequence of events that causes the problem
        to occur.]

############################### program begin ##################
#!/bin/bash

# associative array
declare -A aa

# fill array with something. This makes the memory leak more pronounced.
for i in $(seq 1 10000); do
        aa[$i]="v$i"
done

# loop to show the memory leak
while true; do

        # print the amount of memory this program currently uses
        grep VmSize /proc/$$/status     >/tmp/x
        read x Mem x                    </tmp/x
        echo "Mem=$Mem"

        # do the thing that leaks memory
        for i in ${!aa[*]}; do
#               key="$i"                # query existing elements -> no memory 
leak
                key="xyz"               # query nonexisting elements -> memory 
leak
                value="${aa[$key]}"             # this is the evil line that 
leaks memory
        done

done
######################### program end #########################

Attachment: pgpx1R2VmjX6f.pgp
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]