Created
August 4, 2025 21:36
-
-
Save pablogsal/62c76a3fd2242f1294b396ec522573d1 to your computer and use it in GitHub Desktop.
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 sys | |
| import threading | |
| import time | |
| from memray._test import MemoryAllocator | |
| def background_attacher(): | |
| time.sleep(1) | |
| import memray | |
| with memray.Tracker(destination=memray.FileDestination(path="output.bin", overwrite=True)): | |
| time.sleep(2) | |
| threading.Thread(target=background_attacher, daemon=True).start() | |
| stop_looping = threading.Event() | |
| def io_thread_body(): | |
| print("ready") | |
| for line in sys.stdin: | |
| stop_looping.set() | |
| def alloc_thread_body(): | |
| while not stop_looping.is_set(): | |
| allocator = MemoryAllocator() | |
| allocator.malloc(1024) | |
| allocator.free() | |
| def foo(): | |
| bar() | |
| def bar(): | |
| baz() | |
| def baz(): | |
| allocator = MemoryAllocator() | |
| allocator.valloc(50 * 1024 * 1024) | |
| allocator.free() | |
| # Debugger attaches wait for allocator or deallocator calls, so we can't just | |
| # block waiting for a signal. Allocate and free in a loop in the background. | |
| # Additionally, there must be a Python loop in the main thread, because that's | |
| # the thread `sys.remote_exec` attaches to and it needs the eval breaker. Spawn | |
| # another background thread to let us know when to stop looping. | |
| io_thread = threading.Thread(target=io_thread_body) | |
| io_thread.start() | |
| alloc_thread = threading.Thread(target=alloc_thread_body) | |
| alloc_thread.start() | |
| while not stop_looping.is_set(): | |
| time.sleep(0.1) | |
| alloc_thread.join() | |
| io_thread.join() | |
| foo() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment