Memory Handling and Garbage Collection in Python

16
Memory Handling & Garbage Collection in Python Prepared by: Ms. Pooja Mehta ITSNS Branch, GTU-CDAC-BISAG ME Program, Gandhinagar 1

Transcript of Memory Handling and Garbage Collection in Python

Page 1: Memory Handling and Garbage Collection in Python

Memory Handling &

Garbage Collection in PythonPrepared by:

Ms. Pooja MehtaITSNS Branch,

GTU-CDAC-BISAG ME Program,Gandhinagar

Page 2: Memory Handling and Garbage Collection in Python

May 3, 2023By: Pooja Mehta

2

Memory Management The process of binding values to

memory locations Whether done automatically (as in

Python), or partially by the programmer (as in C/C++), dynamic memory management is an important part of programming language design.

Page 3: Memory Handling and Garbage Collection in Python

May 3, 2023By: Pooja Mehta

3

Three Categories of Memory(for Data Storage)

Static: storage requirements are known prior to run time; lifetime is the entire program execution

Run-time stack: memory associated with active functions Structured as stack frames

Heap: dynamically allocated storage; the least organized and most dynamic storage area

Page 4: Memory Handling and Garbage Collection in Python

May 3, 2023By: Pooja Mehta

4

Cont..

Page 5: Memory Handling and Garbage Collection in Python

May 3, 2023By: Pooja Mehta

5

Cont..

Page 6: Memory Handling and Garbage Collection in Python

May 3, 2023By: Pooja Mehta

6

Memory Leaks and Garbage Collection

Three types of storage Static Stack Heap

Problems with heap storage: Memory leaks (garbage): failure to free storage

when pointers (references) are reassigned Dangling pointers: when storage is freed, but

references to the storage still exist.

Page 7: Memory Handling and Garbage Collection in Python

May 3, 2023By: Pooja Mehta

7

Garbage Any block of heap memory that cannot

be accessed by the program; i.e., there is no stack pointer to the block; but which the runtime system thinks is in use.

Garbage is created in several ways: A function ends without returning the space

allocated to a local array or other dynamic variable.

A node is deleted from a linked data structure, but isn’t freed

Page 8: Memory Handling and Garbage Collection in Python

May 3, 2023By: Pooja Mehta

8

Garbage Collection All inaccessible blocks of storage are

identified and returned to the free list. The heap may also be compacted at this

time: allocated space is compressed into one end of the heap, leaving all free space in a large block at the other end.

Page 9: Memory Handling and Garbage Collection in Python

May 3, 2023By: Pooja Mehta

9

Implementing Automated Garbage Collection If programmers were perfect, garbage

collection wouldn’t be needed.

There are three major approaches to automating the process: Reference counting Mark-sweep Copy collection

Page 10: Memory Handling and Garbage Collection in Python

Reference Counting10

Prior to Python version 2.0, the Python interpreter only used reference counting for memory management.

Reference counting works by counting the number of times an object is referenced by other objects in the system.

When references to an object are removed, the reference count for an object is decremented.

When the reference count becomes zero the object is de allocated.

May 3, 2023By: Pooja Mehta

Page 11: Memory Handling and Garbage Collection in Python

May 3, 2023By: Pooja Mehta

11

Mark and Sweep

Reference counting tries to find unreachable objects by finding objects with no incoming references.

Imprecise because we forget which references those are.

Page 12: Memory Handling and Garbage Collection in Python

May 3, 2023By: Pooja Mehta

12

Cont.. The root set is the set of memory

locations in the program that are known to be reachable.

Any objects reachable from the root set are reachable.

Any objects not reachable from the root set are not reachable.

Page 13: Memory Handling and Garbage Collection in Python

May 3, 2023By: Pooja Mehta

13

Cont..Marking phase: Find reachable objects. Add the root set to a worklist. While the worklist isn't empty:

Remove an object from the worklist. If it is not marked, mark it and add to the worklist all

objects reachable from that object.Sweeping phase: Reclaim free memory. For each allocated object:

If that object isn't marked, reclaim its memory. If the object is marked, unmark it (so on the next

mark-and sweep iteration we have to mark it again).

Page 14: Memory Handling and Garbage Collection in Python

May 3, 2023By: Pooja Mehta

14

Copy Collection Divide heap into two “semispaces”. Allocate from one space (fromspace) till full. Copy live data into other space (tospace). Switch roles of the spaces.

Requires fixing pointers to moved data (forwarding).

Eliminates fragmentation. DFS improves locality, while BFS does not require

any extra storage.

Page 15: Memory Handling and Garbage Collection in Python

References15

https://docs.python.org/2/c-api/memory.html

http://foobarnbaz.com/2012/07/08/understanding-python-variables/

https://www.digi.com/wiki/developer/index.php/Python_Garbage_Collection

May 3, 2023By: Pooja Mehta

Page 16: Memory Handling and Garbage Collection in Python

16

Thank you...!