Skip to content

Instantly share code, notes, and snippets.

@Saruspete
Created November 3, 2017 22:05
Show Gist options
  • Select an option

  • Save Saruspete/69b283f9a55697de979d0943212fff47 to your computer and use it in GitHub Desktop.

Select an option

Save Saruspete/69b283f9a55697de979d0943212fff47 to your computer and use it in GitHub Desktop.
memory leak
#!/bin/bash
# Memory leak induced by the idiom "cmd | read var"
# but not by "read var < <(cmd)"
# Fixed in bash 4.4
# use strict
set -u
# Also fails with no monitor + lastpipe
#set +m
#shopt -s lastpipe
typeset -i i=0
typeset -i max=10000
function getrss { grep VmRSS /proc/$$/status; }
function getdata { echo 1 2 3 4 5; }
typeset rss_start="$(getrss)"
echo "Before loop: RSS = $rss_start"
while [[ $i -lt $max ]]; do
typeset rss_curr="$(getrss)"
[[ "$rss_start" != "$rss_curr" ]] && {
echo "New mem usage at loop $i: $rss_start => $rss_curr"
rss_start=$rss_curr
}
# First half, let's try the working method
if [[ $i -lt $(( $max/2 )) ]]; then
# That bashism is fine
read some data < <(getdata)
else
# That one leaks something
getdata | read some data
fi
i=i+1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment