简体   繁体   English

free()使用iOS Simulator,但不能在实际设备上使用

[英]free() working with iOS Simulator but not on real device

- (void)viewDidLoad
{
    [super viewDidLoad];
    int Byte = 128 * 1000;
    char *po[Byte];
    for (int i = 0; i < Byte; i++){
        po[i] = (char *)calloc(1024, sizeof(char));
        if (po[i] == NULL) {
            printf("can not calloc.");
        }
    }
    sleep(1);
    for (int j = 0; j < Byte; j++){
        free(po[j]);
    }
}

simulator retain memory then call to Free() and release memory as expected, But on real device, it is not release memory. 模拟器保留内存,然后调用Free()并按预期方式释放内存,但是在实际设备上,它不是释放内存。 it's retain 128MB. 保留128MB。 How do I do to release memory on real device? 如何在真实设备上释放内存?

The code you've posted looks fine but you may find that your method for detecting how much memory the process is using is faulty. 您发布的代码看起来不错,但是您可能会发现检测进程正在使用的内存量的方法有问题。

Typically, memory allocated from the operating system to the process is not returned to the operating system (until process exit), instead it's held by the process in a free pool in case you need it again, something like: 通常,从操作系统分配给进程的内存不会返回到操作系统(直到进程退出),而是由进程保存在空闲池中,以防万一您再次需要它,例如:

+----------------+
| process        |
|    (malloc)    |
|       ^        |
|       |        |
|       v        |
| +-----------+  |  +------------------+
| | free pool |<-+--| Operating system |
| +-----------+  |  +------------------+
+----------------+

So, if you're measuring the memory used by the process, it will rise by (about) 128M as you allocate the memory but it will not reduce when you free it. 因此,如果要测量该进程使用的内存,则在分配内存时它将增加(大约)128M,但释放时不会减少。

You could test this indirectly by simply allocating the memory again to see if the process space increases or stays at the levels reached the first time. 您可以通过简单地再次分配内存来查看进程空间是增加还是保持在首次达到的水平从而间接地测试这一点。

You could also add debug statements to the code to ensure calloc and free are being called the correct number of times, and with valid pointers in the case of the latter. 您还可以在代码中添加调试语句,以确保正确调用callocfree的次数,并在正确情况下使用有效的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Expo 应用程序在 Expo Go 和 iOS 模拟器上工作,但在真实设备上崩溃 - Expo app working on Expo Go and iOS Simulator but crashes on real device iOS 通用应用程序链接在模拟器上工作,而不是在真实设备上 - iOS universal app links working on simulator, not on real device Flutter iOS 应用程序在模拟器中完美运行,但在真实设备中无法运行 - Flutter iOS app working perfect in simulator but not in real device AVAudioPlayer在模拟器上工作但不在Real Device上工作 - AVAudioPlayer working on Simulator but not on Real Device 后台获取在模拟器中工作但在真实设备中不工作 - Background fetch working in simulator but not working in real device iOS模拟器崩溃而真实设备没有崩溃 - iOS Simulator crashes while real device does not Firebase与模拟器连接,但未在真实设备iOS中连接 - Firebase connecting with simulator but not in real device iOS 在真实设备中编译但在模拟器iOS 8中编译时出错 - Error compiling in real device but not simulator iOS 8 iOS应用可在模拟器上运行,但不能在真实设备上运行 - iOS app works on simulator but not on real device Tabbarcontroller在模拟器上工作而不在iOS设备上 - Tabbarcontroller is working in simulator not on iOS device
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM