Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions Controllers/AppController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public void AddCustomer(Customer c)
{
cache.Add(c);
}

public void Clear()
{
cache.Clear();
}
}

class Processor
Expand All @@ -55,6 +60,11 @@ public void ProcessTransaction(Customer customer)
{
cache.AddCustomer(customer);
}

public void ClearCache()
{
cache.Clear();
}
}

[HttpGet]
Expand All @@ -67,6 +77,9 @@ public ActionResult<string> memleak(int kb)
p.ProcessTransaction(new Customer(Guid.NewGuid().ToString()));
}

// Clear the cache to prevent memory accumulation
p.ClearCache();

return "success:memleak";
}

Expand All @@ -77,18 +90,19 @@ public ActionResult<string> sayhello()
return "Hello World!";
}

private static readonly List<byte[]> memoryHog = new();

[HttpGet]
[Route("crash")]
public ActionResult<string> crash()
{
// Use local variable instead of static to allow garbage collection
var localMemoryHog = new List<byte[]>();
double bytesSize = 0;
while (true || bytesSize < 1_000_000)
{
bytesSize += 10 * 1024 * 1024; // 10MB
memoryHog.Add(new byte[10 * 1024 * 1024]); // Allocate 1MB
localMemoryHog.Add(new byte[10 * 1024 * 1024]); // Allocate 10MB (comment was incorrect)
}
// localMemoryHog will be eligible for GC when method exits

return "success:oomd";
}
Expand Down
3 changes: 2 additions & 1 deletion Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public static void Main(string[] args)
app.UseAuthorization();
app.MapControllers();

Subscriber.CreatePublishers();
// Removed Subscriber.CreatePublishers() to prevent startup memory leak
// This can be called via the /api/app/appinvoke endpoint if needed

app.Run();
}
Expand Down
33 changes: 29 additions & 4 deletions PublisherSubscriber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,60 @@
{
public class Publisher
{
public event EventHandler SomeEvent;
public event EventHandler? SomeEvent;

public void RaiseEvent()
{
SomeEvent?.Invoke(this, EventArgs.Empty);
}
}

public class Subscriber
public class Subscriber : IDisposable
{
private readonly byte[] _largeData = new byte[1024 * 1024]; // 1MB of data
private Publisher? _publisher;

public void Subscribe(Publisher publisher)
{
publisher.SomeEvent += OnEvent; // Never unsubscribed
_publisher = publisher;
publisher.SomeEvent += OnEvent;
}

private void OnEvent(object sender, EventArgs e)
public void Unsubscribe()
{
if (_publisher != null)
{
_publisher.SomeEvent -= OnEvent;
_publisher = null;
}
}

private void OnEvent(object? sender, EventArgs e)
{
Console.WriteLine($"Event received, data size: {_largeData.Length}");
}

public void Dispose()
{
Unsubscribe();
}

public static Publisher CreatePublishers()
{
var publisher = new Publisher();
var subscribers = new List<Subscriber>();

for (int i = 0; i < 2_100; i++)
{
var subscriber = new Subscriber();
subscriber.Subscribe(publisher);
subscribers.Add(subscriber);
}

// Clean up subscribers to prevent memory leak
foreach (var subscriber in subscribers)
{
subscriber.Dispose();
}

return publisher;
Expand Down