Skip to content Skip to sidebar Skip to footer

How Is Memory Allocated For Variables In Python?

As an example, I have the following Python code: >>> x = 9.89 Now I know the type will be determined as float dynamically during runtime, but I am unsure how memory is al

Solution 1:

This is all going to be CPython implementation details. Other implementations may behaved differently.

Memory allocation for variables is completely independent of what type of object they refer to. Whether you assign x = [], x = 5, or x = SomeCrazyHugeThing(), the value doesn't affect the memory allocated for x. Local variables are allocated as a PyObject * in a Python stack frame's f_localsplus when the function call is entered; other variables are "allocated" as dict entries in the corresponding namespace's dict at time of first assignment. (Crazy metaclasses and exec shenanigans can change this in weird ways, but we're not going to go into that.)

That's for variables. Objects, like the float instance the 9.89 expression evaluates to in your example code, are allocated differently. Each type is responsible for allocating memory when an instance of the type is created and reallocating or deallocating it as needed when the object grows, shrinks, or is destroyed. The manner in which this is done depends on the type.

Note that the time at which an object is created can be surprising. For example, in x = 9.89, Python creates (and allocates memory for) a float object when the bytecode for that statement is compiled. During bytecode execution, Python just retrieves the existing float; no new float is allocated.

Solution 2:

Python Memory Management

Python does a lot of allocations and deallocations. All objects, including "simple" types like integers and floats, are stored on the heap. Calling malloc and free for each variable would be very slow. Hence, the Python interpreter uses a variety of optimized memory allocation schemes. The most important one is a malloc implementation called pymalloc, designed specifically to handle large numbers of small allocations. Any object that is smaller than 256 bytes uses this allocator, while anything larger uses the system's malloc.

Why doesn't Python release the memory when I delete a large object?

Memory allocation works at several levels in Python. There’s the system’s own allocator, which is what shows up when you check the memory use using the Windows Task Manager or ps. Then there’s the C runtime’s memory allocator (malloc), which gets memory from the system allocator, and hands it out in smaller chunks to the application. Finally, there’s Python’s own object allocator, which is used for objects up to 256 bytes. This allocator grabs large chunks of memory from the C allocator, and chops them up in smaller pieces using an algorithm carefully tuned for Python.

and specifically for floats:

floats also use an immortal & unbounded free list.

So no, it would only be allocated if there are no more free float spots in pythons free list, which depends on previous float usage in your program.

Ultimately, python is doing the memory management for you, so even a solid answer to your question won't give you much insight.

Additional discussion can be found at Python: garbage collection fails?

Post a Comment for "How Is Memory Allocated For Variables In Python?"