Skip to content

Comments

Fix memory leaks in crash and memleak endpoints#59

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-58
Draft

Fix memory leaks in crash and memleak endpoints#59
Copilot wants to merge 2 commits intomainfrom
copilot/fix-58

Conversation

Copy link

Copilot AI commented Jul 15, 2025

This PR addresses critical memory issues identified in the Azure SRE Agent memory analysis that were causing high heap usage from System.String allocations and potential application crashes.

Issues Fixed

1. Infinite Loop in crash Endpoint

Problem: The while condition while (true || bytesSize < 1_000_000) created an infinite loop due to the OR operator always evaluating to true, causing unlimited memory allocation.

Fix: Changed to while (bytesSize < 1_000_000) to respect the intended 1MB limit.

// Before (infinite loop)
while (true || bytesSize < 1_000_000)

// After (respects limit)
while (bytesSize < 1_000_000)

2. Memory Accumulation in memleak Endpoint

Problem: The static Processor instance accumulated Customer objects indefinitely across requests, with each Customer containing a GUID string, leading to excessive string allocations and memory retention.

Fix: Added cache clearing after each request to prevent indefinite accumulation while preserving the endpoint's intended functionality.

[HttpGet]
[Route("memleak/{kb}")]
public ActionResult<string> memleak(int kb)
{
    int it = (kb * 1000) / 100;
    for (int i = 0; i < it; i++)
    {
        p.ProcessTransaction(new Customer(Guid.NewGuid().ToString()));
    }

    // Clear cache after processing to prevent indefinite accumulation
    p.ClearCache();

    return "success:memleak";
}

Changes Made

  • Added Clear() and Count methods to CustomerCache class
  • Added ClearCache() and CacheCount methods to Processor class
  • Fixed infinite loop condition in crash endpoint
  • Added memory cleanup in memleak endpoint
  • Fixed allocation size comment for consistency (1MB → 10MB)

Impact

  • Memory Usage: Significantly reduced System.String allocations and prevented infinite memory accumulation
  • Application Stability: Eliminated risk of out-of-memory crashes from infinite loops
  • GC Pressure: Reduced by allowing proper garbage collection of Customer objects
  • Functionality: All existing endpoint behavior preserved

Testing

All endpoints tested and verified:

  • crash endpoint now terminates properly (was infinite loop)
  • memleak endpoint processes requests with memory cleanup
  • appinvoke endpoint continues working as expected
  • ✅ Clean build with no warnings/errors

Fixes #58.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: mrsharm <68247673+mrsharm@users.noreply.github.com>
Copilot AI changed the title [WIP] Memory Analysis: High Heap Usage by System.String in cpu-app (memleak and crash endpoints) Fix memory leaks in crash and memleak endpoints Jul 15, 2025
Copilot AI requested a review from mrsharm July 15, 2025 22:03
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.

Memory Analysis: High Heap Usage by System.String in cpu-app (memleak and crash endpoints)

2 participants