I've been developing an Asp.Net Core Razor Pages app.I put the first released version on a production server a couple of months ago,and been updating it regularly.Everything was working well until yesterday when I noticed
the app keeps logging out after a minute more or less.After searching quite a bit,I realized the problem is that server has limited my app to only use 256 MB of memory,and the reason was,as they said,the app was using too much memory! As far as I know,.Net
Core comes with a feature called Garbage Collection which releases memory automatically! It detects unused objects by itself and disposes memory of them.So,What am I doing wrong? What should I do? Any tip would be greatly appreciated as our school work can't
be done because of this problem!
You could have a memory leak. Check that all objects that implement .Dispose() are called with a using statement. There are many tools for determining memory usage. Also the 2.2 runtime does not have an aggressive gc, nor memory limit support. You might want
to update core 3.0 which has added support for docker memory limits.
I used the Diagnostic Tools provided by Visual Studio to keep track of memory usage,and it appears that the memory gets added without Garbage Collection disposing of any memory! I don't know if it should be the case! I use .Net Framework Core to access data,and
the way it works in Asp.Net Core is Dependency Injection providing an instance of Datacontext in Razor Page Class.Any idea what I should do?
When you say "app kepping logging out" do you mean application is crashing and exiting ? Or is the user getting logged out ?
When you say 256 MB it is hard disk right ? My application hardly uses 1 - 1.2 MB of RAM. There must be some code which is running in a never ending loop or some code which is creating huge number of objects. Like a 1 million record taken from database and
applying a sorting or grouping in backend. or performing an another set of logic on huge set of data. In development environment with single user and 100s of data it may not be an issue. But in production with more data and more concurrent users it will cause
problem. Try to find the difference in data between development and production database
Are you hosting in IIS or simply using the inbuilt kestrel in run command window. Because IIS recovers from unhandled exception, but standalone console window will crash on unhandled exception.
If you are using Session to manage logins, stop and use authentication cookies instead. At least that way, your users won't get logged out if the app pool recycles because of memory pressure.
That should give you some breathing space to learn about profiling to try to identify the source of your memory leak (if that is in fact the root of the problem).
I mean the user keeps getting logged out! I have uploaded the released version in IIS.The server hosting my app has put a 256 limit quota on my app,which has caused the issue!
Without the limit,the app was working fine.By the way,I have at most 12K rows to query over,not millions,and to access data,I'm using LINQ and Entity Framework Core.
Actually,I'm using cookies for athentication.Here's the code:
private void Authenticate(string userName, string role)
{
var identity = new ClaimsIdentity(new[] {
new Claim(ClaimTypes.Name, userName),
new Claim(ClaimTypes.NameIdentifier, userName),
new Claim(ClaimTypes.Role, role)
}, CookieAuthenticationDefaults.AuthenticationScheme);
var principal = new ClaimsPrincipal(identity);
HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal,
new AuthenticationProperties { IsPersistent = LoginData.RememberMe });
}
No,because as far as I know,It's the Garbage Collection to dispose of the data context or any other objects that aren't needed.Am I right?
Garbage collection is handled automatically by .NET framework in most cases, Having said that, you might require of disposing connection objects properly, However you better switch on IIS logs to investigate further.
the GC only tracks managed memory. if the object, like a database context, uses unmanaged memory, the GC does not know about it. You must call Dispose() to free the unmanaged memory. also the GC favors alloc over compact (at least until core 3.0 with memory
constraint support).
most likely you are not calling Dispose(). if you inject a dbcontext into a controller, the controller dispose should call dispose on the dbcontext. be sure you are doing this.
Thanks for your reply,but as far as I know,Dependency Injection in Asp.Net Core is supposed to dispose the data-context object when the request finishes.It injects the service when the page Model is created and disposes the memory of the object when the
request is served with the service. Isn't it right?
Member
6 Points
36 Posts
Asp.Net Core Razor Pages Keeps Logging out
Jun 16, 2019 06:06 AM|deh_meisam|LINK
I've been developing an Asp.Net Core Razor Pages app.I put the first released version on a production server a couple of months ago,and been updating it regularly.Everything was working well until yesterday when I noticed the app keeps logging out after a minute more or less.After searching quite a bit,I realized the problem is that server has limited my app to only use 256 MB of memory,and the reason was,as they said,the app was using too much memory! As far as I know,.Net Core comes with a feature called Garbage Collection which releases memory automatically! It detects unused objects by itself and disposes memory of them.So,What am I doing wrong? What should I do? Any tip would be greatly appreciated as our school work can't be done because of this problem!
Contributor
6711 Points
2334 Posts
Re: Asp.Net Core Razor Pages Keeps Logging out
Jun 16, 2019 08:04 AM|cnranasinghe|LINK
I have no idea of the memory quota, However it would be better to profile your app using a tool like "glimpse",
Check this
All-Star
58474 Points
15793 Posts
Re: Asp.Net Core Razor Pages Keeps Logging out
Jun 16, 2019 03:47 PM|bruce (sqlwork.com)|LINK
Member
6 Points
36 Posts
Re: Asp.Net Core Razor Pages Keeps Logging out
Jun 16, 2019 08:32 PM|deh_meisam|LINK
I used the Diagnostic Tools provided by Visual Studio to keep track of memory usage,and it appears that the memory gets added without Garbage Collection disposing of any memory! I don't know if it should be the case! I use .Net Framework Core to access data,and the way it works in Asp.Net Core is Dependency Injection providing an instance of Datacontext in Razor Page Class.Any idea what I should do?
All-Star
58474 Points
15793 Posts
Re: Asp.Net Core Razor Pages Keeps Logging out
Jun 16, 2019 10:40 PM|bruce (sqlwork.com)|LINK
Contributor
6711 Points
2334 Posts
Re: Asp.Net Core Razor Pages Keeps Logging out
Jun 17, 2019 04:41 AM|cnranasinghe|LINK
Scope would be broader to investigate, some similar issues are discussed here,
Participant
1246 Points
564 Posts
Re: Asp.Net Core Razor Pages Keeps Logging out
Jun 17, 2019 07:26 AM|nideeshm|LINK
When you say "app kepping logging out" do you mean application is crashing and exiting ? Or is the user getting logged out ?
When you say 256 MB it is hard disk right ? My application hardly uses 1 - 1.2 MB of RAM. There must be some code which is running in a never ending loop or some code which is creating huge number of objects. Like a 1 million record taken from database and applying a sorting or grouping in backend. or performing an another set of logic on huge set of data. In development environment with single user and 100s of data it may not be an issue. But in production with more data and more concurrent users it will cause problem. Try to find the difference in data between development and production database
Are you hosting in IIS or simply using the inbuilt kestrel in run command window. Because IIS recovers from unhandled exception, but standalone console window will crash on unhandled exception.
Nideesh.S
Mark as answer, If it helps you.
All-Star
194869 Points
28101 Posts
Moderator
Re: Asp.Net Core Razor Pages Keeps Logging out
Jun 17, 2019 01:52 PM|Mikesdotnetting|LINK
If you are using Session to manage logins, stop and use authentication cookies instead. At least that way, your users won't get logged out if the app pool recycles because of memory pressure.
https://www.mikesdotnetting.com/article/335/simple-authentication-in-razor-pages-without-a-database
That should give you some breathing space to learn about profiling to try to identify the source of your memory leak (if that is in fact the root of the problem).
Member
6 Points
36 Posts
Re: Asp.Net Core Razor Pages Keeps Logging out
Jun 17, 2019 09:21 PM|deh_meisam|LINK
I mean the user keeps getting logged out! I have uploaded the released version in IIS.The server hosting my app has put a 256 limit quota on my app,which has caused the issue!
Without the limit,the app was working fine.By the way,I have at most 12K rows to query over,not millions,and to access data,I'm using LINQ and Entity Framework Core.
Member
6 Points
36 Posts
Re: Asp.Net Core Razor Pages Keeps Logging out
Jun 17, 2019 09:26 PM|deh_meisam|LINK
Actually,I'm using cookies for athentication.Here's the code:
Member
6 Points
36 Posts
Re: Asp.Net Core Razor Pages Keeps Logging out
Jun 17, 2019 09:29 PM|deh_meisam|LINK
No,because as far as I know,It's the Garbage Collection to dispose of the data context or any other objects that aren't needed.Am I right?
All-Star
53711 Points
24042 Posts
Re: Asp.Net Core Razor Pages Keeps Logging out
Jun 17, 2019 10:03 PM|mgebhard|LINK
Looks like custom authentication. I’m guessing you have a bug.
Try using Identity.
Contributor
6711 Points
2334 Posts
Re: Asp.Net Core Razor Pages Keeps Logging out
Jun 18, 2019 02:15 PM|cnranasinghe|LINK
Garbage collection is handled automatically by .NET framework in most cases, Having said that, you might require of disposing connection objects properly, However you better switch on IIS logs to investigate further.
https://www.loganalyzer.net/log-analysis/iis-log-files.html
All-Star
58474 Points
15793 Posts
Re: Asp.Net Core Razor Pages Keeps Logging out
Jun 18, 2019 03:29 PM|bruce (sqlwork.com)|LINK
the GC only tracks managed memory. if the object, like a database context, uses unmanaged memory, the GC does not know about it. You must call Dispose() to free the unmanaged memory. also the GC favors alloc over compact (at least until core 3.0 with memory constraint support).
most likely you are not calling Dispose(). if you inject a dbcontext into a controller, the controller dispose should call dispose on the dbcontext. be sure you are doing this.
Member
6 Points
36 Posts
Re: Asp.Net Core Razor Pages Keeps Logging out
Jun 20, 2019 06:13 PM|deh_meisam|LINK
Thanks for your reply,but as far as I know,Dependency Injection in Asp.Net Core is supposed to dispose the data-context object when the request finishes.It injects the service when the page Model is created and disposes the memory of the object when the request is served with the service. Isn't it right?
None
0 Points
1 Post
Re: Asp.Net Core Razor Pages Keeps Logging out
Jan 13, 2021 07:51 AM|justinh70|LINK
run as admin