Ben
Ben

Reputation: 833

CPU Usage gradually increases in dotnet core webservice

I have a .net Core web service which seems to slowly increase its cpu usage. meaning at the first day it won't go past 10%, the second day it can go up to 20% and so on.

Using the TOP command in linux, all my webservices seems to sometime be shown there (probably when a request is made) and afterward disappear. This specific process after running for a while just stays there constantly consuming cpu even when no request has been made.

the API still working fine, it seems like there are some threads that just keeps hanging and consuming cpu. last time I checked I had 5 threads that consumed 3-4% cpu and didn't die for some reason. My guess is that in some specific scenario a thread just stays alive consuming cpu.

The app runs on ubuntu machine, my first step was trying to create a dump file with ProcDump so I can analyze those threads and maybe find where they are hanging. ProcDump generates a huge 21gb file, which trying to analyze with lldb throws out of memory exception. even tried transferring it to a windows machine to debug with windbg , no help there as it couldn't open the file.

As there is no specific exception or anything I can't really share any piece of code as I have no idea where the issue is... just kind a hoping for some suggestion that might help me get to a solution or at least understand where the problem is.

Thanks a lot for reading, cheers

Upvotes: 2

Views: 3747

Answers (3)

Mario Hewardt
Mario Hewardt

Reputation: 126

A little late but Procdump for Linux just added .NET Core 3 support that generates much more managable sized core dumps. It automatically detects if the target process is .NET Core and does the right thing (i.e., no need to specify switches).

Upvotes: 1

Ben
Ben

Reputation: 833

so iv'e been at it for a while. Eventually found out that my problem was with HttpClient Probably some bad mix of static class and creating new instances of HttpClient that causes the issue Iv'e explained above.

Solved it by utilizing HttpClientFactory as explained here -

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-2.1

Lesson learned :)

Upvotes: 1

Ryan
Ryan

Reputation: 242

You could try using something like jetBrains’ DotMemory, they also have a fairly high level but helpful guide https://www.jetbrains.com/help/dotmemory/How_to_Find_a_Memory_Leak.html it also worth checking your startup file and double checking the services you’ve registered are used in the correct way ie not added as scoped when they should be transient or even a singleton etc

Upvotes: 1

Related Questions