Skip to content

Commit

Permalink
perf: detect memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
apaletta3 committed Jun 14, 2024
1 parent 33fabf4 commit 56dd61a
Show file tree
Hide file tree
Showing 4 changed files with 424 additions and 2 deletions.
53 changes: 53 additions & 0 deletions bindings/python/test/memory_leak_tracker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright © Loft Orbital Solutions Inc.

import psutil
import os
import time
import gc


# Function to monitor memory usage
def monitor_memory_usage():
process = psutil.Process(os.getpid())
mem_info = process.memory_info()
return mem_info.rss / (1024 * 1024) # Return memory usage in MB


# Decorator to track memory usage
def track_memory_usage(func):
def wrapper(*args, **kwargs):
# Memory usage before function execution
mem_before = monitor_memory_usage()
print(f"Memory usage before {func.__name__}: {mem_before:.2f} MB")

# Execute the function
result = func(*args, **kwargs)

# Trigger garbage collection
# gc.collect()

# Memory usage after function execution
mem_after = monitor_memory_usage()
print(f"Memory usage after {func.__name__}: {mem_after:.2f} MB")

# Memory leak
mem_diff = mem_after - mem_before
print(f"Memory change in {func.__name__}: {mem_diff:.2f} MB")

return result

return wrapper


# Example function to test memory usage
@track_memory_usage
def example_function():
# Simulate some memory usage
data = [x**2 for x in range(1000000)]

# Clear the data to release memory
# del data


if __name__ == "__main__":
result = example_function()
Loading

0 comments on commit 56dd61a

Please sign in to comment.