Home > Enterprise >  Why does this bash script use a constantly increasing amount of memory
Why does this bash script use a constantly increasing amount of memory

Time:02-02

Why does the memory use keep increasing in this script, the increase is small but adds up over time?

The script just resizes and move 3 application windows so they all show on one screen with no overlapping.

I also get the occasional error pidof: can't read from 415712/stat in the terminal although the script keeps on running.

#!/bin/bash

# Display Notepad full height on the left half of the screen
resizeNotepadqq() {
    # Wait for the process to start
    until pidof notepadqq-bin >> /dev/null; do   
        sleep 1
    done

    sleep 0.1
    # wmctrl -e <G>,<X>,<Y>,<W>,<H>
    # window Gravity: 0 = window default
    # window position XY coordinates
    # window Width and Height: -1 = unchanged
    wmctrl -xr "notepadqq-bin.Notepadqq" -e 0,0,0,-1,-1
    exit
}

# Display Terminal in the top right half of the screen
resizeTerminal() {
    until pidof gnome-terminal-server >> /dev/null; do   
        sleep 1
    done

    sleep 0.1
    wmctrl -xr "gnome-terminal-server" -e 0,640,0,646,335
    exit
}

# Display Celluloid in the bottom right half of the screen
resizeCelluloid() {
    until pidof celluloid >> /dev/null; do
        sleep 1
    done

    sleep 0.1
    wmctrl -xr "celluloid_player" -e 0,640,360,691,435
    exit
}

while true; do
    sleep 2
    resizeNotepadqq &
    resizeTerminal &
    resizeCelluloid &
done

CodePudding user response:

A possible cause of the problem is memory used by the shell to keep track of background processes. When a background process completes the shell does a wait system call to stop it becoming a zombie process and stores its exit status so it's available for a shell wait command. If the shell code never does a wait command for the process then the exit status is stored until the shell program exits. I've done some testing with a simpler program and verified that memory usage does grow slowly over time if background processes are not waited for.

One possible way to address the problem is to explicitly wait for the background processes:

resizeNotepadqq &
resizeTerminal &
resizeCelluloid &
wait

My testing shows that memory usage still grows a little over time with this (I guess because memory is still needed to store background process status, even if it is stored for only a short time), but much more slowly than before.

Another option is to do the backgrounding in a subprocess:

(
    resizeNotepadqq &
    resizeTerminal &
    resizeCelluloid &
)

The subprocess exits immediately after running the background processes, so any memory used for tracking background processes is never used, or is completely freed almost immediately. The top-level process doesn't run any background processes so it doesn't need to use any memory to hold exit statuses. My testing shows that this approach causes no growth in memory usage for the shell over time.

  •  Tags:  
  • bash
  • Related