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. unittest框架介绍

    1.test fixture(测试框架) 测试准备前要做的工作和测试执行完成后要做的工作,例如测试前需要把数据初始化,测试完成后需要把测试环境中需要关的东西都关掉.主要包括setUp()和tearDo ...

  2. Gradle version 2.2 is required. Current version is 2.14.1.

    gradle版本错误: 1. 修改gradle\wrapper\gradle-wrapper.properties文件: distributionUrl=https\://services.gradl ...

  3. zabbix3.0.4 部署之四 (LNAP > PHP安装)

    1.安装依赖 安装epel-release源 安装 libiconv-1.14.tar.gz (这个还有个devl包)  libmcrypt-2.5.8.tar.gz   mhash-0.9.9.9. ...

  4. 利用jsoup进行模拟登录

    因为工作的原因,近段时间开始接触jsoup.大概也弄清了用java来爬网页是怎样一个过程.特此,写篇日志以便他日方便查看. Jsoup是一个java平台的能够对xml文档结构的文档进行解析.有点类似于 ...

  5. HTML中的target(_self,_blank)用法总结

    HTML中的target(_self,_blank)用法总结 最近一个项目,多次遇到target='_self', target='_blank'的用法, 再次总结一下: 1.<a>标签 ...

  6. C学习笔记(八)字符输入输出和输入确认

    缓冲区 缓冲区分为两类:完全缓冲(fully buffered)I/O和行缓冲(line-buffered)I/O.完全缓冲在缓冲区满时被清空(内容被发送至目的地).这种类型常出现在文件输入中.缓冲区 ...

  7. yii2-basic后台管理功能开发之二:创建CRUD增删改查

    昨天实现了后台模板的嵌套,今天我们可以试着创建CRUD模型啦 刚开始的应该都是“套用”,不再打算细说,只把关键的地方指出来. CRUD即数据库增删改查操作.可以理解为yii2为我们做了一个组件,来实现 ...

  8. Sqlserver2012 中文乱码解决

    1.在Windows Azure的数据库中,如果选择默认字符编码,那么在创建表字段是,字符串类型应该为nvarchar,如果是varchar将会出现乱码,同样的的在sql语句中生命变量,也是需要将字符 ...

  9. Google 黑客搜索技巧

    常用的google关键字: foo1 foo2 (也就是关联,比如搜索xx公司 xx美女) operatorfoo filetype123 类型 sitefoo.com 相对直接看网站更有意思,可以得 ...

  10. springmvc__SimpleUrlHandlerMapping(对访问地址进行加工,以键值对的形式)

    1.配置web.xml(这里配置url-pattern为/) <!-- 编码过滤器 --> <filter> <filter-name>characterEncod ...