Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

           $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)

    [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(); this line.

everything is going good. But my (response) http.success and http.error call back functions are hitting very slow.

Please see below image for the performance while i calling the http methods

enter image description here

Finally the http error function hit after 5 or 10 mins with this below error message.

System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown

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
  • But if I have deleted some rows in database (am using sql server 2008 r2 ), then it's working.

For example, if my database table have below than 7 rows, then it's working fast without that error. But if my table have more than 8 rows, then it's working very slowly and threw the error?? Why?? re could you please share your solution if you stuck this issue.

like image 518
Ramesh Rajendran Avatar asked May 02 '15 06:05

Ramesh Rajendran


People also ask

How do you fix system OutOfMemoryException exception of type system OutOfMemoryException was thrown?

OutOfMemoryException' was thrown. To resolve this issue, I had to restart Visual Studio or go to the Windows Task Manager and terminate IIS Express process. This error could happen due to a variety of reasons related to memory consumption of the application.

What is System OutOfMemoryException?

When data structures or data sets that reside in memory become so large that the common language runtime is unable to allocate enough contiguous memory for them, an OutOfMemoryException exception results.

How can Outofmemory exception be prevented?

Well, according to the topic of the question, best way to avoid out of memory exception would be not to create objects that fill in that memory. Then you can calculate the length of your queue based on estimate of one object memory capacity. Another way would be to check for memory size in each worker thread.

What is a system outofmemoryexception?

Exception Details: System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown. An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

How to avoid outofmemoryexcexption in SQL Server?

Use select queries to lower the burden on the server side and client side as follow: this way you will be able to lower the burden of the data serializing and might possibly avoid the outOfMemoryExcexption. There is no need in opening the Sql Profiler here since the data from the server side comes at a reasonable time.

Why do I get a system outofmemoryexception with StringBuilder?

Instead, a System.OutOfMemoryException can occur when attempting to increase the length of an instance of the StringBuilder class, beyond what is specified by its current MaxCapacity property. To illustrate, here we have some simple code that generates a new StringBuilder instance called builder:

What type of exception was thrown in ASP NET?

asp.net - Exception of type 'System.OutOfMemoryException' was thrown. - Stack Overflow Exception of type 'System.OutOfMemoryException' was thrown.


1 Answers

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)

You can use the Select Linq method to get a list of your new dto class populated with the data from your EF call.

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 (i.e. context.projects.Select(/* select info here */).ToList(), you can make sure EF is only bringing back the fields that you need

when building an API always check the json/XML response, make sure the serialised data contains what you were expecting it to produce. 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 i.e. OK(), BadRequest(), InternalServerError() ...

like image 139
Chris Warnes Avatar answered Sep 24 '22 02:09

Chris Warnes