452 views
in System Integration by
Currently I have a GUI project with a huge amount of objects alive. Here the garbage collection can take up to 100ms which drops some frames during smooth animations. What is the recommended way to avoid garbage collection during animations? Is there any way to limit the garbage collection to a maximum execution time?

1 Answer

0 votes
by

First of all, you should consider to reduce the amount of objects that is kept within the memory. If possible, try to create your objects dynamically during runtime when they are needed. This helps to reduce the necessary runtime for the garbage collection and it will reduce the memory footprint.

Nevertheless, if there is a certain reason for keeping a huge amount of objects within the memory and thus, the garbage collection will disturb your smooth animation, you can try the following:

This is the typical (recommended) implementation of the main loop:

{
  /* Step 1: Receive user inputs (key events)... */
  ...
  /* Step 2: Receive cursor inputs (mouse/touch events)... */
  ...
  /* Step 3: Process expired timers */
  timers = EwProcessTimers();

  /* Step 4: Process the pending signals */
  signals = EwProcessSignals();

  /* Update screen and memory, only if something has changed */
  if ( timers || signals || events )
  {
    /* Step 5: Refresh the screen and draw its content */
    Update( viewport, rootObject );

    /* Step 6: Start the garbage collection */
    EwReclaimMemory();
  }
}

For more details about the main-loop please have a look into the article 'Main loop - How to integrate the generated UI application?'.

You can try to modify the main-loop in the following manner:

{
  /* Step 1: Receive user inputs (key events)... */
  ...
  /* Step 2: Receive cursor inputs (mouse/touch events)... */
  ...

  /* Step 3: Process expired timers */
  timers = EwProcessTimers();

  /* Step 4: Process the pending signals */
  signals = EwProcessSignals();

  /* Step 5: Refresh the screen and draw its content */ 
  if ( timers || signals || events )
    Update( viewport, rootObject );

  /* Step 6: Start the garbage collection */
  else
    EwReclaimMemory();
}

Now, the garbage collection is invoked only, when the system is in idle and there is no pending signal or event or timer. Please be aware, that this might be very dangerous if your application contains effects that are running continuously! Furthermore, if your animation is allocating memory during every animation step, there might be a lot of memory occupied until the end of your animation is reached. Please verify this by using the memory profiling feature.

Alternatively, you can make a combination of the above solutions and switch only to the second solution during the critical animation sequence.

Ask Embedded Wizard

Welcome to the question and answer site for Embedded Wizard users and UI developers.

Ask your question and receive answers from the Embedded Wizard support team or from other members of the community!

Embedded Wizard Website | Privacy Policy | Imprint

...