Objective c 可能是NSURLSession或NSMutableURLRequest未通过循环释放内存调用
我的问题:-NSURLSession没有释放5MB区块的上一个调用API内存 我在do while循环中调用API来上传500MB视频。我必须用不同的API发送每个5MB块,而不是在一个API中 例如,500MB视频和创建100个区块,并使用NSURLSession发送,因此调用100次,但NSURLSession不会释放5MB区块的先前调用API内存 (1) 我已经创建了5MB块 (2) 使用NSFileHandle读取文件,使用偏移量读取5MB块 (3) 更改所有区块的URL并调用api(以不同URL发送所有区块所需) 我不想在NSData(500MB)中转换视频,我想通过API发送块Objective c 可能是NSURLSession或NSMutableURLRequest未通过循环释放内存调用,objective-c,nsurlconnection,nsurlsession,nsurl,nsmutableurlrequest,Objective C,Nsurlconnection,Nsurlsession,Nsurl,Nsmutableurlrequest,我的问题:-NSURLSession没有释放5MB区块的上一个调用API内存 我在do while循环中调用API来上传500MB视频。我必须用不同的API发送每个5MB块,而不是在一个API中 例如,500MB视频和创建100个区块,并使用NSURLSession发送,因此调用100次,但NSURLSession不会释放5MB区块的先前调用API内存 (1) 我已经创建了5MB块 (2) 使用NSFileHandle读取文件,使用偏移量读取5MB块 (3) 更改所有区块的URL并调用api(以
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
//dispatch_group_t group = dispatch_group_create();
__block NSUInteger counterFailure = 0; // PSM Anks calling blob by url fails 4 time, exit for funtion
arrBlobIds = [[NSMutableArray alloc]init];
__block NSInteger intBlockIdCount = 100000; // PSM Anks blobid to assign id to every blob
__block NSUInteger offset = 0; // PSM Anks offset to start posution to read data
NSUInteger length = [[[NSFileManager defaultManager] attributesOfItemAtPath:[urlOfGallaryVideo path] error:nil] fileSize]; // PSM anks total lenght of media
NSUInteger intChunkSize = (5000 * 1024); // PSM anks chunk size
while (offset < length){
//dispatch_group_enter(group);
NSLog(@"offset 1 : %lu",(unsigned long)offset);
// PSM Anks Creat Chunk from file according to length
NSUInteger intThisChunkSize = length - offset > intChunkSize ? intChunkSize : length - offset;
//NSData* chunk = [NSData dataWithBytesNoCopy:(char *)[myBlob bytes] + offset length:intThisChunkSize freeWhenDone:NO];
__block NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:[urlOfGallaryVideo path]];
[fileHandle seekToFileOffset:offset];
__block NSData *dataChunk = [fileHandle readDataOfLength:intThisChunkSize];
// PSM Anks Convert block id in Base 64 encode
NSData *dataBlockId = [[NSString stringWithFormat:@"%ld",intBlockIdCount] dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64BlockId = [dataBlockId base64EncodedStringWithOptions:0];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@&comp=block&blockid=%@",[dictAzureSAS objectForKey:@"uri"],base64BlockId]]];
[request setHTTPMethod:@"PUT"];
[request setHTTPBody:dataChunk];
//[request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)dataChunk.length] forHTTPHeaderField:@"Content-Length"]; // Do not need
//[request setValue:strVideoMIMEType forHTTPHeaderField:@"Content-Type"]; // Do not need
[request addValue:@"BlockBlob" forHTTPHeaderField:@"x-ms-blob-type"];
//NSLog(@"request : %@",request);
//NSLog(@"dataChunk.length : %lu \n url for blob %@ \n request %@",(unsigned long)dataChunk.length,[NSURL URLWithString:[NSString stringWithFormat:@"%@&comp=block&blockid=%@",[dictAzureSAS objectForKey:@"uri"],base64BlockId]],request);
NSLog(@"dataChunk.length : %lu",(unsigned long)dataChunk.length);
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.URLCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
config.timeoutIntervalForRequest = 20.0;
config.URLCredentialStorage = nil;
config.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
///NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
config = nil;
//NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
NSURLSessionDataTask *dataTaskForUpload = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"Finished with status code: %li", (long)[(NSHTTPURLResponse *)response statusCode]);
NSLog(@"response: %@", response);
NSLog(@"error: %@ %@", error,error.description);
if(data != nil) // PSM anks Check Data is nil otherwise app crashed
{
NSMutableArray *jsonList = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"jsonList: %@", jsonList);
}
dataChunk = nil;
fileHandle = nil;
if(error == nil)
{
/*
// PSM Anks First Add Then increment
[arrBlobIds addObject:base64BlockId];
intBlockIdCount++;
offset += intThisChunkSize;
counterFailure = 0;
*/
}
else
{
/*
counterFailure++;
offset = intThisChunkSize;
if(counterFailure >= 4)
{
NSLog(@"Enter counter Failure %lu",(unsigned long)counterFailure);
counterFailure = 0;
[self stopLoader];
[CommonAlertViewMsgs cannotConnectServer:self];
return ;
}
*/
}
//dispatch_group_leave(group);
dispatch_semaphore_signal(semaphore);
[session finishTasksAndInvalidate];
}];
[dataTaskForUpload resume];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"offset 2 : %lu",(unsigned long)offset);
}
dispatch\u async(dispatch\u get\u global\u queue(dispatch\u queue\u PRIORITY\u DEFAULT,0)^{
dispatch\u semaphore\u t semaphore=dispatch\u semaphore\u create(0);
//dispatch_group_t group=dispatch_group_create();
__block nsuiger counterFailure=0;//PSM Anks通过url调用blob失败4次,退出函数
arrBlobIds=[[NSMutableArray alloc]init];
__block NSInteger intBlockIdCount=100000;//PSM Anks BLOBBID为每个blob分配id
__block NSUTEGER offset=0;//PSM Anks offset开始读取数据
NSInteger长度=[[NSFileManager defaultManager]AttributesFiteMatPath:[urlOfGallaryVideo path]错误:nil]fileSize];//PSM anks介质的总长度
NSUInteger intChunkSize=(5000*1024);//PSM anks块大小
while(偏移量<长度){
//调度组输入(组);
NSLog(@“偏移量1:%lu”,(无符号长)偏移量);
//PSM Anks根据长度从文件创建块
NSUTEGER INTTHESCHUNKSIZE=长度-偏移>intChunkSize?intChunkSize:长度-偏移;
//NSData*chunk=[NSData datawithbytesnopy:(char*)[myBlob字节]+偏移量长度:intthishchunksize freehendone:NO];
__block NSFileHandle*fileHandle=[NSFileHandle fileHandleForReadingAtPath:[urlOfGallaryVideo path]];
[文件句柄seekToFileOffset:offset];
__block NSData*dataChunk=[fileHandle readDataOfLength:IntThishChunkSize];
//PSM Anks转换基64编码中的块id
NSData*dataBlockId=[[NSString stringWithFormat:@“%ld”,intBlockIdCount]数据使用编码:NSUTF8StringEncoding];
NSString*base64BlockId=[dataBlockId base64EncodedStringWithOptions:0];
NSMutableURLRequest*请求=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@“%@&comp=block&blockid=%@],[dictAzureSAS objectForKey:@“uri”],base64BlockId]];
[请求设置HttpMethod:@“放置”];
[请求setHTTPBody:dataChunk];
//[request setValue:[NSString stringWithFormat:@“%lu”,(无符号长)dataChunk.length]forHTTPHeaderField:@“Content length”];//不需要
//[请求设置值:HttpHeaderField的StrVideMimeType:@“内容类型”];//不需要
[请求添加值:@“BlockBlob”用于HttpHeaderField:@“x-ms-blob-type”];
//NSLog(@“请求:%@”,请求);
//NSLog(@“dataChunk.length:%lu\n blob%@\n request%@”的url,(无符号长)dataChunk.length,[NSURL URLWithString:[NSString stringWithFormat:@“%@&comp=block&blockid=%@,[DictAzuresa objectForKey:@“uri]”,base64BlockId]],request);
NSLog(@“dataChunk.length:%lu”,(无符号长)dataChunk.length);
NSURLSessionConfiguration*配置=[NSURLSessionConfiguration defaultSessionConfiguration];
config.URLCache=[[NSURLCache alloc]initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
config.timeoutitervalforrequest=20.0;
config.URLCredentialStorage=nil;
config.requestCachePolicy=NSURLRequestReloadIgnoringLocalCacheData;
///NSURLSession*session=[NSURLSession sessionWithConfiguration:config];
config=nil;
//NSURLSession*会话=[NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSession*会话=[NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]委托:自委托队列:[NSOperationQueue mainQueue]];
NSURLSessionDataTask*dataTaskForUpload=[session dataTaskWithRequest:request completionHandler:^(NSData*数据,NSURLRResponse*响应,NSError*错误){
NSLog(@“已完成,状态代码为:%li”,(长)[(NSHTTPURLResponse*)响应状态代码];
NSLog(@“响应:%@”,响应);
NSLog(@“error:%@”,error,error.description);
如果(data!=nil)//PSM anks检查数据为零,否则应用程序崩溃
{
NSMutableArray*jsonList=[NSJSONSerialization JSONObjectWithData:数据选项:NSJSONReadingMutableContainers错误:nil];
NSLog(@“jsonList:%@”,jsonList);
}
dataChunk=nil;
fileHandle=nil;
如果(错误==nil)
{
/*
//PSM Anks先添加,然后增加
[arrBlobIds addObject:base64BlockId];
intBlockIdCount++;
偏移量+=intThishChunkSize;
反断层=0;
*/
}
其他的
{
/*
反帆++;
偏移量=intThishChunkSize;
如果(反故障>=4)
{
NSLog(@“En
while (offset < length) @autoreleasepool {
- (void)sendRequestWithOffset:length:otherParameters: {
send the url request {
do what you do
if newOffset < length {
[self sendRequestWithOffset:newOffset length:length otherParameters:whatever];
} else {
hooray, we're done
}
}
}