for循环内存不足 [英] running out of memory from for loop

查看:255
本文介绍了for循环内存不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,所以我认为,如果我制作了一个会生成随机密码的小应用程序,然后让该应用程序运行所有可能的功能,然后尝试查看密码告诉我它尝试了多少次,那会很酷.该应用有时有时会崩溃,具体取决于密码是什么.我想知道如果要使用最多的内存来阻止崩溃,我可以做些什么.这就是方法.

Hey so i thought it would be cool if i made a little app that generated a random password and then had the app run through all the possibilities and try to see what the password was tell me how many times it attempted it. Sometime the app works sometimes it crashes depends on what the password is. I wanted to know f there was anything i could do to stop it from crashing by using up to much memory. This is the method.

-(void)hackString 
 {
    NSString *string;
    NSString *string1;
    NSString *string2;
    NSString *string3;
    NSString *string4;
    NSString *string5;

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void){


    NSString *hackedString;

    for (string in self.validLetters) {
    //[hackedString appendString:string];
        for (string1 in self.validLetters) {
            //[hackedString appendString:string1];
            for (string2 in self.validLetters) {
                //[hackedString appendString:string1];
                for (string3 in self.validLetters) {
                    //[hackedString appendString:string1];
                    for (string4 in self.validLetters) {
                        for (string5 in self.validLetters) {
                             hackedString = [NSString stringWithFormat:@"%@%@%@%@%@%@",string,string1,string2,string3,string4,string5];
                           // NSLog(@"%@",hackedString);
                            [self testStringWithPassword:hackedString];

                        }

                    }
                }
            }
        }
    }
         });

} 

我应该在那里做些什么来阻止它崩溃吗?

Is there anything I should be doing there to stop it from crashing?

推荐答案

我应该在那里做些什么来阻止它崩溃吗?

Is there anything I should be doing there to stop it from crashing?

您确定程序崩溃了吗?如果您的程序确实崩溃了,您应该会收到崩溃日志或控制台消息,告诉您崩溃的位置,并且您应该能够在调试器中看到崩溃的位置.

Are you sure the program is crashing? If your program is really crashing, you should be getting a crash log or console message that tells you where it crashed, and you should be able to see the place where it crashed in the debugger.

可能是因为您要让程序从validLetters生成六个字符串的每个组合,所以程序只花了很长时间才能运行.根据情况,您的程序也有可能被操作系统中的监视程序杀死.

It may be that your program is simply taking a very long time to run because you're asking it to generate every combination of six strings from validLetters. Depending on the circumstances, it's also possible that your program is being killed by a watchdog process in the operating system.

另一方面,您的程序可能真的崩溃了,因为它的内存不足.每次调用-stringWithFormat:都会分配内存,这将创建一个新的自动释放的字符串.由于您永远不会让运行循环运行,因此自动释放池永远不会耗尽.如果您的程序花很长时间找不到所需的字符串,则很容易用完所有可用的内存.因此,首先,请尝试将最里面的for循环放入@autorelease块中,例如:

On the other hand, your program really might be crashing because it's running out of memory. You're allocating memory every time you call -stringWithFormat:, which creates a new autoreleased string. Since you're never letting the run loop run, the autorelease pool never gets drained. If your program takes too long to find the string that it's looking for, you could easily be using up all your available memory. So, to start off, try putting your innermost for loop inside an @autorelease block, like:

@autorelease {
    for (string5 in self.validLetters) {
        hackedString = [NSString     stringWithFormat:@"%@%@%@%@%@%@",string,string1,string2,string3,string4,string5];
                           // NSLog(@"%@",hackedString);
        [self testStringWithPassword:hackedString];

    }
}

这篇关于for循环内存不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆