iPhone的堆栈大小是固定的吗?

时间:2022-12-02 21:25:28

When I am trying to adjust of stack size of threads:

当我尝试调整线程的堆栈大小时:

- (void)testStack:(NSInteger)n {
    NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(dummy) object:nil];
    NSUInteger size = 4096 * n;
    [thread setStackSize:size];
    [thread start];
}

- (void)dummy {
    NSUInteger bytes = [[NSThread currentThread] stackSize];
    NSLog(@"%@", @(bytes));
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    for (NSInteger i = 126; i <= 130; i++) {
        [self testStack:i];
    }
    return YES;
}

in the output, the size is not changed:

在输出中,大小不会改变:

2015-06-19 11:05:06.912 Stack[52982:2082454] 524288
2015-06-19 11:05:06.913 Stack[52982:2082457] 524288
2015-06-19 11:05:06.913 Stack[52982:2082456] 524288
2015-06-19 11:05:06.913 Stack[52982:2082458] 524288
2015-06-19 11:05:06.913 Stack[52982:2082455] 524288

is the iPhone stack size fixed?

iPhone堆栈大小是固定的吗?

p.s. I am testing the above in iPhone 6 Plus, debug mode.

附:我正在测试iPhone 6 Plus中的上述调试模式。

UPDATE: the stack can be adjusted when running in the Simulator on MacBook:

更新:在MacBook上的模拟器中运行时可以调整堆栈:

2015-06-19 11:25:17.042 Stack[1418:427993] 528384
2015-06-19 11:25:17.042 Stack[1418:427994] 532480
2015-06-19 11:25:17.042 Stack[1418:427992] 524288
2015-06-19 11:25:17.042 Stack[1418:427991] 520192
2015-06-19 11:25:17.042 Stack[1418:427990] 516096

2 个解决方案

#1


3  

The stack size is bounded on the device, and in most cases cannot exceed 1MB for the main thread on iPhone OS, nor can it be shrunk.

堆栈大小在设备上有限,在大多数情况下,iPhone OS上的主线程不能超过1MB,也不能缩小。

The minimum allowed stack size for secondary threads is 16 KB and the stack size must be a multiple of 4 KB. The space for this memory is set aside in your process space at thread creation time, but the actual pages associated with that memory are not created until they are needed.

辅助线程允许的最小堆栈大小为16 KB,堆栈大小必须为4 KB的倍数。在创建线程时,您的进程空间中会留出此内存的空间,但在需要之前不会创建与该内存关联的实际页面。

#2


2  

Actually you can set it. I am not sure if this changed with iOS 10, but on iOS 10.2.1 this does work. The only limitation is that the stack size has to be a multiple of 4kb.

实际上你可以设置它。我不确定这是否会因iOS 10而改变,但在iOS 10.2.1上这确实有效。唯一的限制是堆栈大小必须是4kb的倍数。

pthread_attr_t tattr;
int ret = pthread_attr_init ( &tattr ) ;
size_t size;
ret = pthread_attr_getstacksize(&tattr, &size);
printf ( "Get: ret=%d,size=%zu\n" , ret , size ) ;

size = 4096 * 512 ;
ret = pthread_attr_setstacksize(&tattr, size);
int ret2 = pthread_attr_getstacksize(&tattr, &size);
printf ( "Set & Get: ret=%d ret2=%d,size=%zu\n" , ret , ret2 , size ) ;

#1


3  

The stack size is bounded on the device, and in most cases cannot exceed 1MB for the main thread on iPhone OS, nor can it be shrunk.

堆栈大小在设备上有限,在大多数情况下,iPhone OS上的主线程不能超过1MB,也不能缩小。

The minimum allowed stack size for secondary threads is 16 KB and the stack size must be a multiple of 4 KB. The space for this memory is set aside in your process space at thread creation time, but the actual pages associated with that memory are not created until they are needed.

辅助线程允许的最小堆栈大小为16 KB,堆栈大小必须为4 KB的倍数。在创建线程时,您的进程空间中会留出此内存的空间,但在需要之前不会创建与该内存关联的实际页面。

#2


2  

Actually you can set it. I am not sure if this changed with iOS 10, but on iOS 10.2.1 this does work. The only limitation is that the stack size has to be a multiple of 4kb.

实际上你可以设置它。我不确定这是否会因iOS 10而改变,但在iOS 10.2.1上这确实有效。唯一的限制是堆栈大小必须是4kb的倍数。

pthread_attr_t tattr;
int ret = pthread_attr_init ( &tattr ) ;
size_t size;
ret = pthread_attr_getstacksize(&tattr, &size);
printf ( "Get: ret=%d,size=%zu\n" , ret , size ) ;

size = 4096 * 512 ;
ret = pthread_attr_setstacksize(&tattr, size);
int ret2 = pthread_attr_getstacksize(&tattr, &size);
printf ( "Set & Get: ret=%d ret2=%d,size=%zu\n" , ret , ret2 , size ) ;