-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReferenceCounting.py
More file actions
executable file
·58 lines (42 loc) · 1.04 KB
/
ReferenceCounting.py
File metadata and controls
executable file
·58 lines (42 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 24 17:49:51 2021
@author: maherme
"""
#%%
import sys
a = [1, 2, 3]
print(id(a))
print(sys.getrefcount(a)) # It is two due to the call of getrefcount increases the reference
#%%
import ctypes
def ref_count(address: int):
return ctypes.c_long.from_address(address).value
print(ref_count(id(a)))
# In this case the result is 1 due to id(a) is evaluated and finishes before
# to call ref_count function
#%%
# Observe how the reference counting is changing depending of how much variables
# are pointing to the same memory address:
b = a
print(id(b))
print(ref_count(id(a)))
c = a
print(ref_count(id(a)))
c = 10
print(ref_count(id(a)))
b = None
print(id(b))
print(ref_count(id(a)))
#%%
# Notice what happens here, the action to do a = None is freeing the memory
# and this memory is being using for other purposes, for this reason the
# memory address is changing.
a_id = id(a)
a = None
print(ref_count(a_id))
print(ref_count(a_id))
print(ref_count(a_id))
print(ref_count(a_id))
#%%