Skip to content

Eliminate GC-induced stuttering during animations by eliminating wasteful allocations #1892

@tonypatino-monoclesociety

Description

I noticed that the Realtime chart demo can stutter due to many repeated and wasteful allocations in sections of code that are called in each update of the data, and it would be great to smooth out the animations and remove stuttering. I've so far identified, verified, and tested ways to reduce the number of allocations by approximately 70% and memory consumed by about 30%, with an attendant improvement in animation quality through various means, such as:

  1. Eliminating the use of convenience iterations in favor of old fashioned iteration.
    for(;;){} instead of
    for(Object foo : inSet){}
  2. Caching the results of String Formatting and using those cached results when the value in the dataset has not changed
  3. Using field level references to store short-lived instances within methods and re-setting them instead of instantiating a new one
    eg 1: instead of instantiating a float[] temp = new float[2]; inside a method
    declare the float[] as a private field and initialize its two locations as needed.
    eg 2: instead of instantiating a new Matrix inside of a function, create a Matrix field and call matrix.reset(); where previously a new Matrix would be instantiated.
  4. Pre-instantiating runnables outside of a local Thread object's run method when each Runnable instance performs the same action on the same object instance

Other means that I have not yet explored but deserve some research include using object pooling for frequently instantiated objects such as FSize, PointF, and the like.

Attached are the results from the memory monitor in Android Studio

allocationsComparison.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions