1. Introduction

Flutter uses the Dart development language. Dart RunTime is always present in both debug and release versions, but there are significant differences in how Flutter is built

2. Debug and release differences

Dart builds to the device in the debug version and consists of three parts:

1.Dart RunTime

2. Jit (real-time compiler for Android)/ Interpreter (interpreter for IOS)

3. Debug and analysis services

Under release

1.Dart RunTime

Dart RunTime, which exists in both modes, contains the garbage collector and is a necessary component to allocate and free memory when objects are instantiated and become unreachable.

3. Garbage collector arena

For Flutter, many objects are created: for example, Stateless widgets are destroyed and rebuilt when the state of the application changes or becomes no longer visible from the moment they are created. Most objects have a short life cycle and can run to thousands of widgets if the UI of the application becomes relatively complex

For the above reasons, many people thought that Flutter was not written in Java, Object C, or JavaScript. Is this language really capable of being created and destroyed so frequently?

Java garbage collector

Java memory in the JVM is divided into four parts:

1.Java stack: the main function is to store all the data when the method is executed, and the stack frame represents the execution of a method. Each method is put on and off the stack frame in the VIRTUAL machine from call to execution

2. Local method stack: Mainly for native services, such as C and C++ methods

3. Method area: stores class information loaded by virtual machine, constant, static variables, data compiled by compiler, etc

4. Heap area: The memory of all objects created by new is allocated in the heap, and the heap memory is divided into new and old. The newly created objects are stored in the new generation

Note:

1. StackOverflow occurs when the method stack depth is greater than the JVM depth, for example: stackOverflow

2. If both the new generation and the old generation are full, it will cause OutOfMemory.

Garbage collector algorithm

Garbage collection is mainly aimed at heap memory, and the algorithm mainly includes garbage determination and collection, garbage recovery, garbage recovery time

1. Reference counting (deprecation) : If the object is referenced, it will +1, and when it is not referenced, it will be recycled, but reference counting cannot solve the problem of objects calling each other

2. Reachability algorithm: Through gc root object to start the search, unreachable objects will be recycled, the main types of reference are strong reference, weak reference, when there is a strong reference would rather throw OOM rather than recycling, but weak reference, may be recycled.

3. Mark removal method: The search finds that the object without reference is directly recycled, but too many fragments are caused

4. Copy algorithm: search and scan the object without reference, open up new memory space, copy the surviving object to the new memory, the old memory is directly deleted, because of the exchange space, suitable for fewer objects, and the memory space is shortened by half

5. Mark arrangement method: on the basis of mark removal method, remove non-viable objects and move the surviving objects behind to solve the fragmentation problem

The above garbage collector algorithm is not explicitly specified in the JVM and is implemented by various vendors

Object-c garbage collector

OC lacks a relatively complete memory management mechanism in earlier versions, requiring developers to release it manually. After Xcode4.2, Automatic Reference Counting (ARC) mechanism was introduced.

The ARC mechanism

ARC is called automatic reference Counting. Common ownership keywords in ARC:

Assign corresponds to __unsafe_unretained. When released, the retrieved object still points to the same address, causing wild Pointers

Copy corresponds to the __strong keyword, and the copy method is called during assignment

Retain corresponds to the __strong keyword

Strong corresponds to the keyword __strong

Unsafe_unretained corresponds to the keyword unsafe_unretained

Weak corresponds to the keyword weak

ARC internal implementation

The reference counting behind ARC relies primarily on three methods:

Retain increases the reference count

Release lowers the reference count and releases the object when the reference count is zero

Autorelease Lowers the reference count after the current Auto Release pool ends

JavaScript garbage collector

JavaScript has an automatic garbage collection mechanism, and the garbage collector performs this hype periodically, at fixed intervals, depending on the browser implementation, or at specified times

Methods of garbage collection

The most important collection method in javaScript is to tag a value that is not currently in use and then wait for its memory to be reclaimed

** Reference count ** (no longer used)

Performance Issues The garbage collector runs periodically, and if the amount of memory allocated by a variable is large, the collection effort can be quite large

Dart garbage collector

Dart’s garbage collector is generational and consists of two parts: the Cenozoic spatial collector, the parallel marker scan collector, and, importantly, the scheduler

The scheduler

In the Flutter engine, to minimize the impression of garbage collection on application and UI performance, hooks are provided with the garbage collector. When the engine detects that the application is idle (not interacting with the user), an alarm is raised, giving the garbage collector the opportunity to run its collection phase without affecting performance. And the garbage collector can run memory compression during these idle times, optimizing memory with fewer memory fragments

Cenozoic space collector

This part is similar to Java’s copy algorithm, which is used to clean up objects with short life, such as the Stateless part. Although it will block threads, when combined with the scheduler, the pause of the application during runtime is almost not sensed. Essentially, the new object is allocated to continuous space in memory. Will be assigned to the next available space, until you fill the allocation of the memory, but the use of the Dart is a concave and convex pointer, so this process is very fast, allocate a new object space consists of two parts, only half, at any time after the half full, the object of the activity will be copied to the other half space, half will all empty, determine whether the object activity, The collector starts with the root objects and detects what they refer to. This part is similar to Java’s reachability algorithm, in that the referenced objects are copied into another space

Parallel mark scan collector

When objects reach a certain lifetime, they are lifted to a new memory space and managed by another collector, which has two phases:

Traverses the object, marking the object that is still in use

Scan the entire store and reclaim unmarked objects, then clear all marks

4. To summarize

As described above, Dart’s garbage collector approach follows the implementation of some languages. However, it is important to note that Dart’s private computers are independent of each other. Each IOslates garbage collection event runs on a separate thread and does not affect the performance of other Computers. So Computers are used to avoid UI freezes and perform frequent retrieval operations well. This is one of the reasons dart is used as the primary language of Flutter.