math_lover
math_lover

Reputation: 956

How does memory work in Python?

My apologies in advance for this naive question.

Imagine a program in some language say Python which creates a list and continually adds elements to it. This program can't go on forever because these lists are stored somewhere in the memory of the computer, which is finite (12 GB RAM on my computer). At what point will Python will tell me that I've exceeded the memory limitations of my computer?

Next, say I terminate the program at some point. What happened to the list? Is it still stored in memory or is it deleted and the space freed up?

Upvotes: 1

Views: 278

Answers (1)

user3151902
user3151902

Reputation: 3176

Memory management is one of the tasks of the operating system (OS), e.g. Linux, Windows, macOS.

The Python program will keep adding elements to the list until the OS denies the request for more memory. This can happen before 12 GB have been reached, for example if an administrator has configured a per-process memory limit. All modern operating systems can also be configured to use parts of the disks for memory, so the program might also be able to get more than 12 GB.

When a program terminates, the OS frees up all memory that has been used by the program. However, this does not mean that the memory is cleared, it is merely marked as available. If you stored the value "hello-world" in your list, it could still be found in the RAM (or on the disk).

Upvotes: 1

Related Questions