JSBinding / 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$.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(, , , 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
static bool ListA1_ListA11(JSVCall vc, int argc)
{
int _this = JSApi.getObject((int)JSApi.GetType.Arg); // we have JavaScript object here!
JSApi.attachFinalizerObject(_this); // (4)
--argc; ConstructorInfo constructor = JSDataExchangeMgr.makeGenericConstructor(typeof(System.Collections.Generic.List<>), constructorID0);
if (constructor == null) return true; int len = argc - ;
if (len == )
{
JSMgr.addJSCSRel(_this, // (3)
constructor.Invoke(null, new object[]{}) // (2)
);
} return true;
}
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.
map1[jsObjID] = csObj; // YES
// map2[csObj.hashCode] = jsObjID; // NO
C# object is removed when JavaScript object is collected. (exactly as class object).
JSBinding / Memory Management (GC)的更多相关文章
- Java (JVM) Memory Model – Memory Management in Java
原文地址:http://www.journaldev.com/2856/java-jvm-memory-model-memory-management-in-java Understanding JV ...
- Operating System Memory Management、Page Fault Exception、Cache Replacement Strategy Learning、LRU Algorithm
目录 . 引言 . 页表 . 结构化内存管理 . 物理内存的管理 . SLAB分配器 . 处理器高速缓存和TLB控制 . 内存管理的概念 . 内存覆盖与内存交换 . 内存连续分配管理方式 . 内存非连 ...
- Android内存管理(2)HUNTING YOUR LEAKS: MEMORY MANAGEMENT IN ANDROID PART 2
from: http://www.raizlabs.com/dev/2014/04/hunting-your-leaks-memory-management-in-android-part-2-of- ...
- Android内存管理(1)WRANGLING DALVIK: MEMORY MANAGEMENT IN ANDROID PART 1
from : http://www.raizlabs.com/dev/2014/03/wrangling-dalvik-memory-management-in-android-part-1-of-2 ...
- Java Memory Management(1)
Java Memory Management, with its built-in garbage collection, is one of the language’s finest achiev ...
- 再谈.net的堆和栈---.NET Memory Management Basics
.NET Memory Management Basics .NET memory management is designed so that the programmer is freed fro ...
- Lifetime-Based Memory Management for Distributed Data Processing Systems
Lifetime-Based Memory Management for Distributed Data Processing Systems (Deca:Decompose and Analyze ...
- 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 ...
- js Memory Management
js Memory Management 垃圾回收是一个术语,在计算机编程中用于描述查找和删除那些不再被其他对象引用的对象的处理过程. 换句话说,垃圾回收是删除任何其他对象未使用的对象的过程. 垃圾收 ...
随机推荐
- TCP短连接TIME_WAIT问题解决方法大全
tcp连接是网络编程中最基础的概念,基于不同的使用场景,我们一般区分为“长连接”和“短连接”,长短连接的优点和缺点这里就不详细展开了,有心的同学直接去google查询,本文主要关注如何解决tcp短连接 ...
- setColorFilter()滤镜
----------转载于:http://blog.sina.com.cn/s/blog_5da93c8f01012pkj.html 通过setColorFilter可以实现滤镜效果. 如: ...
- gcc -fvisibility=hidden,-fPIC选项
1. http://www.tuicool.com/articles/fy6Z3aQ 2. http://www.ibm.com/developerworks/cn/linux/l-cn-sdlsta ...
- python核心编程第六章练习6-9
6-9.转换.为练习5-13写一个姊妹函数,接受分钟数,返回小时数和分钟数.总时间不变,并且要求小时尽可能大.[答案]代码如下: #!/usr/bin/env python # translate m ...
- win10窗口设置眼睛保护色
经常电脑前坐着,习惯了豆沙色窗口(据说保护眼睛): 目标 记事本,ide,office等窗口颜色豆沙色:如下图 步骤 打开注册表:win+r 运行"regedit": 依次打开[H ...
- windows docker安装方式的比较小结
稍微小结一下使用InstallDocker 和dockertoolbox的两种方式安装的docker(名称说明可能不妥,仅代表安装方式) InstallDocker 使用的是Microsoft Hy ...
- td元素
一.设置td的宽和高,不设置table的宽和高 1. 当td的值为具体数值时 a. td显示的宽按设置的数值变动,但宽度不会大于父元素:若强行将宽设置的大于父元素的宽,会被系统无视,即最大宽度为父元素 ...
- eclipse 本地项目提交到远程库以及从远程库中添加项目 ---git
本地项目提交到远程库 1.右击项目->team->share project 2.选择本地库 从远处库中的项目拉到本地 1.右击项目->import项目
- 10月9日Android学习笔记:活动与服务之间的通信
最近在照着<第一行代码>这本书来学安卓,顺便记下笔记.主要的内容是Android中服务的第二种启动方式,通过活动绑定服务来启动服务,实现活动与服务之间的通信. 一. 首先创建一个服务类 p ...
- terminator终端工具
terminator是个很好的终端程序,在Ubuntu Linux下安装如下: sudo apt-get install terminator 可在同一屏打开多个窗口: