[英]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. 您还可以在代码中添加调试语句,以确保正确调用
calloc
和free
的次数,并在正确情况下使用有效的指针。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.