Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Bash manage its memory?

For example, if I declare a variable a=8:

  • Where is it stored? in the running process heap?
  • When does it get free?
  • Who's responsible for that?
like image 791
Mano Mini Avatar asked Jan 08 '17 20:01

Mano Mini


People also ask

How to manage memory in Linux?

Let’s go over some of the commands for managing memory in Linux. The /proc/meminfo file contains all the information related to memory. To view this file use the cat command: This command outputs a lot of parameters related to memory. To get the physical memory from proc/meminfo file use: To get the virtual memory from /proc/meminfo file use: 2.

How does the operating system manage memory usage?

Memory management using virtual memory When the memory is allocated and de-allocated dynamically, the operating system must be able to manage it. So, to keep track of memory usage, the operating system, generally, uses two ways: Memory management using bitmap

How long does it take Linux memory management to clear cache?

To demonstrate this in Linux memory management, we will take a very simple example: I will create a small file with some random text: Next we will clear the buffers and cache. Now since the caches are clear, we see that it takes around 0.031 seconds to read the file from the disk

Why do we use virtual memory in Linux?

Now the idea is that if in Linux, when a process is loading, this process needs memory pointers, and these memory pointers don't really have to refer to actual physical RAM, and that is why we are using virtual memory. Virtual Memory is used by the Linux kernel to allow programs to make a memory reservation.


2 Answers

Bash variables like

a=8

are stored as heap memory, and they are never removed unless the user explicitly unset the variable -- so in essence the user is responsible for deleting it if it ever needs deleting.

In bash 2.05 variables are internally managed through a hash table, where memory for the hash table is obtained and release by "malloc" and "free". The removing of the elements from the hash table does not immediately remove the element from the hash table, but it is cleaned up through a garbage collection flush_hash_table that is called at key points in the execution.

Bash version 4.4 have a re-written some of the hash tables, and the flush_hash_table no longer exist, but is replaced with a functionhash_flush.

Hence different versions and port could behave differently and you should not rely on that the memory is actually immediately released even if you do an unset, or expect any particular behavior of memory when writing shell scripts

like image 53
Soren Avatar answered Oct 10 '22 20:10

Soren


Memory management isn't something you need to concern yourself with in any shell language. Suffice it to say, bash is responsible for the allocation and deallocation of any memory used.

All interpreted languages store their variables on the heap; even if they use a stack, that is dynamically allocated on the interpreter's heap as well.

like image 22
2 revs Avatar answered Oct 10 '22 21:10

2 revs