-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
424 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
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() |
Oops, something went wrong.