Refrence:
Memory management
1• Objective-C uses ARC(OC 使用ARC机制)
2• JavaScriptCore uses garbage collection (JS 使用垃圾回收机制)
■ All references are strong (JS中全部都是“强引用”)
3• API memory management is mostly automatic
4• Two situations that require extra attention: (几乎SDK已经做好了很多事情,所以开发者只需要重点掌握以下亮点)
■ Storing JavaScript values in Objective-C objects
■ Adding JavaScript fields to Objective-C objects
(英文文档只要被翻译了,就难免失去原有的含义,尽量不翻译~)
根据官方文档关于JS-OC内存管理总结:由于JS中全部都是强引用,如果JS 与 OC互相引用时,就要防止OC也强引用JS,这样会形成引用循环,所以OC要想办法弱引用,但弱引用会被系统释放,所以把可能被释放的对象放到一个容器中来防止对象被被错误释放。
看代码(一):
JS:
function ClickHandler(button, callback) {
this.button = button;
this.button.onClickHandler = this;
this.handleEvent = callback;
};
OC:
@implementation MyButton
- (void)setOnClickHandler:(JSValue *)handler
{
_onClickHandler = handler; // Retain cycle
}
@end
如果直接保存 handler,就会出现内存泄露,因为 JS 中引用 button 对象是强引用,如果 Button 也用强引用来保存 JS 中的 handler,这就导致了 循环引用。我们没法改变 JavaScript 中的强引用机制,只能在 Objective-C 中弱引用 handler,为了防止 onclick handler 被错误释放, JavaScriptCore 给出的解决方案如下:
- (void)setOnClickHandler:(JSValue *)handler
{
_onClickHandler = [JSManagedValue managedValueWithValue:handler];
[_context.virtualMachine addManagedReference:_onClickHandler
withOwner:self]
}
代码(二):
- (void)loadColorsPlugin
{
// Load the plugin script from the bundle.
NSString *path = [[NSBundlemainBundle]pathForResource:@"colors"ofType:@"js"];
NSString *pluginScript = [NSStringstringWithContentsOfFile:pathencoding:NSUTF8StringEncodingerror:nil];
_context = [[JSContextalloc]init];
// We insert the AppDelegate into the global object so that when we call
// -addManagedReference:withOwner: for the plugin object we're about to load
// and pass the AppDelegate as the owner, the AppDelegate itself is reachable from
// within JavaScript. If we didn't do this, the AppDelegate wouldn't be reachable
// from JavaScript, and there wouldn't be anything keeping the plugin object alive.
_context[@"AppDelegate"] =self;
// Insert a block so that the plugin can create NSColors to return to us later.
_context[@"makeNSColor"] = ^(NSDictionary *rgb){
return [NSColorcolorWithRed:[rgb[@"red"]floatValue] / 255.0f
green:[rgb[@"green"]floatValue] /255.0f
blue:[rgb[@"blue"]floatValue] /255.0f
alpha:1.0f];
};
JSValue *plugin = [_contextevaluateScript:pluginScript];
_colorPlugin = [JSManagedValuemanagedValueWithValue:plugin];
[_context.virtualMachineaddManagedReference:_colorPluginwithOwner:self];
[self.windowsetDelegate:self];
}
JSManagedValue:
The primary use case for JSManagedValue is for safely referencing JSValues
from the Objective-C heap. It is incorrect to store a JSValue into an
Objective-C heap object, as this can very easily create a reference cycle,
keeping the entire JSContext alive.
(将 JSValue 转为 JSManagedValue 类型后,可以添加到 JSVirtualMachine 对象中,这样能够保证你在使用过程中 JSValue 对象不会被释放掉,当你不再需要该 JSValue 对象后,从 JSVirtualMachine 中移除该 JSManagedValue 对象,JSValue 对象就会被释放并置空。)
JSVirtualMachine:
All instances of JSContext are associated with a single JSVirtualMachine. The
virtual machine provides an "object space" or set of execution resources.(JSVirtualMachine就是一个用于保存弱引用对象的数组,加入该数组的弱引用对象因为会被该数组 retain,所以保证了使用时不会被释放,当数组里的对象不再需要时,就从数组中移除,没有了引用的对象就会被系统释放。)
Threading
API is thread safe
• Locking granularity is JSVirtualMachine
■ Use separate JSVirtualMachines for concurrency/parallelism
JavaScriptCore C API
JSValue ↔ JSValueRef :
JSValueRef valueRef = XXX;
JSValue *value = [JSValue valueWithJSValueRef:valueRef inContext:context];
JSValue *value = XXX;
JSValueRef valueRef = [value JSValueRef];
JSContext ↔ JSGlobalContextRef :
JSGlobalContextRef ctx = XXX;
JSContext *context = [JSContext contextWithJSGlobalContextRef:ctx];
JSContext *context = XXX;
JSGlobalContextRef ctx = [context JSGlobalContextRef];
使用Block的注意事项
从之前的例子和介绍应该有体会到Block在JavaScriptCore中起到的强大作用,它在JavaScript和Objective-C之间的转换 建立起更多的桥梁,让互通更方便。但是要注意的是无论是把Block传给JSContext
对象让其变成JavaScript方法,还是把它赋给exceptionHandler
属性,在Block内都不要直接使用其外部定义的JSContext
对象或者JSValue
,应该将其当做参数传入到Block中,或者通过JSContext
的类方法+ (JSContext *)currentContext;
来获得。否则会造成循环引用使得内存无法被正确释放。
比如上边自定义异常处理方法,就是赋给传入JSContext
对象con
,而不是其外创建的context
对象,虽然它们其实是同一个对象。这是因为Block会对内部使用的在外部定义创建的对象做强引用,而JSContext
也会对被赋予的Block做强引用,这样它们之间就形成了循环引用(Circular Reference)使得内存无法正常释放。
对于JSValue
也不能直接从外部引用到Block中,因为每个JSValue
上都有JSContext
的引用 (@property(readonly, retain) JSContext *context;
),JSContext
再引用Block同样也会形成引用循环。
Garbage Collection
虽然Objetive-C和JavaScript都是面向对象的语言,而且它们都可以让程序员专心于业务逻辑,不用担心内存回收的问题。但是两者的内存回首机制全是不同的,Objective-C是基于引用计数,之后Xcode编译器又支持了自动引用计数(ARC, Automatic Reference Counting);JavaScript则如同Java/C#那样用的是垃圾回收机制(GC, Garbage Collection)。当两种不同的内存回收机制在同一个程序中被使用时就难免会产生冲突。
比如,在一个方法中创建了一个临时的Objective-C对象,然后将其加入到JSContext
放在JavaScript中的变量中被使用。因为JavaScript中的变量有引用所以不会被释放回收,但是Objective-C上的对象可能在方法调用结束后,引用计数变0而被回收内存,因此JavaScript层面也会造成错误访问。
同样的,如果用JSContext
创建了对象或者数组,返回JSValue
到Objective-C,即使把JSValue
变量retain
下,但可能因为JavaScript中因为变量没有了引用而被释放内存,那么对应的JSValue
也没有用了。
怎么在两种内存回收机制中处理好对象内存就成了问题。JavaScriptCore提供了JSManagedValue
类型帮助开发人员更好地管理对象内存。
// Convenience method for creating JSManagedValues from JSValues.
+ (JSManagedValue *)managedValueWithValue:(JSValue *)value;
// Create a JSManagedValue.
- (id)initWithValue:(JSValue *)value;
// Get the JSValue to which this JSManagedValue refers. If the JavaScript value has been collected,
// this method returns nil.
- (JSValue *)value;
@end
在《iOS7新JavaScriptCore框架入门介绍》有提到JSVirtualMachine
为整个JavaScriptCore的执行提供资源,所以当将一个JSValue
转成JSManagedValue
后,就可以添加到JSVirtualMachine
中,这样在运行期间就可以保证在Objective-C和JavaScript两侧都可以正确访问对象而不会造成不必要的麻烦。
@interface JSVirtualMachine : NSObject
// Create a new JSVirtualMachine.
- (id)init;
// addManagedReference:withOwner and removeManagedReference:withOwner allow
// clients of JSVirtualMachine to make the JavaScript runtime aware of
// arbitrary external Objective-C object graphs. The runtime can then use
// this information to retain any JavaScript values that are referenced
// from somewhere in said object graph.
//
// For correct behavior clients must make their external object graphs
// reachable from within the JavaScript runtime. If an Objective-C object is
// reachable from within the JavaScript runtime, all managed references
// transitively reachable from it as recorded with
// addManagedReference:withOwner: will be scanned by the garbage collector.
//
- (void)addManagedReference:(id)object withOwner:(id)owner;
- (void)removeManagedReference:(id)object withOwner:(id)owner;
@end