0

I would like to have a program that executes one million asynchronous tasks over a long period of time. I want to manage the concurrency dynamically, but we can imagine simpler version with concurrency 20.

I made a system that is using tokio::task::spawn for each of those asynchronous tasks. tokio::task::spawn requires a Future with the 'static lifetime. If I somehow make my futures static, how would that influence my program long term memory consumption? Would there be some memory accumulation of the futures that were already awaited? Or only thing that would actually accumulate is the thing that is making that future 'static?

Could I pass into each future somewhere dummy static data like &str to make the future static but hold no memory after it is awaited? I'm missing some core understanding here.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Lukáš Křivka
  • 953
  • 6
  • 9

1 Answers1

1

Answering your question

In general, any reference that is 'static lives for the entire lifetime of the program; it is only deallocated after main exits.

If you have many distinct values that have the 'static lifetime (i.e. they aren't all references to one shared value), each one will take up some amount of memory. Generally that amount of memory is constant throughout the lifetime of your program and should be easy to account for.

See also:

Answering your problem

Nothing about futures requires that you must have a 'static lifetime. What you are citing is a bound on generic types that states that any references present must have the static lifetime. That doesn't mean that a reference is required. Instead, use owned values.

See also:

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • Thanks. I finally understood it after your earlier comments. I'm now using move with full clone or Arc and will maybe play with scoped tasks like here https://docs.rs/tokio-scoped/0.1.0/tokio_scoped/ – Lukáš Křivka Apr 01 '20 at 18:10