iPhone - 程序接收信号:0 - 在设备上但不在模拟器上(内存管理问题)

时间:2012-08-21 11:24:21

标签: objective-c ios out-of-memory bulkinsert

我正在经历一个严格的基于内存的问题,同时迭代在SQLite数据库中执行批量插入的循环。

我很确定这是关注内存的,我无法指出导致问题的变量。 我已经发布了所有已分配的内容。

通过仪器分析此时没有内存泄漏。代码片段如下:

int m=0;

while ([arrData count]!=0) {

if (newQuery) {

    exeQuery = query;
    newQuery = false;

}
else 
{
    exeQuery=[exeQuery stringByAppendingFormat:@" UNION"];
}

NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithDictionary:[arrData objectAtIndex:0]];

[data removeObjectForKey:@"statement"];
[data removeObjectForKey:@"datetime_stamp"];

NSString *strVal=@"";
NSString *_value=@"";

int i=0;
    for(;i<[keys  count]-1;i++)
    {
        if(_value==nil || _value==NULL){
            _value=@"";
        }

        _value=[NSString stringWithFormat:@"%@",[data objectForKey:[keys objectAtIndex:i]]];
        _value=[_value stringByReplacingOccurrencesOfString:@"'" withString:@"''"];

        strVal=[strVal stringByAppendingFormat:@"'%@',",_value];

    }

if(_value==nil || _value==NULL){
    _value=@"";
}

_value=[NSString stringWithFormat:@"%@",[data objectForKey:[keys objectAtIndex:i]]];
_value=[_value stringByReplacingOccurrencesOfString:@"'" withString:@"''"];

strVal=[strVal stringByAppendingFormat:@"'%@'",_value];


exeQuery=[exeQuery stringByAppendingFormat:@" SELECT %@",strVal];


if (m!=0 && m%499==0) {

    [DataSource executeQuery:exeQuery];

    //db.execute(exeQuery);
    newQuery = true;
}

[data release];

[arrData removeObjectAtIndex:0];

m++;
}

for (;m<locations_length; m++) {

if (newQuery) {

    exeQuery = query;
    newQuery = false;

}
else 
{
    exeQuery=[exeQuery stringByAppendingFormat:@" UNION"];
}

NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithDictionary:[arrData objectAtIndex:m]];

[data removeObjectForKey:@"statement"];
[data removeObjectForKey:@"datetime_stamp"];

NSString *strVal=@"";
NSString *_value=@"";

int i=0;
    for(;i<[keys  count]-1;i++)
    {
        if(_value==nil || _value==NULL){
            _value=@"";
        }

        _value=[NSString stringWithFormat:@"%@",[data objectForKey:[keys objectAtIndex:i]]];
        _value=[_value stringByReplacingOccurrencesOfString:@"'" withString:@"''"];

        strVal=[strVal stringByAppendingFormat:@"'%@',",_value];

    }

if(_value==nil || _value==NULL){
    _value=@"";
}

_value=[NSString stringWithFormat:@"%@",[data objectForKey:[keys objectAtIndex:i]]];
_value=[_value stringByReplacingOccurrencesOfString:@"'" withString:@"''"];

strVal=[strVal stringByAppendingFormat:@"'%@'",_value];


exeQuery=[exeQuery stringByAppendingFormat:@" SELECT %@",strVal];


if (m!=0 && m%499==0) {

    [DataSource executeQuery:exeQuery];

    //db.execute(exeQuery);
    newQuery = true;
}

[data release];
}

非常感谢任何帮助。 Plz联手......!

提前致谢。

3 个答案:

答案 0 :(得分:1)

在不知道[keys count]中你经历了多少次迭代的情况下,你正在进行大量的NSString操作。每个“stringBy ...”消息将分配一个新的自动释放的NSString实例,在您退出循环之前不会释放该实例。您可能需要考虑使用NSMutableString并使用

replaceOccurrencesOfString:withString:options:range:

减少正在创建的自动释放对象的数量(这将减少应用程序的内存占用量)。请参阅NSMutableString documentation。否则,我建议使用分配工具或Xcode中的类似工具来仔细检查您的内存占用

答案 1 :(得分:1)

很明显,我们使用的字符串在每个实例中都会变异,因此nsmutablestring是优化的解决方案。同时在分析函数时:

[NSMutableString replaceOccurrencesOfString:@"'" withString:@"''"]
[NSMutableString  appendString:]

在乐器中,他们留下较少的记忆足迹,同时将它们与之前使用过的足迹进行比较。

答案 2 :(得分:0)

对于使用Sqlite或Core Data在数据库中批量插入记录,总是更喜欢在@autoreleasepool {}中使用循环,因为这样,自动释放模式下的所有对象在立即使用后都将为零。

int m=0;

while ([arrData count]!=0) {

@autoreleasepool{

if (newQuery) {

    exeQuery = query;
    newQuery = false;

}
else 
{
    exeQuery=[exeQuery stringByAppendingFormat:@" UNION"];
}

NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithDictionary:[arrData objectAtIndex:0]];

[data removeObjectForKey:@"statement"];
[data removeObjectForKey:@"datetime_stamp"];

NSString *strVal=@"";
NSString *_value=@"";

int i=0;
    for(;i<[keys  count]-1;i++)
    {
        if(_value==nil || _value==NULL){
            _value=@"";
        }

        _value=[NSString stringWithFormat:@"%@",[data objectForKey:[keys objectAtIndex:i]]];
        _value=[_value stringByReplacingOccurrencesOfString:@"'" withString:@"''"];

        strVal=[strVal stringByAppendingFormat:@"'%@',",_value];

    }

if(_value==nil || _value==NULL){
    _value=@"";
}

_value=[NSString stringWithFormat:@"%@",[data objectForKey:[keys objectAtIndex:i]]];
_value=[_value stringByReplacingOccurrencesOfString:@"'" withString:@"''"];

strVal=[strVal stringByAppendingFormat:@"'%@'",_value];


exeQuery=[exeQuery stringByAppendingFormat:@" SELECT %@",strVal];


if (m!=0 && m%499==0) {

    [DataSource executeQuery:exeQuery];

    //db.execute(exeQuery);
    newQuery = true;
}

[data release];

[arrData removeObjectAtIndex:0];

m++;
}

for (;m<locations_length; m++) {

if (newQuery) {

    exeQuery = query;
    newQuery = false;

}
else 
{
    exeQuery=[exeQuery stringByAppendingFormat:@" UNION"];
}

NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithDictionary:[arrData objectAtIndex:m]];

[data removeObjectForKey:@"statement"];
[data removeObjectForKey:@"datetime_stamp"];

NSString *strVal=@"";
NSString *_value=@"";

int i=0;
    for(;i<[keys  count]-1;i++)
    {
        if(_value==nil || _value==NULL){
            _value=@"";
        }

        _value=[NSString stringWithFormat:@"%@",[data objectForKey:[keys objectAtIndex:i]]];
        _value=[_value stringByReplacingOccurrencesOfString:@"'" withString:@"''"];

        strVal=[strVal stringByAppendingFormat:@"'%@',",_value];

    }

if(_value==nil || _value==NULL){
    _value=@"";
}

_value=[NSString stringWithFormat:@"%@",[data objectForKey:[keys objectAtIndex:i]]];
_value=[_value stringByReplacingOccurrencesOfString:@"'" withString:@"''"];

strVal=[strVal stringByAppendingFormat:@"'%@'",_value];


exeQuery=[exeQuery stringByAppendingFormat:@" SELECT %@",strVal];


if (m!=0 && m%499==0) {

    [DataSource executeQuery:exeQuery];

    //db.execute(exeQuery);
    newQuery = true;
}

[data release]; 

}


}