Created
January 25, 2025 10:33
-
-
Save nicolargo/4a8e88b91c6ee814d8e11246587f4311 to your computer and use it in GitHub Desktop.
Create a simple memory leak in Python (only for test purpose)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
# Leak 1000 bytes every 1 second | |
leak_size_byte = 1000 | |
leak_freq_sec = 1 | |
class LeakyObject: | |
def __init__(self, id): | |
self.id = id | |
self.data = "X" * leak_size_byte | |
def create_memory_leak(): | |
leaky_list = [] # This list will keep growing | |
counter = 0 | |
try: | |
while True: | |
leaky_list.append(LeakyObject(counter)) | |
counter += 1 | |
time.sleep(leak_freq_sec) | |
except MemoryError: | |
print("MemoryError: The program ran out of memory!") | |
if __name__ == "__main__": | |
print(f"Generate a memory leak {leak_size_byte] bytes every {leak_freq_sec} seconds") | |
create_memory_leak() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment