Memory leaks 大规模内存泄漏-CoCoalLibSpotify
我正在使用CocoaLibSpotify库为Spotify搜索结果加载相册艺术 Instruments报告没有泄漏,静态分析也帮不上忙,我已经手动查看了所有记录加载相册艺术的代码,但在加载几百个结果后,应用程序消耗了超过100mb的内存并崩溃 我相信CocoaLibSpotify在内存中保留了一个图像缓存,但我没有找到禁用缓存的方法。有一个“flushCaches”方法,我每次收到内存警告时都会调用它,但它是无效的 下面是我用来加载相册艺术的内容,我保留了一个数组中所有SPImage对象的引用,以便在提供表视图行时使用它们Memory leaks 大规模内存泄漏-CoCoalLibSpotify,memory-leaks,spotify,cocoalibspotify-2.0,libspotify,Memory Leaks,Spotify,Cocoalibspotify 2.0,Libspotify,我正在使用CocoaLibSpotify库为Spotify搜索结果加载相册艺术 Instruments报告没有泄漏,静态分析也帮不上忙,我已经手动查看了所有记录加载相册艺术的代码,但在加载几百个结果后,应用程序消耗了超过100mb的内存并崩溃 我相信CocoaLibSpotify在内存中保留了一个图像缓存,但我没有找到禁用缓存的方法。有一个“flushCaches”方法,我每次收到内存警告时都会调用它,但它是无效的 下面是我用来加载相册艺术的内容,我保留了一个数组中所有SPImage对象的引用,
[self sendRequestToURL: @"http://ws.spotify.com/search/1/track.json" withParams: @{@"q": spotifySearchBar.text} usingMethod: @"GET" completionHandler: ^(id result, NSError *error) {
//after the search completes, re-enable the search button, replace the searchResults, and
// request the result table to reload the data
spotifySearchBar.userInteractionEnabled = YES;
[searchBar endEditing: YES];
[searchResults release];
int resultLength = [[result objectForKey: @"tracks"] count] < 100 ? [[result objectForKey: @"tracks"] count] : 100;
searchResults = [[[result objectForKey: @"tracks"] subarrayWithRange: NSMakeRange(0, resultLength)] retain];
for(int i = 0; i < 100; i++) {
[albumArtCache replaceObjectAtIndex: i withObject: [NSNull null]];
}
for(NSDictionary *trackDict in searchResults) {
NSString *trackURI = [trackDict objectForKey: @"href"];
[SPTrack trackForTrackURL: [NSURL URLWithString: trackURI] inSession: session callback: ^(SPTrack *track) {
[SPAsyncLoading waitUntilLoaded: track timeout: kSPAsyncLoadingDefaultTimeout then:^(NSArray *loadedItems, NSArray *notLoadedItems) {
if(track == nil) return;
[SPAsyncLoading waitUntilLoaded: track.album timeout: kSPAsyncLoadingDefaultTimeout then:^(NSArray *loadedItems, NSArray *notLoadedItems) {
if(track.album == nil) return;
[SPAsyncLoading waitUntilLoaded: track.album.largeCover timeout: kSPAsyncLoadingDefaultTimeout then:^(NSArray *loadedItems, NSArray *notLoadedItems) {
if(track.album.largeCover == nil) return;
if(![searchResults containsObject: trackDict]) {
NSLog(@"new search was performed, discarding loaded result");
return;
} else{
[albumArtCache replaceObjectAtIndex: [searchResults indexOfObject: trackDict] withObject: track.album.largeCover];
[resultTableView reloadRowsAtIndexPaths: @[[NSIndexPath indexPathForRow: [searchResults indexOfObject: trackDict] inSection: 0]] withRowAnimation: UITableViewRowAnimationAutomatic];
}
}];
}];
}];
}];
}
[resultTableView reloadData];
}];
我真的不知道出了什么问题。非常感谢您的帮助。首先,您应该使用
SPSearch
而不是web API进行搜索
Instruments没有显示内存泄漏的原因是因为没有内存泄漏-CoCoCoalibSpotify出于性能原因在内部缓存相册和图像。因此,加载的专辑封面也会粘在一起
现在,将数百个1024x1024图像加载到内存中显然会有糟糕的结果。缓解此问题的一个简单方法是不加载最大大小的图像-对于1024x1024像素的表视图,通常不需要加载最大大小的图像
否则,您可以修改CocoaLibSpotify以卸载图像。最简单的方法是向
SPImage
添加一个方法,该方法基本上与-惊人的加载相反,即将图像
属性设置为nil
,已开始加载
和将
属性加载到NO
并在spImage
属性上调用sp\u image\u release
,然后将其设置为NULL首先,您应该使用SPSearch
而不是web API进行搜索
Instruments没有显示内存泄漏的原因是因为没有内存泄漏-CoCoCoalibSpotify出于性能原因在内部缓存相册和图像。因此,加载的专辑封面也会粘在一起
现在,将数百个1024x1024图像加载到内存中显然会有糟糕的结果。缓解此问题的一个简单方法是不加载最大大小的图像-对于1024x1024像素的表视图,通常不需要加载最大大小的图像
否则,您可以修改CocoaLibSpotify以卸载图像。最简单的方法是向SPImage
添加一个方法,该方法基本上与-惊人的加载相反,即将图像
属性设置为nil
,已开始加载
和将
属性加载到否
并在spImage
属性上调用sp\u image\u release
,然后将其设置为NULL
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"artistCell"];
if(cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: @"artistCell"] autorelease];
}
cell.textLabel.text = [[searchResults objectAtIndex: indexPath.row] objectForKey: @"name"];
cell.detailTextLabel.text = [[[[searchResults objectAtIndex: indexPath.row] objectForKey: @"artists"] objectAtIndex: 0] objectForKey: @"name"];
if([albumArtCache objectAtIndex: indexPath.row] != [NSNull null]) {
cell.imageView.image = ((SPImage *)[albumArtCache objectAtIndex: indexPath.row]).image;
} else{
cell.imageView.image = nil;
}
return cell;
}