Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Garbage collection in bash

Tags:

linux

bash

Does bash run a garbage collector? Can it be controlled via some command line options? I can't find anything on the net about this.

I have a bash script that runs and over a few days its memory usage increases. I want to know where the memory is going.

like image 200
sashang Avatar asked Feb 11 '13 05:02

sashang


People also ask

What is garbage collection in Linux?

Garbage Collection (GC) is a mechanism that provides automatic memory reclamation for unused memory blocks. Programmers dynamically allocate memory, but when a block is no longer needed, they do not have to return it to the system explicitly with a free() call.

What is $1 and $2 in bash?

$1 - The first argument sent to the script. $2 - The second argument sent to the script.

Does Linux have garbage collection?

The Linux kernel garbage collection system is designed to prevent memory exhaustion in this particular scenario. The inflight count was implemented to identify potential garbage. Each time the reference count is increased due to an SCM_RIGHTS datagram being sent, the inflight count will also be incremented.

What is $() in bash?

$() means: "first evaluate this, and then evaluate the rest of the line". Ex : echo $(pwd)/myFile.txt. will be interpreted as echo /my/path/myFile.txt. On the other hand ${} expands a variable.


1 Answers

Bash does not run a garbage collector as such. Since it has no concept of references, there is no need to find data without references. It does free memory no longer in use, though.

Here's a simple demonstration of memory usage before and after declaring and overwriting a large variable. Memory usage goes up then down again:

ps -o rss -p $$
var=$(printf "%s\n" {1..100000})
ps -o rss -p $$
var="smallstring"
ps -o rss -p $$
like image 77
that other guy Avatar answered Sep 27 '22 23:09

that other guy