bugThe GNU Bourne-Again SHell - Bugs: bug #67622, memory not freed after array unset

 
 

bug #67622: memory not freed after array unset

Submitter:  Kunzol <kunzol>
Submitted:  Thu 23 Oct 2025 03:55:38 PM UTC
   
 
Category:  None Severity:  3 - Normal
Priority:  5 - Normal Item Group:  None
Status:  Invalid Privacy:  Public
Assigned to:  None Open/Closed:  Open
* Mandatory Fields

Post a Comment

Add a New Comment Rich Markup
   

Discussion

Fri 24 Oct 2025 05:25:15 AM UTC, comment #2: 

Many thanks for answering, I really appreciate it!

I guess I didn't use the right search words to find this.

Greetings

Kunzol <kunzol>
Thu 23 Oct 2025 07:52:28 PM UTC, comment #1: 

Here's what I wrote one of the other times this came up:

==========
That's not a memory leak.  Malloc implementations need not release
memory back to the kernel; the bash malloc (and others) do so only
under limited circumstances.  Memory obtained from the kernel using
mmap or sbrk and kept in a cache by a malloc implementation doesn't
constitute a leak.  A leak is memory for which there is no longer a
handle, by the application or by malloc itself.
==========

In the first case, the memory can be released back to the kernel because it's allocated and used in one chunk (a string). In the second, the same amount of memory is allocated, but it's chopped up into individual words (the words that are the results of the brace expansion, which become arguments for the declare builtin) and can't be freed in one chunk.

If you put those commands in a script and run it with valgrind, you'll see that there aren't any leaks. (You'll have to build bash for valgrind first with `make valgrind'.)

Chet Ramey <chet>
Group administrator
Thu 23 Oct 2025 03:55:38 PM UTC, original submission:  

Hello,

I just detected, that memory is not freed if a array variable is unset or overwritten.
String variables do not have this issue. Memory is freed as expected.

To make sure it is in the newest bash version, I cloned the git and compiled the master branch (5.3).

Steps to reproduce:

$ echo $BASH_VERSION
5.3.3(1)-release
$ ps -o rss -p $$
  RSS
 6616
$ var="$( printf "%s\n" {1..1000000} )"
$ ps -o rss -p $$
  RSS
13288
$ var=""
$ ps -o rss -p $$
  RSS
 6608
$ unset -v var
$ ps -o rss -p $$
  RSS
 6608
$ declare -a avar=( {1..1000000} )
$ ps -o rss -p $$
  RSS
693720
$ declare -a avar=()
$ ps -o rss -p $$
  RSS
693720
$ unset -v avar
$ ps -o rss -p $$
  RSS
693720
$ declare -p avar
bash: declare: avar: not found


Configuration Information:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -g -O2
uname output: Linux ryzen 6.8.0-64-generic #67~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue Jun 24 15:19:46 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu

Bash Version: 5.3
Patch Level: 3
Release Status: release

Thanks for reading!
Greetings

Kunzol <kunzol>

 

Attached Files

This item currently has no attached files.

(Note: upload size limit is set to 4.0MiB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

 

Dependencies

This item does not depend on any other items.

No items depend on this one.

 

Mail Notification Carbon-Copy List

Carbon-Copy List
  • -email is unavailable- added by chet (Posted a comment)
  • -email is unavailable- added by kunzol (Submitted the item)
  •  

    Votes

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.

     

    Please enter the title of George Orwell's famous dystopian book (it's a date):

    History

    Follows 1 latest change.

    Date Changed by Updated Field Previous Value => Replaced by
    2025-10-23 chet StatusNone Invalid

    Back to the top

    Powered by Savane 3.16-598c.
    Corresponding source code