Skip to content

Conversation

gilemos
Copy link
Contributor

@gilemos gilemos commented Sep 25, 2025

Why make this change?

We have recently seen an out of memory error coming from the line of code that serializes the column data.

What is this change?

Here, we are using a memory stream in the serialization process to avoid any out of memory errors.

There is a small performance overhead in using a memory stream - and I would like your opinion on whether that could be significant or not. The overhead could be negligible compared to allocating a large string, however, if you think performance would be an issue, we could change the code to only use the memory stream if the data is larger than a threshold

How was this tested?

  • Integration Tests
  • Unit Tests

Sample Request(s)

  • Simple query request
  • Stored procedure call
  • Embedded stored procedure calls

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR addresses out of memory issues during column data serialization by replacing a direct JsonSerializer.Serialize/Deserialize pattern with a memory stream-based approach. The change aims to improve memory management during JSON processing of database result sets.

  • Replaces direct string-based JSON serialization with memory stream approach
  • Adds proper stream positioning and JsonDocument parsing
  • Introduces resource management with using statements for memory cleanup

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +593 to +600
using MemoryStream ms = new();
using (Utf8JsonWriter writer = new(ms))
{
JsonSerializer.Serialize(writer, dbResultSetRow.Columns);
}

ms.Position = 0;
using JsonDocument doc = JsonDocument.Parse(ms);
Copy link
Preview

Copilot AI Sep 25, 2025

Choose a reason for hiding this comment

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

This approach may not resolve the original OOM issue and could introduce performance overhead. The MemoryStream still allocates memory internally, and you're now creating additional objects (MemoryStream, Utf8JsonWriter, JsonDocument). Consider using JsonSerializer.SerializeToUtf8Bytes() and JsonSerializer.Deserialize() with ReadOnlySpan for better memory efficiency.

Suggested change
using MemoryStream ms = new();
using (Utf8JsonWriter writer = new(ms))
{
JsonSerializer.Serialize(writer, dbResultSetRow.Columns);
}
ms.Position = 0;
using JsonDocument doc = JsonDocument.Parse(ms);
byte[] jsonBytes = JsonSerializer.SerializeToUtf8Bytes(dbResultSetRow.Columns);
using JsonDocument doc = JsonDocument.Parse(jsonBytes);

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant