mem: free intermediate arrays during YoloX inference#496
Merged
Conversation
cragwolfe
approved these changes
Apr 1, 2026
Delete origin_img, img/ort_inputs, and output at the points where they become dead instead of letting them linger until function return. The biggest win is origin_img — the full-resolution numpy copy of the input PIL image — which stays alive through ONNX inference in the current code. Savings are proportional to image size.
Call image.close() to release the PIL pixel buffer (~22 MiB for a typical page) before ONNX inference. PIL metadata (.width, .height, .format, .size) remains accessible after close().
4c38813 to
df0bab6
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Free
origin_img,img/ort_inputs,output, and the PIL pixel buffer at the points where they become dead inimage_processing(), instead of letting them linger until function return.The two main wins:
origin_img— the full-resolution numpy copy of the input PIL image stays alive through the entire ONNXsession.run()call.del origin_imgfrees it before inference.np.array(image)copies the pixel data, the PIL buffer is no longer needed.image.close()frees it immediately while preserving PIL metadata (.width,.height,.format,.size).Savings are proportional to image size: larger pages (higher DPI renders) carry bigger unused buffers through inference.
Benchmark
Azure Standard_D8s_v5 — 8 vCPU Intel Xeon Platinum 8473C, 32 GiB RAM, Python 3.12.12
Simulated ONNX session (35 MiB workspace), 1700x2200 letter-size image at 200 DPI.
Memory (peak per test)
b851f094d133(base)333c6d728837(head)Peak memory drops 25 MiB (-35%) by freeing dead buffers before ONNX inference. Timing is neutral (within noise).
Timing — image_processing (1700x2200 image, fake ONNX session)
b851f094d133(base)333c6d728837(head)Timing is within noise — the
delstatements andimage.close()add negligible overhead vs. the ONNX inference wall time.Generated by codeflash optimization agent
Reproduce the benchmark locally
Benchmark test source
Test plan
codeflash compareconfirms -35% peak memory (72.0 MiB -> 47.0 MiB)