In this post I am going to talk about memory management in general and also specific to Python. While I was gathering content on this subject, I comprehended that there is a lot of concepts which I want to touch upon. Therefore I decided to break this post down into two modules. In this module I will cover:
- What is Memory Management?
- What are Private Heaps?
- What are Run time stacks?
Memory management is a form of resource management for computer memory. What do I mean by that? Memory relates to “storing information“, whenever we are trying to learn something or read on something that information gets captured in our human memory (disclaimer: If we are paying attention) our memory acts like a storage facility, Similarly computers have a storage facility too, it store data in both volatile and non-volatile form. Issue is that it is finite and is that it’s an expensive resource so we need to utilize it properly. Our Computer memory is divided into 3 parts:
- RAM(Read Only Memory)-volatile
- EEPROM (Electrically Erasable Programmable Read-Only Memory)
Out of these our main focus is going to be RAM. Why RAM? RAM aids you computer performance. And How’s that? RAM has a lot to do with speed and performance of your entire system, It is a sort of extremely fast memory which stores data temporarily. So what processor really does is that it keeps copies of the items that has been accessed recently in RAM so that next time when the user needs the item, instead of going to the main memory it can go to RAM and collect it. I don’t want to go deeper in this, this is just a high level view, but feel free to go and read further on RAM.
Now let’s get some coding into picture. When we create a variable it gets stored in the memory. But which part of the memory? They are available in RAM. In python everything is of type object, At run-time Python divides the RAM into two parts
- Run-time STACK
- Private Heap
Private heap hold the objects and the run-time stack holds the reference to the objects. Stack and Heap are both data structures which aids to execute (in our case) Python programs. Stack is required for maintaining information about the state of the programs as it executes. In python we use something like Private Heaps( when sometimes the apps make frequent allocations from heap). The management of private heap is taken care by Python memory manager. Another point is that stack is a static memory allocation(allocation of memory at compile time) and Heaps are dynamic memory allocation(allocation of memory at run time). We will be talking in-depth with STACKS and HEAPS in later posts. Coming back to Python, lets consider the following
x holds the value 10, the memory location of x = 494134176. Now I say y=x, so the reference is copied to y now when i check the memory location of y it is same as x. How is that? This is because in python the object is not copied by value, its copied by reference. Now if I way:
Id changed now it points to a new memory location.
In the next post I will be discussing about Python memory manager, data structures for efficient utilization of memory resource. Until then Happy coding!