Python: Memory Management

Modules & Packages

Sacha Knowledge Exchange Session

Topic

Vijayalakshmi

kvijay62@ford.com

0.What is Memory management?

1. Memory allocation in python

    - Stack

    - Heap

Efficient Resource Handling in Python

Python's Memory Manager

  • Dynamic Typing
  • Reference Counting

Garbage Collector

(Python objects &

data structure)

Memory Manager

Python Program

Interpreter

Private Heap Space

Reference Counting

refcount = 1

refcount = 2

refcount = 3

Garbage Collection (GC)

  • Purpose: Automatically deallocates memory by removing objects that are no longer in use.
  • Mechanisms:
    • Reference Counting: Immediate deallocation when reference count drops to zero.
    • Generational GC: Divides objects into generations (young, middle-aged, old) to optimize the collection process.

Modules: gc, sys, tracemalloc.

  • gc module: Provides control over garbage collection.
  • sys module: Allows for interrogation of memory usage.
  • tracemalloc module: Tracks memory allocations for detecting leaks.

Global Interpreter Lock (GIL)

How it Works?

 - Single Thread Execution

 - Switching Between Threads

What?

What is Modules?

Importing Modules

Standard Library

How to create Own Modules?

What is packages?

Build your own package

Any Queries?

MM-Module-packages

By viji sulochana