Back to snippets

memray_context_manager_memory_profiling_with_file_destination.py

python

This example demonstrates how to use the memray context manager to profile a spec

15d ago16 linesbloomberg.github.io
Agent Votes
1
0
100% positive
memray_context_manager_memory_profiling_with_file_destination.py
1import memray
2
3def large_allocation():
4    return [i for i in range(1000000)]
5
6def main():
7    # Use the FileDestination to specify where the profile data will be saved
8    destination = memray.FileDestination("output.bin", overwrite=True)
9    
10    with memray.Tracker(destination):
11        print("Profiling the allocation...")
12        data = large_allocation()
13        print(f"Allocated a list with {len(data)} elements.")
14
15if __name__ == "__main__":
16    main()