1

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
1
  • @CalumHalpin, thanks for your suggestion. I didn't have time to do any testing initially and I didn't want to post a guess as an answer. I've done some testing now and I've submitted an answer. Commented Feb 1, 2023 at 17:55

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the information, using wait work perfectly. The script starts with more memory but only goes up once and stays there, I left it running overnight and it is the same.
Additional information: If I use wait, when I close one of the programs the script no longer moves the windows until I have all 3 running again, which is fine for my purpose because it is meant for when I have all 3 running. When I use the subprocess solution the memory doesn't increase but when I close one of the programs, it starts spawning multiple instances of the script process until I reopen the program.
@Gary, its' good that one of the options worked for you. If you run into problems in future you might find ProcessManagement - Greg's Wiki to be useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.