Score:1

How to cause ASP.NET app to recycle when OutOfMemoryException is thrown

ss flag

I have a ASP.NET app (.NET framework 4.8) that runs in IIS on Server 2019. It happened a few times over that last few weeks that a customer tries to do some operation on the site that requires a large block of memory. The ASP.NET runtime is unable to find such a large block in memory and throws OutOfMemoryException. The site continues to work but some pages are working and some don't. IIS Application pool health is looking as if everything is OK but in reality most of the site is not working until I manually recycle the pool.

The application pool already has recycling set when private bytes are above a few GB but when looking on the w3wp process when the site is not working it is only using a few hundred MB. Is there any way to tell ASP.NET or IIS to recycle when this exception is thrown?

Lex Li avatar
vn flag
It is technically difficult due to 1) you cannot recycle IIS whenever such exceptions occur, as that's over-killing 2) ASP.NET exceptions are well isolated from IIS, so IIS itself won't recycle on certain exceptions. 3) ASP.NET web apps cannot request recycle themselves via IIS API without running as local admin. A more complex mechanism needs to be developed/configured to log ASP.NET exceptions and trigger recycle, but that's more a Stack Overflow question.
djdomi avatar
za flag
I would suggest that you take a look at the code and try to fix this issue. that's a abnormal behavior. but for that case I agree with Lex Li, it's supposed to be a question for Stackoverflow.com
Score:0
cn flag

This needs to be monitored and handled externally from IIS and your application. There is probably an exception in the Application event log that a scheduled task could use as a trigger to stop/start the app pool from the command line.

This also seems like a fairly basic garbage collection setting. At a minimum, all application(s) should be configured for server GC.

Additionally, there is probably code that can be added to facilitate a proper health check. It's been standard procedure for years for a load balancer health check to use a page that forces use of the .NET Framework. That health check must fail if the application pool crashed due to an out of memory exception.

ss flag
I also thought that an ASPX page would fail but some still keep working for some reason. The event is definitly logged in the event log so I will check how to create a trigger from that. Thanks!
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.