10110110
).
Computation means processing data using the CPU, such as performing mathematical operations or logic-based tasks.
Let’s analyze a basic function and how memory allocation works during computation.
Example: Memory Usage in Computation
import sys
def add_numbers(a, b):
result = a + b
return result
# Define two numbers
num1 = 10
num2 = 20
# Function call
sum_result = add_numbers(num1, num2)
# Checking memory usage
print("Memory used by num1:", sys.getsizeof(num1), "bytes")
print("Memory used by num2:", sys.getsizeof(num2), "bytes")
print("Memory used by sum_result:", sys.getsizeof(sum_result), "bytes")
OUTPUT:
Memory used by num1: 28 bytes
Memory used by num2: 28 bytes
Memory used by sum_result: 28 bytes
num1 = 10
→ Stored in memory.num2 = 20
→ Stored in memory.sys.getsizeof(num1)
shows how much memory Python allocates for storing this integer.add_numbers(num1, num2)
)
10
and 20
from memory.10 + 20 = 30
) inside the CPU.30
) back in memory.30
to sum_result
.sys.getsizeof(10)
→ 28 bytes (in Python 3.x).sys.getsizeof(30)
shows how much memory is allocated for the result.Data Type | 32-bit Python | 64-bit Python |
---|---|---|
Integer (int ) |
24 bytes | 28 bytes |
Float (float ) |
16 bytes | 24 bytes |
List (Empty) | 36 bytes | 48 bytes |
64-bit Python allocates more memory per object than 32-bit Python.
The difference comes from pointers (references to memory locations).
x = 5
y = 10
z = x * y + 2
INTERPRETATION:
1. `x * y` → CPU fetches `5` and `10`, performs multiplication, **stores `50` in memory**.
2. `50 + 2` → CPU adds `2`, stores `52` in memory.
3. `z = 52` → Final result stored.
Computation Process Summary:
Step | Action |
---|---|
1 | Retrieve x from RAM. |
2 | Retrieve y from RAM. |
3 | Multiply x * y in CPU registers. |
4 | Add 2 to result. |
5 | Store z in memory. |
6 | Display output. |
When you run a Python script:
Memory Type | Stores Data When? | Data Persistence |
---|---|---|
RAM (Random Access Memory) | While program is running | Data is lost when program stops |
HDD/SSD (Hard Drive, Solid State Drive) | Manually saved (e.g., files, databases) | Data is permanently stored until deleted |
num1 = 10
in Python, it gets allocated in RAM.If you want to keep the values even after the script closes, you need to save them to a file or database.
Example: Save data to a file:
# Write to a file
with open("data.txt", "w") as file:
file.write("num1 = 10\nnum2 = 20\n")
# Later, read from the file
with open("data.txt", "r") as file:
print(file.read()) # This retrieves the saved data
Now, even if you close Python or VS Code, the values are stored in data.txt
.
Example 2: Store Data in a Database
import sqlite3
conn = sqlite3.connect("numbers.db") # Create or open a database
cursor = conn.cursor()
# Create a table if it doesn’t exist
cursor.execute("CREATE TABLE IF NOT EXISTS numbers (num1 INTEGER, num2 INTEGER)")
# Insert data
cursor.execute("INSERT INTO numbers VALUES (10, 20)")
conn.commit()
# Retrieve data
cursor.execute("SELECT * FROM numbers")
print(cursor.fetchall())
conn.close()
Now, the numbers are permanently stored in a database.
SUMMARY
NOTE: This article has been taken from GPT.
Properties of an Object in Python:
EXAMPLE
import sys
listA = ['Islamabad','Pakistan','Karachi', 100, 200]
print(f"Memory ID: {id(listA)} \n Reference Count: {sys.getrefcount(listA)}\nSize: {sys.getsizeof(listA)}")
OUTPUT:
Memory ID: 2583558530176
Reference Count: 2
Size: 104
Example: Below is an example to show the reference point of an IMMUTABLE OBJECT holding values between the range of -5 and 256.
import sys
numOne = 40
print(f"\nReference count of numOne(40) -> {sys.getrefcount(numOne)}\nMemory ID-> {id(numOne)}")
numTwo = 70
print(f"Reference count of numOne(70) -> {sys.getrefcount(numTwo)}\nMemory ID-> {id(numTwo)}")
numThree = numTwo
print(f"Reference count of numThree=numTwo -> {sys.getrefcount(numThree)}\nMemory ID-> {id(numThree)}")
OUTPUT:
Reference count of numOne(40) -> 4294967295
Memory ID-> 140713838512264
Reference count of numOne(70) -> 4294967295
Memory ID-> 140713838513224
Reference count of numThree=numTwo -> 4294967295
Memory ID-> 140713838513224
INTERPRETATION:
1. numOne and numTwo reference counts are higher number because their object values are between the range of -5 to 256.
2. numOne and numTwo, both hold different Memory ID's, because they are both new objects created since their values are different.
3. numThree reference count also remains higher and the same as other objects because it too falls between the range of -5 and 256, since numThree is referenced to numTwo.
4. However, numThree holds the same Memory ID as numTwo because python has interned the value of numTwo which is 70, since they are both immutable objects and hold the same integer value, therefore python maintains a SINGLE copy of the integer.
cA = 1000
print(sys.getrefcount(cA))
cB = 1000
print(f"cA Memory ID: {id(cA)}\ncB Memory ID: {id(cB)}\nREF COUNT cA: {sys.getrefcount(cA)}\nREF COUNT cB: {sys.getrefcount(cB)}")
cXA = 20
cXB = 20
print(f"cXA Memory ID: {id(cXA)}\ncXB Memory ID: {id(cXB)}\nREF COUNT cXA: {sys.getrefcount(cXA)}\nREF COUNT cXB: {sys.getrefcount(cXB)}")
OUTPUT:
4
cA Memory ID: 2928490041808
cB Memory ID: 2928490041808
REF COUNT cA: 5
REF COUNT cB: 5
cXA Memory ID: 140713768912392
cXB Memory ID: 140713768912392
REF COUNT cXA: 4294967295
REF COUNT cXB: 4294967295
bA = "Hello"
bB = "Hello"
bC = "Hello World"
print(f"\nMemory ID bA: {id(bA)}\nMemory ID bB: {id(bB)}\nMemory ID bC: {id(bC)}\n REF COUNT bA: {sys.getrefcount(bA)}\n REF COUNT bB: {sys.getrefcount(bB)}\n REF COUNT bC: {sys.getrefcount(bC)}")
OUTPUT:
Memory ID bA: 2928490384192
Memory ID bB: 2928490384192
Memory ID bC: 2928490366448
REF COUNT bA: 5
REF COUNT bB: 5
REF COUNT bC: 4
arrA = ['Karachi','Islamabad','Poland','Netherlands']
print(f"\nMemory ID arrA-> {id(arrA)}\nReference Count arrA -> {sys.getrefcount(arrA)}")
arrB = arrA
print(f"Memory of ID arrB=arrA -> {id(arrB)}\nMemoryID arrB -> {sys.getrefcount(arrB)}")
arrC = ['Ireland','Canada', 'Switzerland']
print(f"\nMemory ID arrC-> {id(arrC)}\nReference Count arrC -> {sys.getrefcount(arrC)}")
arrD = ['Karachi','Islamabad','Poland','Netherlands']
print(f"\narrD is a new list by assigning the same values of arrA \nMemory ID arrD -> {id(arrD)}\nReference Count arrD-> {sys.getrefcount(arrD)}")
OUTPUT:
Memory ID arrA-> 1584623721856
Reference Count arrA -> 2
Memory of ID arrB=arrA -> 1584623721856
MemoryID arrB -> 3
Memory ID arrC-> 1584623722112
Reference Count arrC -> 2
2 1584623721344
3 1584623722112 1584623722112
arrD is a new list by assigning the same values of arrA
Memory ID arrD -> 1855307193408
Reference Count arrD-> 2
Example
import sys
boxA = 'Pakistan'
print(f"Memory ID: {id(boxA)}")
boxA = 'America'
print(f"Memory ID (After reassigning new value): {id(boxA)}")
OUTPUT:
Memory ID: 1708450300272
Memory ID (After reassigning new value): 1708450316528
sys.getsizeof([])
returns ~88 bytes (or 56) because an empty list has internal structure even before elements are added.Example
import sys
listC: list = []
print(f"\nMemory Size of an empty list: {sys.getsizeof(listC)}")
listC = [100, 200, 400, 300, 900]
print(f"\nMemory size of a list with values: {sys.getsizeof(listC)}")
OUTPUT:
Memory Size of an empty list: 56
Memory size of a list with values: 104
x = 10
, Python creates the object 10
in memory and assigns x
to it.y = x
) refers to it, both share the same memory address during that execution.x = 10
), their memory addresses (IDs) may change because Python dynamically manages memory allocation.10, 20
) may be cached, but larger objects get new memory.id()
FunctionSUMMARY:
Feature | id() | sys.getrefcount() |
---|---|---|
Purpose | Returns the memory address (identity) of an object. | Returns the number of references to an object. |
Type of Value | Integer (memory address). | Integer (reference count). |
Changes Over Time? | No, remains the same unless object is deleted. | Yes, increases/decreases as references are added/removed. |
Garbage Collection | No relation to garbage collection. | When count reaches zero, object is garbage collected. |
Usage | Checking if two variables point to the same object. | Checking how many variables reference an object. |
Data Type | Object Type | Memory ID | REF Count Base Value | Reference Count |
---|---|---|---|---|
String | Immutable | Unique | Referenced variable will share the same ID | 2 | Increases only if another variable is pointing to the Base string object. If the value of string is updated, then the ref count starts from the base again and Memory ID becomes unique again. |
Integer (between -5 to 256) | Immutable | Unique | Referenced variable will share the same ID | Cached (Example: 4294967295) | Reference count remains the same as shown in the number of cached value. |
Integer (256+) | Immutable | Unique | Referenced variable will share the same ID | 4 | Reference count begins from 4 unless if any other variable is pointing to it. Depending on the number of references, the count increases or decreases. |
List | Mutable | Unique | 2 | Increases only if another variable is Referenced to it, otherwise remains the base value. |