Looking at this code:
public async Task<T> ConsumeAsync()
{
await a();
await b();
await c();
await d();
//..
}
Let's say that a,b,c,d
also have nested async awaits (and so on)
Async/await POV - for each await
, there is a state machine being kept.
Question (theoretical):
As each state machine is kept in memory, could this cause big memory consumption?
It might be a vague question to ask, but if there are many states, it seems inevitable not to wonder about the sizes of state machines being kept.
C# Language Async-Await Async/await will only improve performance if it allows the machine to do additional work.
If the work you have is I/O-bound, use async and await without Task. Run . You should not use the Task Parallel Library. If the work you have is CPU-bound and you care about responsiveness, use async and await , but spawn off the work on another thread with Task.
with async / await , you write less code and your code will be more maintainable than using the previous asynchronous programming methods such as using plain tasks. async / await is the newer replacement to BackgroundWorker , which has been used on windows forms desktop applications.
If a method has no async operations inside it there's no benefit in making it async . You should only have async methods where you have an async operation (I/O, DB, etc.). If your application has a lot of these I/O methods and they spread throughout your code base, that's not a bad thing.
As each state machine is kept in memory , could this cause big memory consumption ?
Very unlikely. Each state machine will occupy a few dozen bytes, at the outside.
So it will only matter when you have very many of them. Nesting isn't really going to cause that, but executing the members of a Task[]
might.
But that is not really new or different form any other resource type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With