This page has been translated automatically.
Video Tutorials
Interface
Essentials
Advanced
How To
UnigineEditor
Interface Overview
Assets Workflow
Settings and Preferences
Working With Projects
Adjusting Node Parameters
Setting Up Materials
Setting Up Properties
Lighting
Landscape Tool
Sandworm
Using Editor Tools for Specific Tasks
Extending Editor Functionality
Built-in Node Types
Nodes
Objects
Effects
Decals
Light Sources
Geodetics
World Objects
Sound Objects
Pathfinding Objects
Players
Programming
Fundamentals
Setting Up Development Environment
Usage Examples
UnigineScript
C++
UUSL (Unified UNIGINE Shader Language)
File Formats
Rebuilding the Engine Tools
GUI
Double Precision Coordinates
API
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
Filesystem Functionality
GUI-Related Classes
Math Functionality
Node-Related Classes
Objects-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
IG Plugin
CIGIConnector Plugin
Rendering-Related Classes
Content Creation
Content Optimization
Materials
Art Samples
Tutorials
Warning! This version of documentation is OUTDATED, as it describes an older SDK version! Please switch to the documentation for the latest SDK version.
Warning! This version of documentation describes an old SDK version which is no longer supported! Please upgrade to the latest SDK version.

Garbage Collector

The Garbage Collector (GC) represents an automatic memory manager responsible for allocation and release of memory for an application. So, developers working with managed code don't have to think about memory management tasks, and write any specific code for this purpose. Automatic memory management helps eliminate common problems, such as forgetting to free an object and causing a memory leak or attempting to access memory for an object that's already been freed.

Here are the benefits the Garbage Collector provides:

  • Frees developers from having to release memory manually.
  • Efficiently allocates objects on the managed heap.
  • Reclaims objects that are no longer used, clears their memory, keeping the memory available for future allocations. Managed objects automatically get clean content to start with, so you don't have to initialize each and every data field in their constructors.
  • Provides memory safety making sure that the content of an object cannot be used by another object.

Garbage Collection Modes#

UNIGINE's Garbage Collector offers you the following set of modes (Engine.GCMode) making the process of garbage collection management flexible:

  • DEFAULT (default) - default C# garbage collector mode. In this case heavy spikes and excessive memory consumption are imminent if you don’t manage your objects properly and do not use the Dispose() method.
  • USE_MEMORY_PRESSURE - passes the information about C++ memory consumption to C#. This results in more frequent GC calls preventing the application from eating too much memory right after startup and removing heavy spikes.
  • EVERY_FRAME - the garbage collector is called every frame. This results in overall performance reduction, but removes heavy spikes.
  • WORLD_SHUTDOWN - the garbage collector is called on closing the world. This mode is ideal if the number of memory allocations is your code is insignificant.

You can set Garbage Collector's mode in the SystemLogic::Init() method or anywhere in your code, and you can change it when necessary, depending on the current situation. It is also safe to do it in your C# components.

Source code (C#)
class UnigineApp
{
  class AppSystemLogic : SystemLogic
  {
    // ...

    public override bool Init()
    {
      Engine.GCMode = Engine.GCMODE.EVERY_FRAME;
      // ...
      return true;
    }
    // ...
}
Last update: 2021-04-29
Build: ()