首页 新闻 搜索 专区 学院

ASP.NET Core 3.0 程序出现 OutOfMemoryException 异常

0
悬赏园豆:30 [待解决问题]

今天晚上一个跑在容器中的 ASP.NET Core 3.0 Preview 5 程序在启动时竟然出现了 OutOfMemoryException 异常,异常堆栈信息如下,很是奇怪

System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
   at System.Text.StringBuilder.ExpandByABlock(Int32 minBlockCharCount)
   at System.Text.StringBuilder.Append(Char* value, Int32 valueCount)
   at System.Text.StringBuilder.AppendHelper(String value)
   at System.Text.StringBuilder.Append(String value)
   at Microsoft.Extensions.Internal.TypeNameHelper.ProcessType(StringBuilder builder, Type type, DisplayNameOptions& options)
   at Microsoft.Extensions.Internal.TypeNameHelper.GetTypeDisplayName(Type type, Boolean fullName, Boolean includeGenericParameterNames, Boolean includeGenericParameters, Char nestedTypeDelimiter)
   at Microsoft.Extensions.Logging.Logger`1..ctor(ILoggerFactory factory)
   at Microsoft.Extensions.Logging.LoggerFactoryExtensions.CreateLogger[T](ILoggerFactory factory)
   at ResolveService(ILEmitResolverBuilderRuntimeContext , ServiceProviderEngineScope )
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
   at lambda_method(Closure , IServiceProvider , Object[] )
   at Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.<>c__DisplayClass4_0.<CreateActivator>b__0(ControllerContext controllerContext)
   at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass5_0.<CreateControllerFactory>g__CreateController|0(ControllerContext controllerContext)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
问题补充:

异常是在 corefx 的 StringBuilder.cs 的 ExpandByABlock 方法中抛出的

int newBlockLength = Math.Max(minBlockCharCount, Math.Min(Length, MaxChunkSize));

// Check for integer overflow (logical buffer size > int.MaxValue)
if (m_ChunkOffset + m_ChunkLength + newBlockLength < newBlockLength)
    throw new OutOfMemoryException();

调用 StringBuilder 是在 TypeNameHelper.cs 的 ProcessType 方法中

dudu的主页 dudu | 高人七级 | 园豆:38801
提问于:2019-05-23 20:46

代码里是否有大文件的加载?

BUTTERAPPLE 1年前

@BUTTERAPPLE: 就是闪存遇到的,之前跑在 .NET Core 2.2 上从没遇到过这个问题

dudu 1年前
< >
分享
所有回答(1)
0
丫的 | 园豆:1575 (小虾三级) | 2019-05-24 11:26

logical buffer size > int.MaxValue
这里说明你要处理的string最大长度大于int32的最大长度
可以尝试把你要处理的string分成多段用 StringBuilder处理

支持(0) 反对(0) 丫的 | 园豆:1575 (小虾三级) | 2019-05-24 15:54
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册