ASP.NET Core Singleton instance vs Transient instance performance

时间秒杀一切 提交于 2020-02-01 02:17:52

问题


In ASP.NET Core Dependency Injection, I just wonder if registering Singleton instances will improve performance better than registering Transient instances or not?

In my thinking, for Singleton instance, it just cost once time for creating new object and dependent objects. For Transient instance, this cost will be repeat for each service request. So Singleton seems to be better. But how much performance gaining we have when using Singleton over Transient? Thank you in advanced!


回答1:


Like others have said, performance is not should make your decision here: performance will not be dramatically impacted either way. What should be your consideration is dependencies, both managed and unmanaged. Singletons are best when you're utilizing limited resources, like sockets and connections. If you end up having to create a new socket every time the service is injected (transient), then you'll quickly run out of sockets and then performance really will be impacted.

Transient scope is better when resource usage is temporary and of minimal impact. If you're only doing compute, for instance, that can be transient scoped because you're not exhausting anything by having multiple copies.

You also want to use singleton scope when state matters. If something needs to persist past one particular operation, then transient won't work, because you'll have no state, because it will essentially start over each time it's injected. For example, if you were trying to coordinate a concurrent queue, using semaphores for locks, then you'd definitely want a singleton scoped service. If state doesn't matter, then transient is probably the better scope.

Finally, you must look at other services your service has a dependency on. If you need access to scoped services (such as things that are request-scoped), then a singleton is a bad fit. While you can possibly use a service-locator pattern to access the scoped services, that's a faux pas, and not recommended. Basically, if your service uses anything but other singleton services, it should likely be scoped or transient instead.

Long and short, use a transient scope unless you have a good, explicit reason to make it a singleton. That would be reasons like mentioned above: maintaining state, utilizing limited resources efficiently, etc. If the service will work in a transient scope, and there's no good reason to do otherwise, use transient scope.

Now, ASP.NET Core's DI has both a "transient" and a "scoped" lifetime. Both of these are "transient" in the sense that they come and go, but "scoped" is instantiated once per "scope" (usually a request), whereas "transient" is always instantiated every time it is injected. Here, you should use "scoped" unless you have a good, explicit reason to use "transient".




回答2:


Like mentioned in the replies this isn't really a matter over performance.

For more details please see the link below where the difference is very elaborately explained. https://stackoverflow.com/a/38139500/10551193

The only way that this will matter performance wise is if your constructor is doing a lot of stuff. This can and should be avoided though in all cases.



来源:https://stackoverflow.com/questions/54790460/asp-net-core-singleton-instance-vs-transient-instance-performance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!