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 垃圾回收是一个术语,在计算机编程中用于描述查找和删除那些不再被其他对象引用的对象的处理过程. 换句话说,垃圾回收是删除任何其他对象未使用的对象的过程. 垃圾收 ...
随机推荐
- zoj3228Searching the String(ac自动机)
链接 这个题把病毒分为了两种,一种包含可以覆盖,另一种不可以,需要分别求出包含他们的个数,可以把两种都建在一颗tire树上,在最后求得时候判断一下当前节点是属于哪种字符串,如果是不包含的需要判断一下p ...
- FTPS链接服务器
一.首先登录ftp服务器:命令行 lftp user@ip 上传:put 下载: get 二. 关于FTP传输模式与传输的方式 众所周知,FTP传输有两种工作模式,Active Mode和Pass ...
- 使用新版Android Studio检测内存泄露和性能
内存泄露,是Android开发者最头疼的事.可能一处小小的内存泄露,都可能是毁于千里之堤的蚁穴. 怎么才能检测内存泄露呢?网上教程非常多,不过很多都是使用Eclipse检测的, 其实1.3版本以后的 ...
- python核心编程第六章练习6-12
6-12.字符串.(a)创建一个名字为findchr()的函数,函数声明如下.def findchr(string, char)findchr()要在字符串string中查找字符char,找到就返回该 ...
- no leveldbjni64-1.8 in java.library.path
在抽取以太坊Java版本的Trie树部分时,遇到了一个问题: Exception in thread "main" java.lang.UnsatisfiedLinkError: ...
- NGINX 定时器
写在前面 写NGINX系列的随笔,一来总结学到的东西,二来记录下疑惑的地方,在接下来的学习过程中去解决疑惑. 也希望同样对NGINX感兴趣的朋友能够解答我的疑惑,或者共同探讨研究. 整个NGINX系列 ...
- vue服务端渲染
这篇文章写得还蛮好https://segmentfault.com/a/1190000006701796 从官方网站下载了例子看,用es6写的,还好之前看过es6不然都看不懂,正好es6的东西一起熟悉 ...
- mousedown(function(){ return false; })作用
mousedown(function(){ return false;}); 阻止浏览器的默认行为. 比如a你加个空连接,可能会在当前页跳转, 你加了这句,就可以阻止a跳转,然后只执行js函数的代 ...
- ORM原型概念
ORM[Object-Relation-Mapping]对象关系映射. 这个名词已经出来好几年了.已经不陌生. 以前在项目中针对相对复杂业务逻辑时一般采用领域模型驱动方式进行业务概述,分析和建模. ...
- MATLAB 例子研究 Motion-Based Multiple Object Tracking
这个例子是用来识别视频中多个物体运动的.我要研究的是:搞清楚识别的步骤和相应的算法,识别出物体运动的轨迹. 详细参见官方帮助文档,总结如下: 移动物体的识别算法:a background subtra ...