"""GC / heap snapshot helpers for debug logging.""" import gc def snapshot(): """Return a dict of memory stats after ``gc.collect()``.""" gc.collect() out = { "free": gc.mem_free(), "alloc": gc.mem_alloc(), } try: import esp32 blocks = esp32.idf_heap_info(esp32.HEAP_DATA) if blocks: block = blocks[0] if isinstance(block, dict): if "total_free_bytes" in block: out["idf_free"] = block["total_free_bytes"] largest = block.get("largest_free_block") if largest is None: largest = block.get("largest_free_block_in_bytes") if largest is not None: out["idf_largest"] = largest except Exception: pass return out def print_mem(label): """Print one timestamped memory line (via ``print_timestamp`` when installed).""" print("mem %s:" % label, snapshot())