feature: dyld && LD 链接器

This commit is contained in:
杭城小刘
2024-06-29 16:00:34 +08:00
parent 1a8659e143
commit 13f7457be9
367 changed files with 12893 additions and 3049 deletions

View File

@@ -1154,6 +1154,8 @@ MemoryStatus 机制会开启一个 memorystatus_jetsam_thread 的线程,它负
当监控线程发现某 App 有内存压力时,就发出通知,此时有内存的 App 就去执行 `didReceiveMemoryWarning` 代理方法。在这个时机,我们还有机会做一些内存资源释放的逻辑,也许会避免 App 被系统杀死。
**源码角度查看问题**
iOS 系统内核有一个数组,专门维护线程的优先级。数组的每一项是一个包含进程链表的结构体。结构体如下:
@@ -1785,7 +1787,7 @@ for (NSInteger index = 0; index < 10000000; index++) {
现象:
1. 在 viewDidLoad 也就是主线程中内存消耗过大,系统并不会发出低内存警告,直接 Crash。因为内存增长过快主线程很忙。
2. 多线程的情况下App 因内存增长过快会收到低内存警告AppDelegate 中的`applicationDidReceiveMemoryWarning` 先执行,随后是当前 VC 的 `didReceiveMemoryWarning`。
2. 多线程的情况下App 因内存增长过快会收到低内存警告AppDelegate 中的`applicationDidReceiveMemoryWarning` 先执行,随后是当前 VC 的 `didReceiveMemoryWarning`。也可以注册 `UIApplicationDidReceiveMemoryWarningNotification` 通知,此时获取一下内存情况一般就是 high Water 线。
结论:
@@ -2052,7 +2054,7 @@ for (NSInteger index = 0; index < 10000000; index++) {
其他的开发习惯就不一一描述了,良好的开发习惯和代码意识是需要平时注意修炼的。
### 7.
### 7. Memory Graph
在使用了一波业界优秀的的内存监控工具后发现了一些问题,比如 `MLeaksFinder`、`OOMDetector`、`FBRetainCycleDetector`等都有一些问题。比如 `MLeaksFinder` 因为单纯通过 VC 的 push、pop 等检测内存泄露的情况,会存在误报的情况。`FBRetainCycleDetector` 则因为对象深度优先遍历,会有一些性能问题,影响 App 性能。`OOMDetector` 因为没有合适的触发时机。
@@ -3699,56 +3701,44 @@ CFNetwork 使用 CFReadStreamRef 来传递数据,使用回调函数的形式
@implementation NetworkDelegateProxy
#pragma mark - life cycle
```
- (instancetype)sharedInstance {
static NetworkDelegateProxy *_sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [NetworkDelegateProxy alloc];
- (instancetype)sharedInstance {
static NetworkDelegateProxy *_sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [NetworkDelegateProxy alloc];
});
return _sharedInstance;
return _sharedInstance;
}
#pragma mark - public Method
- (instancetype)setProxyForObject:(id)originalTarget withNewDelegate:(id)newDelegate {
NetworkDelegateProxy *instance = [NetworkDelegateProxy sharedInstance];
instance->_originalTarget = originalTarget;
instance->_NewDelegate = newDelegate;
return instance;
}
- (void)forwardInvocation:(NSInvocation *)invocation {
if ([_originalTarget respondsToSelector:invocation.selector]) {
[invocation invokeWithTarget:_originalTarget];
[((NSURLSessionAndConnectionImplementor *)_NewDelegate) invoke:invocation];
}
- (instancetype)setProxyForObject:(id)originalTarget withNewDelegate:(id)newDelegate {
NetworkDelegateProxy *instance = [NetworkDelegateProxy sharedInstance];
instance->_originalTarget = originalTarget;
instance->_NewDelegate = newDelegate;
return instance;
}
- (nullable NSMethodSignature *)methodSignatureForSelector:(SEL)sel{
return [_originalTarget methodSignatureForSelector:sel];
- (void)forwardInvocation:(NSInvocation *)invocation {
if ([_originalTarget respondsToSelector:invocation.selector]) {
[invocation invokeWithTarget:_originalTarget];
[((NSURLSessionAndConnectionImplementor *)_NewDelegate) invoke:invocation];
}
}
- (nullable NSMethodSignature *)methodSignatureForSelector:(SEL)sel{
return [_originalTarget methodSignatureForSelector:sel];
}
@end
```
```
- 创建一个对象,实现 NSURLConnection、NSURLSession、NSIuputStream 代理方法
```objective-c
// NetworkImplementor.m
#pragma mark-NSURLConnectionDelegate
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
- (void)connection:(NSURLConnection *)connection didFailWithErrorbo:(NSError *)error {
NSLog(@"%s", __func__);
}