简体   繁体   English

System.OutOfMemoryException:angularjs中抛出了类型'System.OutOfMemoryException'的异常

[英]System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown in angularjs

I have spent couple of weeks for this issue. 我花了几个星期来解决这个问题。 but still I can't resolve this issue. 但我仍然无法解决这个问题。

i am calling a web API service by using http in angularjs 我使用angularjs http调用Web API服务

           $http({
               method: 'GET',
               url: rootUrl + '/api/Project/ProjectList',
               headers: {
                   'Content-Type': "application/json; charset=utf-8"
               }
           }).success(function (response) {
               $scope.ProjectList = response;
           }).error(function (response, errorCode) {
               if (errorCode == 444) {
               }
           })

I have put break point in server and client side coding. 我已经在服务器和客户端编码中提出了突破点。

When i call the service, the server side method hit quickly 当我调用该服务时,服务器端方法快速命中

My server side method ( am Using MVC WEB API with entity framework ) 我的服务器端方法( 使用带有实体框架的MVC WEB API

    [ActionName("ProjectList")]
    [HttpGet]   
    public IList<Project> ProjectList(Project projectModel)
    {
        return objIProjectService.ListOfProject();
    }

I checked, the service return 8 records( 8 rows from database) with a break point in objIProjectService.ListOfProject(); 我检查过,服务返回8条记录(数据库中有8行),并在objIProjectService.ListOfProject();有一个断点objIProjectService.ListOfProject(); this line. 这条线。

everything is going good. 一切都很顺利。 But my (response) http.success and http.error call back functions are hitting very slow. 但是我的(响应) http.success and http.error回调函数的速度非常慢。

Please see below image for the performance while i calling the http methods 我在调用http方法时请看下面的图像

在此输入图像描述

Finally the http error function hit after 5 or 10 mins with this below error message. 最后, http error function在5或10分钟后命中,并显示以下错误消息。

System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown System.OutOfMemoryException:抛出类型'System.OutOfMemoryException'的异常

This is the problem. 这就是问题。 Please let me know how can i solve it? 请让我知道我该如何解决?

Actually am did something for this issue. 实际上我为这个问题做了一些事情。

  • I have cleared the temp folder - not working 我已经清除了临时文件夹 - 没有工作
  • I have restart the Visual studio and clean the solution, restart my system and repairing visual studio.- not working 我重新启动Visual Studio并清理解决方案,重新启动系统并修复visual studio.-不工作
  • But if I have deleted some rows in database (am using sql server 2008 r2 ), then it's working. 但是,如果我删除了数据库中的某些行(使用sql server 2008 r2 ),那么它正在运行。

For example, if my database table have below than 7 rows, then it's working fast without that error. 例如,如果我的数据库表有少于7行,那么它没有错误就能快速运行。 But if my table have more than 8 rows, then it's working very slowly and threw the error?? 但是,如果我的表有超过8行,那么它的工作非常缓慢,并抛出错误? Why?? 为什么?? re could you please share your solution if you stuck this issue. 如果你坚持这个问题,请你分享你的解决方案。

I think the problem is that the serialiser is accessing all the related properties on your project Class, so rather than directly returning the Entity Framework Class, create a new class to represent the data that you wish to send through your api (research further into DTO classes for further information) 我认为问题在于序列化程序正在访问项目类的所有相关属性,因此不是直接返回实体框架类,而是创建一个新类来表示您希望通过api发送的数据(进一步研究到DTO)课程以获取更多信息)

You can use the Select Linq method to get a list of your new dto class populated with the data from your EF call. 您可以使用Select Linq方法获取填充了EF调用数据的新dto类的列表。

var projects = objIProjectService.ListOfProject();

return projects.Select(p => new ProjectDTO() {
   ID = p.Id
   //... other properties of DTO class
}).ToList();

even better, if you put this select method, into your EF query (ie context.projects.Select(/* select info here */).ToList() , you can make sure EF is only bringing back the fields that you need 更好的是,如果你把这个select方法放入你的EF查询(即context.projects.Select(/* select info here */).ToList() ,你可以确保EF只带回你需要的字段

when building an API always check the json/XML response, make sure the serialised data contains what you were expecting it to produce. 在构建API时总是检查json / XML响应,确保序列化数据包含您期望它生成的内容。 with entity framework this response can end up huge as it navigates through all the related tables pulling out all the linked information and then attempting to serialise it. 使用实体框架,这种响应最终会变得非常庞大,因为它会浏览所有相关的表,提取所有链接的信息,然后尝试将其序列化。

as a personal preference I always prefer to return an IHttpActionResult it allows you to manage what is being sent back to the client especially when there are issues, the controller has a number of methods you can use to createthis ie OK(), BadRequest(), InternalServerError() ... 作为个人喜好,我总是喜欢返回IHttpActionResult它允许你管理发送回客户端的内容,特别是当出现问题时,控制器有许多方法可以用来创建它,即OK(), BadRequest(), InternalServerError() ......

Open Sql Server Profiller and watch EF generated sql queries and results. 打开Sql Server Profiller并观察EF生成的sql查询和结果。 And then try execute on sql window this raw queries. 然后尝试在sql窗口执行这个原始查询。 Probably you'ill understand after this. 可能你会在此之后理解。

The issue here is that the serializing of the information is taking to much time. 这里的问题是信息的序列化花费了很多时间。 You are accessing all the related classes of your project class, which may cause a significant damage when dealing with memory aspects. 您正在访问项目类的所有相关类,这可能会在处理内存方面时造成重大损害。

you can resolve this in two ways: 你可以通过两种方式解决这个问题:

One is to return your promises and then fetch the data: 一种是返回你的承诺,然后获取数据:

 return $http({
               method: 'GET',
               url: rootUrl + '/api/Project/ProjectList',
               headers: {
                   'Content-Type': "application/json; charset=utf-8"
               }
           }).then(function (response) {

A second option will be in the code, using Entity framework or linq queries. 第二个选项将在代码中,使用Entity框架或linq查询。

Use select queries to lower the burden on the server side and client side as follow: 使用选择查询可以降低服务器端和客户端的负担,如下所示:

    var projectsQuery = from p in ProjectModel as pm where pm.Id = p.Id select p.SomeValue
return projectsQuery 

this way you will be able to lower the burden of the data serializing and might possibly avoid the outOfMemoryExcexption. 这样您就可以降低数据序列化的负担,并可能避免outOfMemoryExcexption。

There is no need in opening the Sql Profiler here since the data from the server side comes at a reasonable time. 这里没有必要打开Sql Profiler,因为来自服务器端的数据是在合理的时间。

The HTTP specification doesn't impose a specific size limit for posts. HTTP规范没有对帖子强加特定的大小限制。 The problem with GET queries is that the parameters are embedded in the URL, which is limited in size (this limit is browser and server dependent). GET查询的问题是参数嵌入在URL中,其大小有限(此限制取决于浏览器和服务器)。 Don't use a GET query for big data use a POST one. 不要对大数据使用GET查询使用POST。

there is another topic If you are using MVC and you working on big data, you need to use this one. 还有另一个话题如果您正在使用MVC并且处理大数据,则需要使用此数据。

<appSettings>
  <add key="aspnet:MaxJsonDeserializerMembers" value="150000" />
</appSettings>

or 要么

<system.webServer>
<security>
    <requestFiltering>
        <requestLimits maxAllowedContentLength="1000000" />
    </requestFiltering>
</security>

from : MSDN 来自: MSDN

I hope these will solve your problem. 我希望这些能解决你的问题。

Going further with Chris Warnes suggestion and describing the possible reason. 进一步与克里斯沃恩斯建议和描述可能的原因。

Let me guess... you use entity framework with lazy loading enabled. 让我猜一下......你使用启用了延迟加载的实体框架。 So during serialization every navigational property you have is loading data lazily and recursively sometimes. 因此,在序列化期间,您拥有的每个导航属性有时会懒惰地递归加载数据。 For instance: Project contains Worker which contains reference to same Project in some navigational property such as WorkerProjects . 例如: Project包含Worker ,它包含某些导航属性(如WorkerProjects对同一Project引用。

Serializer tries to make sense out of it, but it cannot, without some hint. Serializer试图理解它,但如果没有一些暗示它就不能。 the easiest way to check what is going on, is to disable entity framework lazy loading and get that breakpoint there, right after ToList() and just before the return . 检查实际情况的最简单方法是禁用实体框架延迟加载并在ToList()return之前获取该断点。

BTW. BTW。 Serializing this kind of structure to JSON is - kind of - impossible. 将这种结构序列化为JSON是 - 有点 - 不可能。 You need to have external mechanism (such as cycle.js is using XPath to encode such a reference) to deal with duplicated references and cycles. 您需要有外部机制(例如cycle.js使用XPath来编码这样的引用)来处理重复的引用和循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.OutOfMemoryException:抛出了“System.OutOfMemoryException”类型的异常 - System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown 异常:抛出 System.OutOfMemoryException - Exception: System.OutOfMemoryException was thrown 抛出了类型&#39;System.OutOfMemoryException&#39;的异常 - Exception of type 'System.OutOfMemoryException' was thrown 类型为&#39;system.outofmemoryexception&#39;的异常被抛出 - exception of type 'system.outofmemoryexception' was thrown 引发了“ System.OutOfMemoryException” - 'System.OutOfMemoryException' was thrown 类型为“ System.OutOfMemoryException”的异常 - There was an exception of type 'System.OutOfMemoryException' 类型&#39;System.OutOfMemoryException&#39;的异常 - An exception of type 'System.OutOfMemoryException' 读取大文件-System.OutOfMemoryException:引发了类型为&#39;System.OutOfMemoryException&#39;的异常。 在C#中 - Reading large file - System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown. in C# c# System.OutOfMemoryException: 'System.OutOfMemoryException' 类型的异常被抛出。 - c# System.OutOfMemoryException: 'Exception of type 'System.OutOfMemoryException' was thrown.' 在SFTP连接中抛出了类型&#39;System.OutOfMemoryException&#39;的异常 - Exception of type 'System.OutOfMemoryException' was thrown in a SFTP connection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM