GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Python - Memory Management
Selected Reading
Python - Memory ManagementIn Python, memory management is automatic, it involves handling a private heap that contains all Python objects and data structures. The Python memory manager internally ensures the efficient allocation and deallocation of this memory. This tutorial will explore Python's memory management mechanisms, including garbage collection, reference counting, and how variables are stored on the stack and heap. Memory Management ComponentsPython's memory management components provides efficient and effective utilization of memory resources throughout the execution of Python programs. Python has three memory management components −
Memory Allocation in PythonPython manages memory allocation in two primary ways − Stack and Heap. Stack − Static Memory AllocationIn static memory allocation, memory is allocated at compile time and stored in the stack. This is typical for function call stacks and variable references. The stack is a region of memory used for storing local variables and function call information. It operates on a Last-In-First-Out (LIFO) basis, where the most recently added item is the first to be removed. The stack is generally used for variables of primitive data types, such as numbers, booleans, and characters. These variables have a fixed memory size, which is known at compile-time. Example Let us look at an example to illustrate how variables of primitive types are stored on the stack. In the above example, variables named x, y, and z are local variables within the function named example_function(). They are stored on the stack, and when the function execution completes, they are automatically removed from the stack. def my_function(): x = 5 y = True z = 'Hello' return x, y, z print(my_function()) print(x, y, z) On executing the above program, you will get the following output − (5, True, 'Hello')
Traceback (most recent call last):
File "/home/cg/root/71937/main.py", line 8, in <module>
print(x, y, z)
NameError: name 'x' is not defined
Heap − Dynamic Memory AllocationDynamic memory allocation occurs at runtime for objects and data structures of non-primitive types. The actual data of these objects is stored in the heap, while references to them are stored on the stack. Example Let's observe an example for creating a list dynamically allocates memory in the heap. a = [0]*10 print(a) Output On executing the above program, you will get the following results − [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] Garbage Collection in PythonGarbage Collection in Python is the process of automatically freeing up memory that is no longer in use by objects, making it available for other objects. Pythons garbage collector runs during program execution and activates when an object's reference count drops to zero. Reference CountingPython's primary garbage collection mechanism is reference counting. Every object in Python maintains a reference count that tracks how many aliases (or references) point to it. When an object's reference count drops to zero, the garbage collector deallocates the object. Working of the reference counting as follows −
Example Here is an example that demonstrates working of reference counting in Python. import sys
# Create a string object
name = "Tutorialspoint"
print("Initial reference count:", sys.getrefcount(name))
# Assign the same string to another variable
other_name = "Tutorialspoint"
print("Reference count after assignment:", sys.getrefcount(name))
# Concatenate the string with another string
string_sum = name + ' Python'
print("Reference count after concatenation:", sys.getrefcount(name))
# Put the name inside a list multiple times
list_of_names = [name, name, name]
print("Reference count after creating a list with 'name' 3 times:", sys.getrefcount(name))
# Deleting one more reference to 'name'
del other_name
print("Reference count after deleting 'other_name':", sys.getrefcount(name))
# Deleting the list reference
del list_of_names
print("Reference count after deleting the list:", sys.getrefcount(name))
Output On executing the above program, you will get the following results − Initial reference count: 4 Reference count after assignment: 5 Reference count after concatenation: 5 Reference count after creating a list with 'name' 3 times: 8 Reference count after deleting 'other_name': 7 Reference count after deleting the list: 4 Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |