2011-04-07 1 views
1

이 코드의 문제점은 while 루프가 실행될 때 메모리 사용이 지속적으로 증가한다는 것입니다. 이 코드는 while 루프에서 메모리를 늘리는 이유가 무엇인지 알고 싶습니다. 메모리를 먹고있는 개체가 여기에 있습니다. 루프에서 메모리가 증가하지 않도록하려면 어떻게해야합니까?while 루프 메모리 문제

당신이 거기있는 일부 비효율적 인 코드가
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
NSNumber *totalTime = [[self nowPlayingItem] valueForProperty:MPMediaItemPropertyPlaybackDuration]; 
while (self.currentPlaybackTime < [totalTime floatValue]) 
{ 
    NSNumber *currentTime = [[NSNumber alloc] initWithFloat:self.currentPlaybackTime]; 
    if([[NSThread currentThread] isCancelled]) 
    { 
     [NSThread exit]; 
     [newThread release]; 
    } 
    else if([totalTime intValue] - [currentTime intValue] == 1.0) 
    { 
     if(currentTime) 
      [currentTime release]; 
     break; 
    } 
    [currentTime release]; 
} 
[pool release] 
+0

[풀 드레인]이고; 할당 계기를 사용하여 문제가 발생하면 찾으십시오. – 0xDE4E15B

+0

내가 언급 한 것처럼 루프가 실행 중일 때 메모리가 증가합니다. Allocations 장비에서 얻을 수있는 것이 있습니까? – Dhawal

+0

작년의 WWDC 비디오를 보면 잠시 동안 자동 회수 풀이 놓이는 것을 볼 수 있습니다. 이렇게하면주기 당 모든 자동 회수 객체가 해제되므로 메모리 유출이 줄어 듭니다. – rckoenes

답변

0

나는이 문제를 해결했다. while 루프를 Timer로 바꾸고 몇 가지 변경 사항을 적용했습니다. performAction에서 다음 매초

timer = [NSTimer timerWithTimeInterval:1 
           target:self 
           selector:@selector(performAction) 
           userInfo:nil 
           repeats:YES]; 

를 발생하는 타이머는, 현재의 재생 시점을 확인하여 타이머를 무효화 등재 그 시간차는 < = 1 초

int totalTime = [[[self nowPlayingItem] valueForProperty:MPMediaItemPropertyPlaybackDuration] intValue]; 
    if((totalTime - self.currentPlaybackTime) <= 1.0) 
    { 
     if(timer) 
      [timer invalidate]; 

     /* Performed my desired action here.... */ 
    } 
+0

다른 사용자가 동일한 문제를 겪고 Google에서 질문을 찾으면 변경 사항을 게시 할 수 있습니까? –

+0

그것은 실제로 문제가 아닙니다. 그는 가능한 한 빨리 반복되는 긴밀한 루프를 실행하고 루프에서 자동 해제 된 개체를 만듭니다. 해결책은 DON "T DO THAT입니다. – gnasher729

0

.... 그렇지 않은 있도록 Heres는 더 이상 아무 이유없이 정신 할당하는 메모리를하지 않는 대체 당신이 거기에서 잠을 넣어 할 수 있습니다 CPU를 먹는다

// not needed any more 
//NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

int totalTime = [[[self nowPlayingItem] valueForProperty:MPMediaItemPropertyPlaybackDuration] intValue]; 

while (self.currentPlaybackTime < totalTime) 
{ 
    //not needed any more 
    //NSNumber *currentTime = [[NSNumber alloc] initWithFloat:self.currentPlaybackTime]; 
    if([[NSThread currentThread] isCancelled]) 
    { 
     [NSThread exit]; 
     [newThread release]; 
    } 
    else if((totalTime - self.currentPlaybackTime) <= 1.0) // you may never hit 1 
    { 
     break; 
    } 
} 
//[pool release] 
+0

안녕하세요 Tony, 고맙습니다. 문제는 계속됩니다. 메모리는 while 루프에서 마운트 할 때 계속 유지됩니다. 그리고 네,이 코드를 본 후, 저는 제 코드에 당혹 스럽습니다. 저는 여전히 iOS에 익숙하지 않고 제 코딩 스타일을 좋지 않게 개선합니다. – Dhawal

+0

@Dhawal 메모리가 계속 증가하는 경우 문제는 아마도 우리가 보여준 코드에없는 것입니다. 아니면 인스 트루먼 트에서 잘못 찍은 것일 수도 있습니다. –