Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the principle of JSBinding + SharpKit memory management and garbage collection

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to analyze the principles of JSBinding + SharpKit memory management and garbage collection. I think it is very practical, so I share it with you. I hope you can get something after reading this article.

Both C # and JS have garbage collection mechanisms, which need to ensure that they can work together.

Class object

A class is a reference type in C #. We maintain 2 map in C # to keep the one-to-one correspondence between C # object and JS object.

As an example, take a look at the following code

1 / / new List 2 List lst = new List (); 3 4 / / JS5 var lst = new System.Collections.Generic.List$1.ctor (System.Int32.ctor)

Returning an object from C # to JS and the subsequent process is as follows:

1) C # gets an ID that represents a JS object.

1 / / File: System_Collections_Generic_List77.javascript2 _ jstype.definition.ctor = function (t0) {CS.Call (5,249,0, true, this, t0.getNativeType ();} / / 1) after the this here is passed to C #, C # gets an object ID

2) C # creates a List csObj object.

3) keep the one-to-one correspondence of two kinds of objects.

1 / / Storage correspondence, pseudocode, see JSMgr.addJSCSRel function for details! 2 map1 [jsObjID] = csObj;3 map2 [csObj.hashCode] = jsObjID

Note that since we have saved the csObj object, we must remove it from the map at some point, or the object will never be recycled by C # GC.

4) register the finalizer callback function for the JS object, and notify us of the callback when it is reclaimed by JS GC.

What we do in the callback is to remove the reference to csObj from the map so that we don't take the initiative to hold the csObj and it will be recycled at some point.

1 / / File: System_Collections_Generic_List77Generated.cs 2 3 static bool ListA1_ListA11 (JSVCall vc, int argc) 4 {5 int _ this = JSApi.getObject ((int) JSApi.GetType.Arg); / 1) get the JS object ID 6 JSApi.attachFinalizerObject (_ this); / / 4) register the finalizer function for the JS object, and notify our callback 7-argc when he is garbage collected 8 9 ConstructorInfo constructor = JSDataExchangeMgr.makeGenericConstructor (typeof (System.Collections.Generic.List), constructorID0); 10 if (constructor = = null) return true;11 12 int len = argc-1 politics 13 if (len = = 0) 14 {15 JSMgr.addJSCSRel (_ this, constructor.Invoke (null, new object [] {})), 3) create a C# object and save the one-to-one correspondence 16} 17 18 return true 19}

5) at any time, when you want to return an object from C # to JS, it will look up whether there is already a corresponding JS object. If so, return to the one that has been saved. If not, a corresponding JS object is created and returned to JS.

Summary: for class objects, we store the one-to-one correspondence between JS objects and CS objects. JS objects are still fully managed by JS GC, and what should be recycled will be recycled whenever. For the CS object, by registering the garbage collection event with the JS object, we will be notified when the JS object is collected, and then we can actively remove the reference to the CS object.

Structural object

Structures are of value type in C #. It is impossible to store one-to-one correspondence. When the structure object is returned to JS from C #, one object is recreated each time and returned to JS. The corresponding C # object can only be found through the JS object, and vice versa.

The storage relationship becomes:

1 / / Storage correspondence, pseudocode, see JSMgr.addJSCSRel function for details! 2 map1 [jsObjID] = csObj;3 / / map2 [csObj.hashCode] = jsObjID; / / there is no such thing!

Garbage collection controls are the same as class objects.

Note: Vector2,Vector3 is currently written once in JS in the code (it will be added later). They have nothing to do with the storage method of the structure object mentioned above.

Whenever from JS- > CS, a C# object is constructed based on the xyz value of the JS object to use.

Each time from CS- > JS, a JS object is constructed based on the xyz value of the CS object.

Is it difficult to understand? Imagine how int JS- > CS,CS- > JS? At present, the transmission of V2, V3 and int is similar, and each has its own data structure on JS and CS).

Is it still hard to understand? Ask in the group, or ask me)

Storage of Pure JS objects in C #

The storage of class objects and structure objects mentioned above has one thing in common: both JS and CS know what type of object to store. For example, the List,GameObject,2 side knows these two classes well and can deal with their objects.

A pure JS object means that only JS has a class definition, which C # is not aware of.

1 / / JS code 2 3 JsTypes.push ({4 fullname: "DebugMessages.Message", 5 Kind: "Class", 6 definition: {7 ctor: function (txt) {8 this.txt = txt; 9} 10} 11}); 12 13 var l = new System.Collections.Generic.List$1.ctor (DebugMessages.Message.ctor); 14 l.Add (new DebugMessages.Message ("hello") 15 l.Add (new DebugMessages.Message ("world")

Line13, when you create a List,C# side construct List, you will find that the type name is "DebugMessage.Message". According to this name, no System.Type can be found (see the JSDataExchange.GetTypeByName function).

What should I do? Whether it is possible to use List,int to represent the JS object ID directly when C # cannot find the type. Isn't that nice?

Yes, it was done in the first place. Take a look at Line14,new, a JS object and pass it to Centrum Magazine C # to store his ID. It looks great.

But! Yes! The JS object will not be referenced next, and if GC occurs, that object will be recycled. On the other hand, there is still an int stored in C#, so you can't find the JS object when you need it later.

The same problem exists when you set a JS function to C # Delegate.

So, when storing this JS object in C #, add a reference to the JS object!

I added a CSRepresentedObject class to do this.

(there is also a small detail: using the WeakReference class, you can take a look at the code, but it doesn't seem to work without him.)

1 public class CSRepresentedObject 2 {3 public static int s_objCount = 0; 4 public static int s_funCount = 0; 5 6 / / do not create this object directly, call JSDataExchangeMgr.getObject 7 public CSRepresentedObject (int jsObjID, bool bFunction = false) 8 {9 this.jsEngineRound = JSMgr.jsEngineRound;10 this.jsObjID = jsObjID;11 this.bFunction = bFunction;12 JSMgr.addJSCSRel (jsObjID, this, true) 13 14 if (bFunction) 15 sworn fungal counters 16 else 17 sworn objCounting counters 18 19 / / usually 1, 20 int refCount = JSApi.incRefCount (jsObjID) not added; 21 / / Debug.Log (new StringBuilder (). AppendFormat ("+ CSRepresentedObject {0} Ref [{1}] Fun [{1}]", jsObjID, refCount, bFunction? 1: 0)) 22} 23 ~ CSRepresentedObject () 24 {25 if (bFunction) 26 JSApi.decRefCount int refCount (jsObjID); 31 if (refCount)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report