【问题标题】:System.OutOfMemoryException asp net MVCSystem.OutOfMemoryException asp net MVC
【发布时间】:2012-03-28 14:00:57
【问题描述】:

当代码来到这部分时,我得到了异常:

[GridAction]
        public JsonResult _ForRisk()
        {
            var user = current.CurrentUser;            
            var applications = from c in appRepository.GetAll().PendingForRisk(user)
                                   let client = c.Clients.FirstOrDefault(t => t.MainOwner == (int)RelationType.Borrower)                                   
                                   let timing = c.Timings.Where(t => t.ApplicationID == c.ID && t.Status == (int)AppStatus.Commited).OrderByDescending(t => t.Date).Take(1).FirstOrDefault()
                                   select new ApplicationViewModelScreen
                                   {
                                       ApplicationID = c.ID,
                                       Branch = c.Branch.Name,
                                       CompanyName = client != null ? client.CompanyName : "",
                                       CompanyRegisterNr = client != null ? client.CompanyRegistrationNo : "",
                                       DecisionStatus = c.ApplicationStatus,
                                       CreateDate = c.RegistrationDate,
                                       Submissiondate = timing != null ? timing.Date : new DateTime?(),
                                       Inputer = c.User.Name + " " + c.User.Surname,
                                    FincAssignedOfficer = c.AssignedOfficerForFinanc != null ? c.AssignedOfficerForFinanc.Name + " " + c.AssignedOfficerForFinanc.Surname : "",
                                    CRMAssignedOfficer = c.AssignedOfficerForCRM != null ?  c.AssignedOfficerForCRM.Name + " " + c.AssignedOfficerForCRM.Surname : "",
                                    CRMIndustry = client.CRMIndustyCode.Description

                                   };
            var data = applications.ToList();

                return Json(new GridModel<ApplicationViewModelScreen>
                {
                    Data = data,
                    Total = data.Count()
                }, JsonRequestBehavior.AllowGet);
        }

(我使用 StructureMap 作为 DI 并注入控制器)

public ApplicationController(
            IApplicationRepository _appRepository,
            IService<Application> _applicationService, 
            ITargetRepository _targetRepository, 
            IIDGenerator _idGen,
            ICurrent _current,
            IProductRepository _productRepository,
            IUserRepository _userRepository,
            IDecisionReasonRepository _decisionReason,
            IInformation _information,
            IDecisionExceptionRepository _decisionExceptionRepository,
            ITimingRepository _timingRepository,
            ITimingDecisionRepository _timingDecisionRepository)
        {
            current = _current;
            idGen = _idGen;
            appRepository = _appRepository;
            applicationService = _applicationService;
            targetRepository = _targetRepository;
            productRepository = _productRepository;
            userRepository = _userRepository;
            decisionReason = _decisionReason;
            information = _information;
            decisionExceptionRepository = _decisionExceptionRepository;
            timingRepository = _timingRepository;
            timingDecisionRepository = _timingDecisionRepository;
        }

例外

System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
   at System.Collections.BitArray.set_Length(Int32 value)
   at System.Data.Query.InternalTrees.VarVec.Align(VarVec other)
   at System.Data.Query.InternalTrees.NodeInfoVisitor.Visit(FilterOp op, Node n)
   at System.Data.Query.InternalTrees.FilterOp.Accept[TResultType](BasicOpVisitorOfT`1 v, Node n)
   at System.Data.Query.InternalTrees.BasicOpVisitorOfT`1.VisitNode(Node n)
   at System.Data.Query.InternalTrees.NodeInfoVisitor.RecomputeNodeInfo(Node n)
   at System.Data.Query.InternalTrees.Node.InitializeNodeInfo(Command command)
   at System.Data.Query.InternalTrees.Node.GetExtendedNodeInfo(Command command)
   at System.Data.Query.InternalTrees.NodeInfoVisitor.Visit(ProjectOp op, Node n)
   at System.Data.Query.InternalTrees.ProjectOp.Accept[TResultType](BasicOpVisitorOfT`1 v, Node n)
   at System.Data.Query.InternalTrees.BasicOpVisitorOfT`1.VisitNode(Node n)
   at System.Data.Query.InternalTrees.NodeInfoVisitor.RecomputeNodeInfo(Node n)
   at System.Data.Query.InternalTrees.Node.InitializeNodeInfo(Command command)
   at System.Data.Query.PlanCompiler.TransformationRulesContext.PreProcessSubTree(Node subTree)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.InternalTrees.RuleProcessor.ApplyRulesToSubtree(RuleProcessingContext context, ReadOnlyCollection`1 rules, Node subTreeRoot, Node parent, Int32 childIndexInParent)
   at System.Data.Query.PlanCompiler.TransformationRules.Process(PlanCompiler compilerState, ReadOnlyCollection`1 rulesTable, Boolean& projectionPruningRequired)
   at System.Data.Query.PlanCompiler.TransformationRules.Process(PlanCompiler compilerState, TransformationRulesGroup rulesGroup)
   at System.Data.Query.PlanCompiler.PlanCompiler.Compile(List`1& providerCommands, ColumnMap& resultColumnMap, Int32& columnCount, Set`1& entitySets)
   at System.Data.EntityClient.EntityCommandDefinition..ctor(DbProviderFactory storeProviderFactory, DbCommandTree commandTree)
   at System.Data.EntityClient.EntityProviderServices.CreateCommandDefinition(DbProviderFactory storeProviderFactory, DbCommandTree commandTree)
   at System.Data.EntityClient.EntityProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree)
   at System.Data.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree)
   at System.Data.Objects.Internal.ObjectQueryExecutionPlan.Prepare(ObjectContext context, DbQueryCommandTree tree, Type elementType, MergeOption mergeOption, Span span, ReadOnlyCollection`1 compiledQueryParameters)
   at System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)
   at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Corporate.Presentation.MVC.Controllers.ApplicationController._ForRisk() in K:\TempProjects\CorporateAPS\Corporate.Presentation.MVC\Controllers\ApplicationController.cs:line 220
   at lambda_method(Closure , ControllerBase , Object[] )
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
   at System.Web.Mvc.Controller.ExecuteCore()
   at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
   at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)
   at System.Web.Mvc.MvcHandler.<>c__DisplayClass6.<>c__DisplayClassb.<BeginProcessRequest>b__5()
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass1.<MakeVoidDelegate>b__0()
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   at System.Web.Mvc.MvcHandler.<>c__DisplayClasse.<EndProcessRequest>b__d()
   at System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f)
   at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action)
   at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
   at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
        Logged on Wednesday, March 28, 2012 at 3:47:20 PM

【问题讨论】:

    标签: c# asp.net-mvc-3 telerik


    【解决方案1】:

    行:

    appRepository.GetAll().PendingForRisk(user)
    

    正在将可能有很多行的内容加载到内存中。此外,接下来的两行使情况变得更糟:

    let client = c.Clients.FirstOrDefault(t => t.MainOwner == (int)RelationType.Borrower)                                   
    let timing = c.Timings.Where(t => t.ApplicationID == c.ID && t.Status == (int)AppStatus.Commited).OrderByDescending(t => t.Date).Take(1).FirstOrDefault()
    

    对于每个单独的appRepository 项目,您正在评估ClientsTimings,其中可能还有许多元素。这些可能是延迟加载的,并将所有更多的对象带入内存。简单地说,例外很明显——您一次将太多对象加载到内存中。

    不幸的是,在不知道appRepository 究竟是什么(Linq-to-Sql、实体框架等)的情况下,很难提供有关如何改进代码的任何提示。 appRepository.GetAll().PendingForRisk(user)的返回类型是什么?

    【讨论】:

    • 我有类似的错误 - 它出现在实际访问数据库之前,解析器在解析查询时遇到了这个
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-02
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多