Skip to content
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

Blazor WASM + looping JsonSerializer.Serialize() results in out-of memory #23451

Closed
JeremyKnaggs opened this issue Jun 28, 2020 · 2 comments
Closed

Comments

@JeremyKnaggs
Copy link

JeremyKnaggs commented Jun 28, 2020

Describe the bug

I was doing some stress testing and found memory doesn't seem to be released inside a loop running in Blazor WASM apps. If I run the below method, I can watch the browser's memory continue to increase until the app crashes.

To Reproduce

@code {

private void StressMemory()
{

    Dictionary<string, double[]> LotsOfData = new Dictionary<string, double[]>();

    int arraySize = 1000000;
    var ary = new double[arraySize];
    Random r = new Random();

    for (int i = 0; i < 1000; i++)
    {

        Console.WriteLine(i.ToString() + " Starting");
        string key = "key";
        if (!LotsOfData.ContainsKey(key)) LotsOfData.Add(key, ary);
        for (int j = 0; j < arraySize; j++)
        {
            LotsOfData[key][j] = r.NextDouble();
        }
        Console.WriteLine("Serializing: "+i.ToString());

        JsonSerializer.Serialize(LotsOfData);
        Console.WriteLine(i.ToString() + " Done");
    }

}

}

Exceptions (if any)

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Out of memory
System.OutOfMemoryException: Out of memory
at System.Buffers.TlsOverPerCoreLockedStacksArrayPool1[T].Rent (System.Int32 minimumLength) <0x1a8fcd0 + 0x0014c> in <filename unknown>:0 at System.Text.Json.PooledByteBufferWriter.CheckAndResizeBuffer (System.Int32 sizeHint) <0x1eafe40 + 0x00074> in <filename unknown>:0 at System.Text.Json.PooledByteBufferWriter.GetMemory (System.Int32 sizeHint) <0x1eafd68 + 0x00006> in <filename unknown>:0 at System.Text.Json.Utf8JsonWriter.Grow (System.Int32 requiredSize) <0x1ebc810 + 0x000b6> in <filename unknown>:0 at System.Text.Json.Utf8JsonWriter.WriteNumberValueMinimized (System.Double value) <0x29af080 + 0x0002e> in <filename unknown>:0 at System.Text.Json.Utf8JsonWriter.WriteNumberValue (System.Double value) <0x29aee60 + 0x0003e> in <filename unknown>:0 at System.Text.Json.Serialization.Converters.JsonConverterDouble.Write (System.Text.Json.Utf8JsonWriter writer, System.Double value, System.Text.Json.JsonSerializerOptions options) <0x29aeca0 + 0x0000c> in <filename unknown>:0 at System.Text.Json.JsonPropertyInfoNotNullable4[TClass,TDeclaredProperty,TRuntimeProperty,TConverter].OnWriteEnumerable (System.Text.Json.WriteStackFrame& current, System.Text.Json.Utf8JsonWriter writer) <0x29aeab8 + 0x00088> in :0
at System.Text.Json.JsonPropertyInfo.WriteEnumerable (System.Text.Json.WriteStack& state, System.Text.Json.Utf8JsonWriter writer) <0x1eb0e98 + 0x0001e> in :0
at System.Text.Json.JsonSerializer.HandleEnumerable (System.Text.Json.JsonClassInfo elementClassInfo, System.Text.Json.JsonSerializerOptions options, System.Text.Json.Utf8JsonWriter writer, System.Text.Json.WriteStack& state) <0x1ebb578 + 0x00158> in :0
at System.Text.Json.JsonSerializer.Write (System.Text.Json.Utf8JsonWriter writer, System.Int32 originalWriterDepth, System.Int32 flushThreshold, System.Text.Json.JsonSerializerOptions options, System.Text.Json.WriteStack& state) <0x1eab1f0 + 0x0007a> in :0
at System.Text.Json.JsonSerializer.WriteCore (System.Text.Json.Utf8JsonWriter writer, System.Object value, System.Type type, System.Text.Json.JsonSerializerOptions options) <0x1c887a8 + 0x0008a> in :0
at System.Text.Json.JsonSerializer.WriteCore (System.Text.Json.PooledByteBufferWriter output, System.Object value, System.Type type, System.Text.Json.JsonSerializerOptions options) <0x1c87ef0 + 0x00040> in :0
at System.Text.Json.JsonSerializer.WriteCoreString (System.Object value, System.Type type, System.Text.Json.JsonSerializerOptions options) <0x1c87af8 + 0x0003c> in :0
at System.Text.Json.JsonSerializer.Serialize[TValue] (TValue value, System.Text.Json.JsonSerializerOptions options) <0x299f310 + 0x0000c> in :0
at TestBlazorMemoryRelease.MemoryCheck.MemoryCheck.StressMemory () <0x21e6ac8 + 0x000e8> in :0
at TestBlazorMemoryRelease.Pages.Counter.StressMemory () <0x21e64b0 + 0x0000a> in :0
at Microsoft.AspNetCore.Components.EventCallbackWorkItem.InvokeAsync[T] (System.MulticastDelegate delegate, T arg) <0x20cf748 + 0x0005e> in :0
at Microsoft.AspNetCore.Components.EventCallbackWorkItem.InvokeAsync (System.Object arg) <0x20c95a0 + 0x0000a> in :0
at Microsoft.AspNetCore.Components.ComponentBase.Microsoft.AspNetCore.Components.IHandleEvent.HandleEventAsync (Microsoft.AspNetCore.Components.EventCallbackWorkItem callback, System.Object arg) <0x20c9508 + 0x0000a> in :0
at Microsoft.AspNetCore.Components.EventCallback.InvokeAsync (System.Object arg) <0x20c90d0 + 0x00040> in :0
at Microsoft.AspNetCore.Components.RenderTree.Renderer.DispatchEventAsync (System.UInt64 eventHandlerId, Microsoft.AspNetCore.Components.RenderTree.EventFieldInfo fieldInfo, System.EventArgs eventArgs) <0x20c86c0 + 0x000a8> in :0
f.printErr @ blazor.webassembly.js:1

Further technical details

  • ASP.NET Core version
  • Include the output of dotnet --info
@JeremyKnaggs JeremyKnaggs changed the title Blazor WASM + looping Json.Serialize results in out-of memory Blazor WASM + looping JsonSerializer.Serialize() results in out-of memory Jun 28, 2020
@JeremyKnaggs
Copy link
Author

I realized this is more related to mono: mono/mono#19665

@JeremyKnaggs
Copy link
Author

Also: mono/mono#20009

@ghost ghost locked as resolved and limited conversation to collaborators Jul 28, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant