MainThreadFinalizerProcessor

Native
1.3
object MainThreadFinalizerProcessor
(source)

This objects allows to customize the behavior of the finalizer processor that runs on the main thread.

On Apple platforms Kotlin/Native releases ObjC/Swift objects that were passed to Kotlin when it processes the finalizers after GC. Kotlin/Native can also utilize main run loop to release objects that were passed to Kotlin on the main thread. This can be turned off by setting objcDisposeOnMain binary option to false. For more information, see iOS integration.

This finalizer processor works as follows:

  • Finalizers that must be run on the main thread get scheduled by the GC after the main GC phase is finished
  • A task will be posted on the main run loop in which the scheduled finalizers will start processing
  • Finalizers will be processed inside an autoreleasepool in batches of size batchSize
  • If at some point during task the processor detected that more than maxTimeInTask has passed, it will stop and post another task to the main thread to continue processing finalizers later. Note that if some finalizer takes a very long time, the task will still process the entire batchSize and may significantly overflow maxTimeInTask
  • It's guaranteed that the time interval between tasks is at least minTimeBetweenTasks.

maxTimeInTask and minTimeBetweenTasks allow other tasks posted on the main thread (e.g. UI events) be processed without significant delays.

Properties

Native
1.3

available

true if Kotlin/Native will use MainThreadFinalizerProcessor to process finalizers.

val available: Boolean
Native
1.3

batchSize

How many finalizers will be processed inside a single autoreleasepool.

var batchSize: ULong
Native
1.3

maxTimeInTask

How much time can each task take.

var maxTimeInTask: Duration
Native
1.3

minTimeBetweenTasks

The minimum interval between two tasks.

var minTimeBetweenTasks: Duration