0% found this document useful (0 votes)
3 views2 pages

Memory Mapping in Python Explained

The article discusses the importance of understanding memory mapping in Python programming, detailing its structure, including stack, heap, code, and static data sections. It explains Python's memory management system, which utilizes reference counting and automatic garbage collection, and demonstrates how to implement memory mapping using the built-in `id()` function. The paper emphasizes that grasping memory mapping is crucial for writing efficient Python programs.

Uploaded by

lorenzors135
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Memory Mapping in Python Explained

The article discusses the importance of understanding memory mapping in Python programming, detailing its structure, including stack, heap, code, and static data sections. It explains Python's memory management system, which utilizes reference counting and automatic garbage collection, and demonstrates how to implement memory mapping using the built-in `id()` function. The paper emphasizes that grasping memory mapping is crucial for writing efficient Python programs.

Uploaded by

lorenzors135
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Ali Albuloushi.

International Journal of Engineering Research and Applications


[Link]
ISSN: 2248-9622, Vol. 13, Issue 5, May 2023, pp. 80-81

RESEARCH ARTICLE OPEN ACCESS

Memory Mapping in Python


Ali Albuloushi
Computer Department – Higher Institute of Communications and Navigation Studies - Kuwait

----------------------------------------------------------------------------------------------------------------------------- ----------
Date of Submission: 02-05-2023 Date of acceptance: 12-05-2023
----------------------------------------------------------------------------------------------------------------------------- ----------

I. Introduction: collected by the interpreter when their reference


In computer programming, understanding count reaches zero.
the memory map of a process is an essential aspect
of software development. A memory map is a Python uses a private heap space for storing objects,
diagram that represents various memory areas and when an object is created in Python, it is
occupied by a program. In Python, it is essential to allocated in the heap space. Python's memory
learn how a memory map works and how it can be management system provides several features, such
represented with code. This paper will explore as automatic garbage collection and efficient
Python's memory map, including its structure, and memory usage.
how it can be implemented with code.
Implementing Memory Map in Python:
Understanding Memory Map:
To implement the memory map in Python, the built-
To better understand Python's memory map, it is in `id()` function can be used. This function returns
essential to understand its structure. The memory the unique identifier of an object, which represents
map in Python can be divided into different sections, the memory address of the object.
which include the following:
#Example 1: Memory Map of Variables
1. Stack: This is the section of the memory that x = 10
stores all the variables declared within a function. y = "hello"
The stack grows from the high address to the low z = [1, 2, 3]
address. print("Memory address of x:", id(x))
print("Memory address of y:", id(y))
2. Heap: This is the section of the memory that print("Memory address of z", id(z))
stores all the dynamic memory allocations, such as
lists and dictionaries. The heap grows from the low
address to the high address. The output of the above code will be as follows:

3. Code: This section of the memory stores the Memory address of x: 140705858303836
compiled bytecode of the program. Memory address of y: 140705726650284
Memory address of z: 140705736197788
4. Static Data: This section of the memory stores all
the static memory allocations, such as global The above code shows the memory addresses of
variables. variables `x`, `y`, and `z`. The `id()` function also
shows that the memory addresses of variables `x`
Python's Memory Management: and `z` are contiguous, which indicates that they are
stored in the same section of the memory.
Python has its own memory management system
that automatically manages the allocation and #Example 2: Memory Map of Lists
deallocation of memory. Python's memory a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
management system is based on reference counting, print("Memory address of a:", id(a))
which means that objects are automatically garbage print("Memory address of a[0]:", id(a[0]))
print("Memory address of a[1]:", id(a[1]))
print("Memory address of a[2]:", id(a[2]))

[Link] DOI: 10.9790/9622-13058081 80 | Page


Ali Albuloushi. International Journal of Engineering Research and Applications
[Link]
ISSN: 2248-9622, Vol. 13, Issue 5, May 2023, pp. 80-81

The output of the above code will be as follows:

Memory address of a: 140705603577944


Memory address of a[0]: 140705565063064
Memory address of a[1]: 140705565063128
Memory address of a[2]: 140705565063192

The above code shows that the memory addresses of


the list `a` and its elements are not contiguous. This
indicates that the list elements are stored in the heap
section of the memory.

II. Conclusion:

Python's memory map plays an essential


role in software development. Understanding how
Python's memory map works and how to represent it
with code is crucial in writing efficient and effective
Python programs. By using the built-in `id()`
function, we can easily access the memory
addresses of objects in Python. Overall,
understanding the memory map is an essential
aspect of Python programming.

REFERENCES

[1]. Python Official Documentation


([Link]
[2]. DigitalOcean Tutorials
([Link]
torials/python-id)
[3]. Real Python Tutorials
([Link]
management)

[Link] DOI: 10.9790/9622-13058081 81 | Page

You might also like