需要有关消耗 RAM 的循环的帮助

发布于 2024-11-30 12:11:11 字数 836 浏览 0 评论 0原文

我试图让这个循环释放它使用的所有内存,但它仍然很快消耗 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

若相惜即相离 2024-12-07 12:11:11

您可以对字符串做的唯一事情是不使用 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.

叹倦 2024-12-07 12:11:11

因为您基本上使用该一个字符串作为临时缓冲区,所以分配一次并根据需要多次 initWithFormatting 怎么样?然后在循环完成后释放它。

NSString * add_str = [NSString alloc];

loop... {
    NSString* add_str = [ add_str initWithFormat:@"\n%0.06f", number_of_seconds/number_of_samples*i];
    [print_str appendString:add_str];
}
// end of loop

[add_str release];

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.

NSString * add_str = [NSString alloc];

loop... {
    NSString* add_str = [ add_str initWithFormat:@"\n%0.06f", number_of_seconds/number_of_samples*i];
    [print_str appendString:add_str];
}
// end of loop

[add_str release];
jJeQQOZ5 2024-12-07 12:11:11

您可以尝试使用 NSOutputStream,而不是创建字符串并使用 fprintf。您的代码可能如下所示:

NSOutputStream *os = [NSOutputStream outputStreamToFileAtPath:@"/path/to/file.ext"
                                                       append:YES];
[os open];
for (int i = 0; i < number_of_samples; i++) {
    // create a buffer to store the data to write
    [os write:buffer maxLength:bufferLength];
    // release the buffer
}
[os close];

这是一个粗略的草图 - 请参阅 NSStreamNSOutputStream 了解详细信息。

Instead of creating strings and using fprintf, you might try using NSOutputStream. Your code might look something like this:

NSOutputStream *os = [NSOutputStream outputStreamToFileAtPath:@"/path/to/file.ext"
                                                       append:YES];
[os open];
for (int i = 0; i < number_of_samples; i++) {
    // create a buffer to store the data to write
    [os write:buffer maxLength:bufferLength];
    // release the buffer
}
[os close];

This is a rough sketch - see the documentation for NSStream and NSOutputStream for details.

酒绊 2024-12-07 12:11:11
  1. 您可以使用 [print_strappendFormat:] 而不是每次分配和释放新字符串。

  2. 在紧密循环中运行时,有时您需要创建一个 NSAutoRelease 池并定期耗尽它并创建一个新池,但我没有看到您使用任何自动释放对象。

  3. 您是否能够在使用时释放data_buffer或者稍后再次使用它? free(data_buffer[g + i*number_of_channels]);

  1. You could use [print_str appendFormat:] instead of allocating and releasing a new string each time.

  2. 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.

  3. Are you able to free the data_buffer as you go or do you use it again later? free(data_buffer[g + i*number_of_channels]);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文