Skip to content

psp-10487 address major memory useage outliers. #4880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2025

Conversation

devinleighsmith
Copy link
Collaborator

No description provided.

@devinleighsmith devinleighsmith self-assigned this Jul 3, 2025
@@ -7,6 +7,8 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<ProjectGuid>{16BC0468-78F6-4C91-87DA-7403C919E646}</ProjectGuid>
<TargetFramework>net8.0</TargetFramework>
<ServerGarbageCollection>true</ServerGarbageCollection>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default GC setting is actually "workstation" which doesn't seem appropriate for our case. See here:
https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/workstation-server-gc

@@ -257,8 +257,9 @@ public async Task<ExternalResponse<DocumentDetailModel>> TryUploadDocumentAsync(
string authenticationToken = await _authRepository.GetTokenAsync();

byte[] fileData;
using var byteReader = new BinaryReader(file.OpenReadStream());
fileData = byteReader.ReadBytes((int)file.OpenReadStream().Length);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This opens an extra (untracked) readstream just to get the stream length.

@@ -48,13 +48,13 @@ public async Task Invoke(HttpContext context)
{
context.Request.EnableBuffering();
await using var requestStream = _recyclableMemoryStreamManager.GetStream();
await context.Request.Body.CopyToAsync(requestStream);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The copy was happening before the max length check - as a result the entire copy operation was being persisted to memory - despite the using call (possibly due to the recycleable memory stream manager). This increased the size of the heap by the largest document uploaded.

string body = null;
if (reader.BaseStream.Length < maxStreamLength)
if (context.Response.ContentLength < maxStreamLength)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar story here - larger responses end up persisting in memory after the request is handled in these logging processes.

Copy link

sonarqubecloud bot commented Jul 3, 2025

Copy link
Contributor

github-actions bot commented Jul 3, 2025

See CodeCov Report Here: https://app.codecov.io/github/bcgov/psp/pull/4880

@asanchezr asanchezr merged commit c9f0624 into bcgov:dev Jul 3, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants