JSBinding + SharpKit / Memory Management (GC)

C# and JavaScript both have Garbage Collection (GC). They should not conflict with each other.

Class type object

Class is reference type. We maintain a Map<> to store one-to-one relationship between C# class object and corresponding JavaScript object.

An example:

// C#
List<int> lst = new List<int>();

// JavaScript (Compiled from C#)
var lst = new System.Collections.Generic.List$1.ctor(System.Int32.ctor);

The process of returning a C# class object to JavaScript is:

1) C# gets JavaScript object ID from JavaScripit.

// File: System_Collections_Generic_List77.javascript
_jstype.definition.ctor = function(t0) { CS.Call(5, 249, 0, true, this, t0.getNativeType()); } // 1) Here, ‘this‘ will be passed to C#,C# wil get an ID (an int value representing JavaScript object)

2) C# creates a List<T> object (T is from JavaScript). See code below, Line14.

// System_Collection_Generic_List77Generated.cs

 1 static bool ListA1_ListA11(JSVCall vc, int argc)
 2 {
 3     int _this = JSApi.getObject((int)JSApi.GetType.Arg); // we have JavaScript object here!
 4     JSApi.attachFinalizerObject(_this); // (4)
 5     --argc;
 6
 7     ConstructorInfo constructor = JSDataExchangeMgr.makeGenericConstructor(typeof(System.Collections.Generic.List<>), constructorID0);
 8     if (constructor == null) return true;
 9
10     int len = argc - 1;
11     if (len == 0)
12     {
13         JSMgr.addJSCSRel(_this, // (3)
14             constructor.Invoke(null, new object[]{})  // (2)
15         );
16     }
17
18     return true;
19 }

3) C# stores one-to-one relationship. see code above, line 13.

NOTE: we store List<> object, and this object must be removed from map some time later, otherwise it will never be collected by C# GC.

4) Register a C# finalizer callback for JavaScript object, when it is collected by JavaScript GC, C# will be notified, and will remove the one-to-one relationship in C# map. See code above, Line 4.

5) Any time we want to return a C# object to JavaScript, we will first search the map, if corresponding JavaScript already exists, we simply return it. Otherwise we create and return a new JavaScript object. In this example, we already have a JavaScript (Line 3), so we don‘t need to create a new one.

Summary

for class type object, we will store C# object and JavaScript object relationship to a map. JavaScript object‘s life cycle is controlled by JavaScript GC, and C# object is removed when that JavaScript is collected.

Value type object (struct)

struct is value type. It‘s not possible to create a one-to-one relationship.  When we want to return a C# struct to JavaScript, we always create a new one. And the relationship is: it‘s OK to find C# struct by JavaScript object ID, but it‘s not possible to find a JavaScript object by C# struct object.

时间: 2024-08-06 20:05:57

JSBinding + SharpKit / Memory Management (GC)的相关文章

Java 几个有用的命令 - All Options, Memory Options, GC Options, System Properties, Thread Dump, Heap Dump

jcmd  ##Refer to http://www.cnblogs.com/tang88seng/p/4497725.html java -XX:+PrintFlagsFinal -version   ##Display all JVM options java -XX:+PrintCommandLineFlags -version   ##Display default JVM Memory and GC options -XX:InitialHeapSize=134209344 -XX:

Android Memory Management, OutOfMemoryError

A Android框架强制每个进程的24 MB内存限制.在一些旧的设备,如在G1,限制为16 MB 更低,更重要的是,由位图使用的内存限制.处理图像的应用程序,它是很容易达到此限制,并获得与OOM 异常死亡 的过程:E / dalvikvm堆(12517):1048576字节外部分配这个 过程中过大的E / GraphicsJNI(12517): VM将不会让我们分配1048576字节 / AndroidRuntime(12517):关闭VM / dalvikvm(12517):主题ID = 1

JSBinding + SharpKit / Home

Description JSBinding is a great tool enabling you to run actual JavaScript in Unity3D. It contains Mozilla SpiderMonkey JavaScript engine version 33 library. New version is expected to work with SharpKit (sharpkit.net). SharpKit is an open source to

JSBinding+SharpKit / 菜单介绍

[JSB | Generate JS and CS Bindings] 生成绑定,即让 Js 和 Cs 互通.详情请看 JSBinding+SharpKit / 生成 JavaScript 绑定 [JSB | Add SharpKit JsType Attribute for all Structs and Classes] 在所有逻辑代码中,所有类的定义前面加上 [JsType(JsMode.Clr, "~/Assets/StreamingAssets/JavaScript/SharpKitG

JSBinding + SharpKit / 编译 Cs 成 Js

轻轻一点菜单:[JSB | Compile Cs to Js] 主要产出:StreamingAssets/JavaScript/SharpkitGeneratedFiles.javascript,你的所有逻辑代码都在这里 其他产出: Temp/AllInvocations.txt:记录所有逻辑代码对框架代码的调用 (1) Temp/AllInvocationsWithLocation.txt:同上,但同时记录每个调用的文件名和行号 (2) Temp/YieldReturnTypes.txt:记录

JSBinding + SharpKit / Important Notes

Serialization of List<T> is not supported. public List<int> lst; // NOT supported, use int[] instead About menu: JSB | Add SharpKit JsType Attribute for all Structs and Classes. If you execute this menu more than once, only one JsType will be

JSBinding + SharpKit / 初体验:下载代码及运行Demo

QQ群:189738580 git地址:https://github.com/qcwgithub/qjsbunitynew.git插件源码地址(不包含SpiderMonkey源代码):https://github.com/qcwgithub/qjsbmozillajswrap.git 首先用 Unity 打开代码目录下的 proj 工程 由于使用的插件存在依赖,请将 Assets/Plugins/x86/mozjs-28.dll 拷贝于 Unity 安装目录下.如图所示. 如果没有做这个步骤,运

Fixed Partition Memory Management UVALive - 2238 建图很巧妙 km算法左右顶点个数不等模板以及需要注意的问题 求最小权匹配

/** 题目: Fixed Partition Memory Management UVALive - 2238 链接:https://vjudge.net/problem/UVALive-2238 题意:lv 思路:lrjP352. 来自lrj训练指南. n个程序作为左边结点, n*m个结点在右边:由于只要求n个程序在右边能找到的匹配点,km算法可以求解.修改nx,ny的值. if(f[i][j]==-1){ for(int k = 1; k <= n; k++) love[i][j*n+k-

Objective -C Memory Management 内存管理 第一部分

Objective -C Memory Management??内存管理??第一部分 Memory management is part of a more general problem in programming called resource management. 内存管理是资源管理的一部分. Every computer system has finite resources for your program to use. These include memory, open fi