Memory Management

A python program is a collection of

  1. Methods (operation, example while adding, add is the method)

  2. References (names given to access the value)

    ** The reference does not point to a value in Python but points to the memory address of an object

  3. Objects (everything is an object in python)

In python, memory is managed by the Python manager which determines where to put the application data in the memory

The Manager finds the free space and provides it to the application. The procedure of providing memory to objects is called allocation.

Basics

  • A bit is a binary digit, the smallest increment of data on a computer.

  • A bit can hold only one of two values: 0 or 1, corresponding to the electrical values of off or on

  • Because bits are so small, you rarely work with information one bit at a time.

  • Bits are usually assembled into a group of eight to form a byte.

  • A byte contains enough information to store a single ASCII character, like "h"

  • The abbreviations for numbers of bits use a lower-case "b" instead of an upper-case "B"

  • Speed of 3 Mbps internet speed is equivalent to .375 MBps (calculated as 3/8), significantly lower

    1 kilobyte (KB)

    1,024 bytes

    1 megabyte (MB)

    1,048,576 bytes

    1 gigabyte (GB)

    1,073,741,824 bytes

    1 terabyte (TB)

    1,099,511,627,776 bytes

    1 petabyte (PB)

    1,125,899,906,842,624 bytes

Python Memory Allocation

Memory allocation is a process, which basically allots free space in the computer's virtual memory, and there are two types of virtual memory that works while executing programs

  • Static Memory Allocation

    • Static memory allocation happens at the compile time

    • For example - In C/C++, we declare a static array with the fixed sizes

    • We cannot use the memory again in the further program

    • The stack data structure is used to store static memory

  • Dynamic Memory Allocation

    • Dynamic memory allocates the memory to the objects at the runtime of the program

    • We use the Heap to implement dynamic memory management

    • We can use the memory throughout the program

http://net-informations.com/faq/general/valuetype-referencetype.htm

Static memory allocation

Dynamic memory Allocation

Memory is allocated at compile time.

Memory is allocated while the program starts executing.

It is a faster way of memory allocation.

It is a slower way of memory allocation.

Once static memory is allocated, neither its size can be changed, nor it can be re-used. Hence, less efficient.

We can change the memory size after allocation and can be reused as well. Hence, more efficient.

In this case, variables get allocated permanently, and allocated memory remains blocked until the program terminates.

In this case, variables get allocated only when the program unit gets active and releases the memory when variables get out of scope.

Uses stack for memory management.

Uses heap for memory management.

Few important basics

  • Python does not have anything like ‘variables’, instead, it uses ‘names’

  • Python ‘name’ is just a label for an object and used to refer to a value.

  • We never specify ‘type’ information while creating an object.

  • A python ‘name’ can change its type.

  • A single Python object can have lots of names.

  • Two names will point to the same object if the id(x) method returns the same value.

Memory Allocation to Each Data-type

  • To get the size of an object in python, we can import the sys library and use it

  • Boolean: 28 bytes for True and 24 bytes for False (similar to 0)

  • Int: 24 bytes for a positive number, 28 bytes for a negative number, and 64 bytes for long

  • Float: 24 bytes for all float object types

  • String:

  • List: 64 bytes for an empty list and 8 bytes for each object added to the list

  • Set and dictionary requires more space in comparison to other data types

import sys

''' way to get the size 
 sys.getsizeof( variable_name )
'''

### size of boolean ###
sys.getsizeof( bool() ) # prints 24
sys.getsizeof(True) # prints 28
sys.getsizeof(False) # prints 24

### size of integer ###
sys.getsizeof( int() ) # prints 24
sys.getsizeof(0) # prints 24
sys.getsizeof(1) # prints 28
sys.getsizeof(-2) # prints 28
sys.getsizeof(2* 10**307) # prints 164

### size of float ###
sys.getsizeof( float() ) # prints 24
sys.getsizeof(0.0) # prints 24
sys.getsizeof(1e2) # prints 24
sys.getsizeof(1.123451234512345123451234512345e2) # prints 24
sys.getsizeof(1.123451234512345123451234512345e307) # prints 24
sys.getsizeof(1.123451234512345123451234512345e-307) # prints 24
sys.getsizeof(-100.25) # prints 24

### size of string ###

### size of list ###
sys.getsizeof( list() ) # prints 64
sys.getsizeof([]) # prints 64, 64 bytes for an empty list
sys.getsizeof([1]) # prints 72
sys.getsizeof([2,3]) # prints 80, 8 bytes for each element
a = ["Hello, let this be long string occupying more and more and more bytes", 1.2]
sys.getsizeof( a ) # prints 80

### size of set ###
sys.getsizeof( set() ) # prints 224
sys.getsizeof({1}) # prints 224
a = {"Some random string with abcd", "Some random string with efgh"} 
sys.getsizeof( a ) # prints 224
a = set( range(1,10) )
sys.getsizeof( a ) # prints 736

### size of dictionary ###
sys.getsizeof( dict() ) # prints 240
sys.getsizeof({1: -1}) # prints 240 
a = {"Some random string with abcd" : -1, "Some random string with efgh" : -2} 
sys.getsizeof( a ) # prints 240
a = dict( zip( range(1,10), range(-1,-10, -1) ) )
sys.getsizeof( a ) # prints 368

Python Garbage Collector

  • Python removes those objects that are no longer in use or can say that it frees up the memory space

  • This process of vanish the unnecessary object's memory space is called the Garbage Collector

  • The Python garbage collector initiates its execution with the program and is activated if the reference count to an object falls to zero

  • When we assign the new name or placed it in containers such as a dictionary or tuple, the reference count increases its value. If we reassign the reference to an object, the reference counts decrease its value. It also decreases its value when the object reference goes out of scope or an object is deleted

  • Python uses dynamic memory allocation which is managed by the heap data structure

  • Python memory manager manages the allocation or de-allocation of the heap memory space through the API functions

References for Further Reading

Last updated

Was this helpful?