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)的更多相关文章

  1. Java (JVM) Memory Model – Memory Management in Java

    原文地址:http://www.journaldev.com/2856/java-jvm-memory-model-memory-management-in-java Understanding JV ...

  2. Operating System Memory Management、Page Fault Exception、Cache Replacement Strategy Learning、LRU Algorithm

    目录 . 引言 . 页表 . 结构化内存管理 . 物理内存的管理 . SLAB分配器 . 处理器高速缓存和TLB控制 . 内存管理的概念 . 内存覆盖与内存交换 . 内存连续分配管理方式 . 内存非连 ...

  3. 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- ...

  4. 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 ...

  5. Java Memory Management(1)

    Java Memory Management, with its built-in garbage collection, is one of the language’s finest achiev ...

  6. 再谈.net的堆和栈---.NET Memory Management Basics

    .NET Memory Management Basics .NET memory management is designed so that the programmer is freed fro ...

  7. Lifetime-Based Memory Management for Distributed Data Processing Systems

    Lifetime-Based Memory Management for Distributed Data Processing Systems (Deca:Decompose and Analyze ...

  8. 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 ...

  9. js Memory Management

    js Memory Management 垃圾回收是一个术语,在计算机编程中用于描述查找和删除那些不再被其他对象引用的对象的处理过程. 换句话说,垃圾回收是删除任何其他对象未使用的对象的过程. 垃圾收 ...

随机推荐

  1. USACO2016Splitting the Field分割牧场

    Description FJ的N头奶牛分别位于他二维的牧场的不同位置.FJ想用一个矩形栅栏围住这些牛(牛可以在栅栏边上),并使这个栅栏尽可能小.这个栅栏的边与x轴或y轴平行.不幸的是,FJ上个季度的牛 ...

  2. 谷歌 火狐 CSS兼容

    @media screen and (-webkit-min-device-pixel-ratio:0){}谷歌@-moz-document url-prefix(){}火狐

  3. mongodb unclean shutdown 修复方法

    启动mongodb时,提示Unclean shutdown detected mongodb,解决方法很简单 mongod --repair --dbpath D:\MongoDB\data\db

  4. 第八章 企业项目开发--分布式缓存memcached

    注意:本节代码基于<第七章 企业项目开发--本地缓存guava cache> 1.本地缓存的问题 本地缓存速度一开始高于分布式缓存,但是随着其缓存数量的增加,所占内存越来越大,系统运行内存 ...

  5. guava学习--File1

    ByteSource:表示一个可读的字节.通常情况下,我们期望的字节来源是一个文件,但它也可以从一个字节数组读取字节. File f1 = new File("D:\\test2.txt&q ...

  6. C# 技巧(1) C# 转换时间戳

    经常发现很多地方使用一个时间戳表示时间.比如: 1370838759  表示 2013年6月10日 12:32:39. 我们就需要一个工具,方便地转换这种时间格式 什么是时间戳? 时间戳, 又叫Uni ...

  7. jquery统计页面的pv/ip及停留时间等

    我们在做网站的时候经常需要统计网站的访问信息,这里介绍一个用jquery写的一个统计方法 新建一个js文件jun_record.js 代码如下: var start; var end; var tim ...

  8. iOS开发Swift篇—(六)流程控制

    iOS开发Swift篇—(六)流程控制 一.swift中的流程控制 Swift支持的流程结构如下: 循环结构:for.for-in.while.do-while 选择结构:if.switch 注意:这 ...

  9. iTunes Affiliate Resources

    https://affiliate.itunes.apple.com/resources/documentation/itunes-store-web-service-search-api/

  10. 二模 (13)day2

    第一题: 题目大意: 给出一个N*M的矩阵,定义一条路径的权值为经过的所有点权值的最大值.求一条从第一行到第N行的路径,使得路径权值最小. N,M<=1000 矩阵内点的权值小于1000. 解题 ...