需要有关消耗 RAM 的循环的帮助
我试图让这个循环释放它使用的所有内存,但它仍然很快消耗 RAM。希望有人能告诉我如何让字符串按应有的方式解除分配。基本上,循环将双精度数据写入文本文件,有时必须写入几兆字节。
for (int i = 0; i < number_of_samples; i++)
{
if (print_str == nil)
{
print_str = [[NSMutableString alloc] init];
}
NSString* add_str = [[NSString alloc] initWithFormat:@"\n%0.06f", number_of_seconds/number_of_samples*i];
[print_str appendString:add_str];
[add_str release];
for (int g = 0; g < number_of_channels; g++)
{
add_str = [[NSString alloc] initWithFormat:@"\t%f", data_buffer[g + i*number_of_channels]];
[print_str appendString:add_str];
[add_str release];
}
if (i % 100 == 0 && i != 0)
{
fprintf(c_file_handle, "%s", [print_str UTF8String]);
[print_str release];
print_str = nil;
}
}
I'm trying to get this loop to free all of the memory it uses, but it still gobbles up RAM pretty quickly. Hopefully someone can tell me how I can get the strings deallocating as they should. Basically, the loop writes doubles to a text file, and sometimes it has to write several megabytes of them.
for (int i = 0; i < number_of_samples; i++)
{
if (print_str == nil)
{
print_str = [[NSMutableString alloc] init];
}
NSString* add_str = [[NSString alloc] initWithFormat:@"\n%0.06f", number_of_seconds/number_of_samples*i];
[print_str appendString:add_str];
[add_str release];
for (int g = 0; g < number_of_channels; g++)
{
add_str = [[NSString alloc] initWithFormat:@"\t%f", data_buffer[g + i*number_of_channels]];
[print_str appendString:add_str];
[add_str release];
}
if (i % 100 == 0 && i != 0)
{
fprintf(c_file_handle, "%s", [print_str UTF8String]);
[print_str release];
print_str = nil;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以对字符串做的唯一事情是不使用 print_str 因为它仍然保存所有字符串并占用内存。使用fopen中的"a"选项将数据追加到文件中,将格式化字符串add_str追加到文件中,而不是添加到内存中缓冲。
The only thing you can do with the string is not using the print_str cause it still holds all the strings and occupies memory. Use "a" option in fopen to append data to the file and append the formatted string add_str to the file instead of adding it to the memory buffer.
因为您基本上使用该一个字符串作为临时缓冲区,所以分配一次并根据需要多次 initWithFormatting 怎么样?然后在循环完成后释放它。
Because you're basically using that one string as a temporary buffer, how about allocating it once, and initWithFormatting it as often as you want? Then release it when you're done with the loop.
您可以尝试使用 NSOutputStream,而不是创建字符串并使用 fprintf。您的代码可能如下所示:
这是一个粗略的草图 - 请参阅 NSStream 和 NSOutputStream 了解详细信息。
Instead of creating strings and using
fprintf
, you might try usingNSOutputStream
. Your code might look something like this:This is a rough sketch - see the documentation for NSStream and NSOutputStream for details.
您可以使用
[print_strappendFormat:]
而不是每次分配和释放新字符串。在紧密循环中运行时,有时您需要创建一个
NSAutoRelease
池并定期耗尽它并创建一个新池,但我没有看到您使用任何自动释放对象。您是否能够在使用时
释放
data_buffer
或者稍后再次使用它?free(data_buffer[g + i*number_of_channels]);
You could use
[print_str appendFormat:]
instead of allocating and releasing a new string each time.When running in tight loops sometimes you need to create an
NSAutoRelease
pool and periodically drain it and create a new one but I do not see you using any autoreleased objects.Are you able to
free
thedata_buffer
as you go or do you use it again later?free(data_buffer[g + i*number_of_channels]);