> For the complete documentation index, see [llms.txt](https://ret2basic.gitbook.io/ctfnote/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ret2basic.gitbook.io/ctfnote/computer-science/python/memory-management.md).

# Memory Management

## CPython

{% hint style="info" %}
The default Python implementation, **CPython**, is actually written in the C programming language.
{% endhint %}

Python is an interpreted programming language. Your Python code actually gets compiled down to more computer-readable instructions called **bytecode**. These instructions get **interpreted** by a virtual machine when you run your code. Have you ever seen a `.pyc` file or a `__pycache__` folder? That’s the bytecode that gets interpreted by the virtual machine.

## Garbage Collection (GC)

{% hint style="info" %}
CPython utilizes **reference count** for GC. Once reference count drops to 0, the object is freed.
{% endhint %}

The reference count gets increased for a few different reasons. For example, the reference count will increase if you assign it to another variable:

```python
numbers = [1, 2, 3]
# Reference count = 1
more_numbers = numbers
# Reference count = 2
```

It will also increase if you pass the object as an argument:

```python
total = sum(numbers)
```

As a final example, the reference count will increase if you include the object in a list:

```python
matrix = [numbers, numbers, numbers]
```

Python allows you to inspect the current reference count of an object with the `sys` module. You can use `sys.getrefcount(numbers)`, but keep in mind that passing in the object to `getrefcount()` increases the reference count by `1`. In any case, if the object is still required to hang around in your code, its reference count is greater than `0`. Once it drops to `0`, the object has a specific deallocation function that is called which “frees” the memory so that other objects can use it.

## Pool

{% hint style="info" %}
CPython's memory management logic: "wholesale" a large piece of memory as a **pool**, then "retail" small pieces when needed.
{% endhint %}

![Pool](/files/AwZvWZXmEHVkM7sR6fGo)

A `usedpools` list tracks all the pools that have some space available for data for each size class. When a given block size is requested, the algorithm checks this `usedpools` list for the list of pools for that block size. Pools themselves must be in one of 3 states:

* `used`
  * has available blocks for data to be stored
* `full`
  * containing blocks that are allocated and nonempty
* `empty`
  * no data stored and can be assigned any size class for blocks when needed

A `freepools` list keeps track of all the pools in the `empty` state. But when do empty pools get used? Assume your code needs an 8-byte chunk of memory. If there are no pools in `usedpools` of the 8-byte size class, a fresh `empty` pool is initialized to store 8-byte blocks. This new pool then gets added to the `usedpools` list so it can be used for future requests. Say a `full` pool frees some of its blocks because the memory is no longer needed. That pool would get added back to the `usedpools` list for its size class.

## Reference

{% embed url="<https://realpython.com/python-memory-management>" %}
Memory Management in Python - Real Python
{% endembed %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ret2basic.gitbook.io/ctfnote/computer-science/python/memory-management.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
