Skip to content
This repository was archived by the owner on Nov 17, 2022. It is now read-only.

Commit 3968275

Browse files
authored
Merge pull request #1475 from NativeScript/bektchiev/document-marking-mode-none
docs(android-runtime): Document markingMode: "none" option
2 parents 537711d + 9e9eec0 commit 3968275

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

docs/core-concepts/android-runtime/advanced-topics/memory-management.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,26 @@ if the actual `id` has value `123`. In other worlds, creating JavaScript objects
3535

3636
Having said that, this could lead to some scenarios where there are unreachable JavaScript objects (e.g. effective garbage) for which the corresponding Java objects hold a lot of memory (think of `Bitmap`, `String`, `StringBuilder`, I/O buffers, etc.). But because there is little or no memory pressure in the JavaScript heap there is no reason for V8 GC to kick off. Therefore NativeScript for Android runtime doesn't have the chance to release the Java object handles (if any). This could cause `OutOfMemoryError`.
3737

38+
## markingMode: "none"
39+
40+
By default during a V8 GC pass the Android runtime performs an automatic traversal of alive JavaScript instances (a routing called `MarkReachableObjects`). In apps which create huge JavaScript instances graphs however this can cause significant delays in garbage collections, leading to unpleasant blocks of the UI thread.
41+
42+
Basically, this algorithm makes sure that no Java instances will be prematurely collected by the Android Java GC, while they are still being used by the JavaScript side. JavaScript code, however, can be structured in such a way that this additional step becomes unneeded. The only case when it is required is when there are no other references to the Java instance than the scope and the scope is the only thing that takes care to keep it alive.
43+
44+
There's a `app/package.json` option which apps can set in order to disable `MarkReachableObjects`:
45+
```JSON
46+
{
47+
"android": {
48+
"markingMode": "none"
49+
}
50+
}
51+
```
52+
53+
The code inside `tns-core-modules` and all plugins published by the NativeScript Team (since version 5.1.0) are written in such a way, that it does not depend on the scope to keep those Java instances alive. This makes apps using these plugins fully compatible with the a lot more performant `markingMode: "none"` option.
54+
55+
> **WARNING**: Enabling this option if the JavaScript code dealing with native objects (either in the application or any plugins you are using) does not correctly take care of the lifetime of Java instances may cause unexpected and unpredictable crashes of the application due to Java instances being prematurely collected. Use caution when enabling it and make sure to thoroughly test your apps with different memory constrains and devices! The errors generated in such cases look like this:
56+
`Error: com.tns.NativeScriptException: Attempt to use cleared object reference id=<some-object-id-number>`
57+
3858
## Syncronizing Garabage Collectors
3959

4060
In order to mitigate such issues we provide an experimental feature which tries to synchronize both garbage collectors. One option is to trigger GC for the JavaScript heap so it collect all unreachable JavaScript objects which can hold reference to potentially large Java objects. There are three possible approaches.
@@ -85,4 +105,4 @@ The described approaches are not mutually exclusive. You can combine them as it
85105
* Avoid allocating many large objects at once
86106
* Avoid allocating objects in loops
87107

88-
Be cautious when you write closures as it often hard to observe object retentions along all the reference chain.
108+
Be cautious when you write closures as it often hard to observe object retentions along all the reference chain.

0 commit comments

Comments
 (0)