Reduce memory load of redistributable download #342
Merged
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.
The use of
HttpClient.GetAsync
to download the Python redistributable inRedistributableLocator
causesthe entire response content to be buffered to memory before being written to disk. Just stepping over the following line in the debugger:
CSnakes/src/CSnakes.Runtime/Locators/RedistributableLocator.cs
Line 166 in de05da4
shows the memory spike (over 2,600 objects and tens of MBs allocated):
If one examines the heap for byte arrays, we find a 41.6MB allocation done by
HttpContent.LimitMemoryStream
The exact allocation size of 416,204,438 corresponds to the HTTP content length:
The PR fixes the problem by using
HttpClient.GetStreamAsync
, which checks for a success response code before returning a stream over the content (instead of entirely buffering it). The image below shows the memory usage drops dramatically, allocating just 284.4 KB after stepping over theHttpClient.GetStreamAsync
call (ID 2) and something similar until the end of theDownloadFileToTempDirectoryAsync
method (ID 3):