bug-bash
[Top][All Lists]
Advanced

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

Memory leak with associative arrays


From: David Parks
Subject: Memory leak with associative arrays
Date: Tue, 4 Oct 2011 11:48:57 -0700

Version: GNU bash, version 4.2.8(1)-release (x86_64-pc-linux-gnu) on Ubuntu
10.04

If I set an associative array, as in: 

MYARRAY["something"]="Goobledygook"

Then I set that same variable name again later (and so on in a loop). The
earlier variables are never released from memory, before I re-set a given
item I MUST call unset on the item first, possibly storing the value to a
temp variable if it's needed (such as is the case with a counter, for
example).

The scripts below demonstrate a case which exposes this memory leak, and one
that works around it with a store/unset/set operation, respectively.

--------------------------------------

#!/bin/bash
#Memory leak exposed in associative array
while true; do
        declare -A DB
        i=0
        while (( i < 200000 )); do
                DB["static"]="Any old gobbledygook"
                if (( $i == 100000 )); then echo 100k; fi
                i=$(($i+1))
        done
        echo "Clearing DB";
        unset DB
done

--------------------------------------

#!/bin/bash
#Memory leak worked around with store/unset/set operation
while true; do
        declare -A DB
        DB["static"]="Any old gobbledygook"
        i=0
        while (( i < 200000 )); do
                TEMP=${DB["static"]}
                unset DB["static"]
                DB["static"]=$TEMP
                if (( $i == 100000 )); then echo 100k, DB val is:
${DB["static"]}; fi
       i=$(($i+1))
        done
        echo "Clearing DB";
        unset DB
done






reply via email to

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