Linux

Python memory management

In this page we will discuss various topics like different type of scope of variables and how python interpreter uses run-time stack and heap.

Scope

Scope refers to a "part of program" where a collection of identifiers are visible. A scope is just the range in which a variable can be modified or change in
The identifier must be defined using id = ...
In previous chapters we explored about variables, assignment operator, but now we have to explore little bit more.
if i write, for instance
k = 9
You would say k equals to 9. It is careless saying. The meaning of above expression is that k is a reference that points to an object that contains 9 inside it. In simple words references have names; and objects are pointed to by references.

Local scope

Consider the below program, contain several scopes. The light yellow region is called the Local scope. What is local scope ? The local scope is the scope of function that the Python interpreter is currently executing. When you reference an identifier in your code, Python interpreter first examines the local scope to if the identifier is defined there. See the below code, While executing line 7, the reference k is used, if Python interpreter finds k in the local scope, it looks up the corresponding value and retrieves it.
Scope of variable
Scope of variable
Click to view code


Enclosing Scope

If Python interpreter does not find the k within the local scope, it will examine the Enclosing Scope to see if it can get k there. While excuting line 7, the enclosing scope is the light green. The identifier defined in this enclosing scope include list1. While executing line 4 the light green region is the local scope. Every function definition defines a different scope.
It must be clear that the scope does not include the function name itself, but it includes the function's parameter and body


Global scope

The scope outside of all functions is called the global scope. In above figure k is defined in global scope.

Buit-In scope

The last scope in Python is the Built-In scope. The Global scope is limited with in the module. Consider if an identifier is not found within any of the scopes including Global scope within the modules, then Python interpeter will examine the built-in identifier to check if it is defined there. For a moment, consider the identifier str() If you were to write the following:
z = str(34)
Python would first examine in the local scope to check if str were defined. If str is not found within the local scope, Python would go into all enclosing scopes. If str is not found in the enclosing scope then Python would examine the Buit-In function, where it would surly find the str function or class.
Now you know about scope, it should now be clear, that's why it is always said don't use identifiers that is already exists in the buit-in scope


The Run-time stack and the Heap

As we learned about scopes. Local scope of function, contained parameters and variables, must be stored someplace in the RAM of Computer. Python divided the RAM into tow parts called Run-time stack and the Heap.
The activation record is a "chunk of memory" (a bunch of buckets) which contains all the information necessary to keep track of a "function call". The run-time stack is a stack of Activation Records. When a function is called the Python interpreter pushes the activation record of function onto the run-time stack. When a function returns, its corresponding activation record is popped from the run-time stack.
The Heap is area of RAM where all values (objects) are stored. The run-time stack never contains the object. The run-time stack store only references pointed to corresponding objects in the heap.
Python Run-time stack and heap
Python Run-time stack and heap


When Python executing lines 7,8 and 9 of the code, the run-time stack looks as shown in figure above. There are three activation records on run-time stack. Python Interpreter first pushed the module Activation Record, While executing module the Python interpreter went through top to bottom and put variable definition of module scope into the activation record of module. Activation record of module consisted of the reference k to the value 4.

When you write
k = 4
it means K is reference in stack, binds to object containing 4, stored in heap. When you use del()built-in function to delete k it deletes the binding. Object containing 4 on the heap is also reclaimed by the garbage collector because there are no references pointing at it anymore.
At the end of module the main function is called. When main function was called python interpreter pushed the activation record of main function. The main function activation record consisted of Variable, list1, defined in the main function. At the end of main function the add function is called, then activation record of add function is pushed onto the stack.






admin