Getting a memory warning in the ViewController ("ScrollView-style page scroll")

I have a project where there are two views, in the first view I have a uitableview displayed. And when you select any of the cells in the row, another view opens.

In the first view there are 10 cells and each cell, when selected, opens another view consisting of at most 21 views and at least 1 view inside.

Each view in the second view consists of a table view of an array of images and many subzones in a tableview view (also an array of images is displayed inside a scroll with several types of images)

Now the problem is that I can’t free the memory allocation that I don’t know, because according to my code, all that is allocated is the release. I also tried instrumental ones, but this does not work.

Previously, when I visited only one view, it crashed the application, so I reduced the resolution of the images, and now it moves for almost 9 applications when another application is not open, otherwise it works for 6 views. Now I can’t even reduce the resolution of the images, because now it is of lower quality. So, how can I reduce this memory allocation, which does not support my application in the least and does not even free the occupied memory.

Make sure you are not responding with a sentence like instrumental leak because it is not useful. I need to know how to free up unused memory.

You can see that in one view there are three views, and this is the scroll bar, as you move by scrolling more views, and the image will be displayed on top, which is also a scroll bar. In addition, there is detailed information about the table. But when I create this view, I encounter a memory problem, so for this I free everything from this view, even I tried to delete the whole view during navigation. Please help me urgently. For this, I used the example "PagedFlowView"

https://github.com/kejinlu/PagedFlowView

The link above is for pagedflowview example, and the link below is for my code reference class that I implemented, please check my class and tell me where I can improve it.

http://www.4shared.com/file/rHmRDr2E/SwipeExerciseViewController.html

Thanks in advance and really carried away if you help me.

+6
source share
6 answers

I recently had problems with a UITableView, and in my case it was creating a UITableViewCell that was wrong. In fact, there are at least two things to check:

  • Consider using autorelease for a UITableViewCell:

    UITableViewCell * cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease]; 
  • Best practice requires reusing UITableViewCell instances using [tableView dequeueReusableCellWithIdentifier] (see example below, taken from cellForRowAtIndexPath)

     UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) cell = [self tableViewCellWithReuseIdentifier:CellIdentifier]; 

The second part requires you to assign and process an "identifier" (a string used to identify a cell).

Please consider sending the code if you still have questions. Sincerely, D.

0
source

Remember one more thing when working with images. When you use an image, it does not accept in memory what it shows as the file size, as many would expect (i.e. Something less than 3 MB for 2048 x 1536). Instead, it actually loads in raw format, taking memory based on the calculation as follows:

width x height xn bytes, where n is the number of bits taken to represent colors per pixel, mostly 4.

therefore, for the same 2048 x 1536 image, it will take 12 MB.

So, now check what the original resolution of the image you are talking about is, and calculate how many MB it will need, and change your code accordingly.

0
source

I just took a quick look at the PagedFlowView project. In fact, although it was the wrong reuse of the UIImageView you created, which allows you to increase memory. But everything seems to be in order. I tried the auto-calculation pool (only on PagedFlowView using .tiff files) and, apparently, it reduces the number of allocated bytes by 20%.

In the PagedFlowView.m file, line 85, you should find the removeCellAtIndex method: (NSInteger) index

Try changing it as follows:

 - (void)removeCellAtIndex:(NSInteger)index{ UIView *cell = [_cells objectAtIndex:index]; if ((NSObject *)cell == [NSNull null]) { return; } @autoreleasepool { [self queueReusableCell:cell]; if (cell.superview) { cell.layer.transform = CATransform3DIdentity; [cell removeFromSuperview]; } [_cells replaceObjectAtIndex:index withObject:[NSNull null]]; } } 

This should help. But perhaps this is not enough. If you still have a problem, I can get a deeper look at how PagedFlowView uses _cells and _reusableCells.

0
source

Adding 100 empty views also does not cause memory problems, but adding 10 images causes memory problems. Rethink the process of how you intend to present your data.

One of the ways that I can suggest is to reduce the size of the thumbnail that you show in the second view, where you said that there will be from 1 to 21 views. Then enter the extra step, like clicking on this thumbnail, to go to the detail screen with the original resolution.

The second approach, I can think about the fact that I have an image of a place. Download only images of visible cells asynchronously and write them to the Cache directory, and then keep displaying only visible cells

0
source

First - check your dealloc, it seems that you have at least a music library (and probably a lot more). On the second - recycle

 - (UIView *)flowView:(PagedFlowView *)flowView cellForPageAtIndex:(NSInteger)index 

In your current code:

if view is zero: you create an autodetection pool but don’t reset it (and it is better to use the @autoreleasepool directive), then you skip scrollview (alloc / init, then add a subview, but cannot see any issue), then a pointer to the image is set to nil, and then you try to free it (the nil message is non-op, but the same for the image above), etc. this part is full of leaks, as it seems from the code.

If the view is not null: you add a new scrollview, etc. To this already configured mind (and their leak too). Therefore, instead of just reconfiguring these views, you almost recreate them.

I would suggest you subclass UIView for these views, make public methods like

 - (void)configureWithParameters:(NSDictionary *)parameters; - (id)initWithParameters:(NSDictionary *)parameters; 

in this class and when flowview requests a view, first try deleting the reusable cell, if it is nil - alloc / init / autorelease this view with the necessary parameters, if the view has already been created - use the configure method.

In addition, this code needs a significant reorganization. It will be difficult to maintain, make changes or improve.

0
source

Having studied my code, I found that each time you select an image and a web view on a cell. You can reduce this part by adding an image and webview when the cell is zero. Since you know that cells are being reused, we can avoid the memory warning for this.

Here is the modified code:

 static NSString * store=@ "cell"; UITableViewCell *utvc = [[tableView dequeueReusableCellWithIdentifier:store] autorelease]; if(utvc == nil) { utvc = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease]; UIImageView *textView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 40, 276, 360)]; textView.tag = 1001; // some tag value [textView setImage:[UIImage imageNamed:@"bluelineBg.png"]]; [utvc addSubview:textView]; [textview release]; UIWebView *webv = [[UIWebView alloc]initWithFrame:CGRectMake(0, -12, 277, 300)]; webv.userInteractionEnabled = FALSE; webv.scrollView.scrollEnabled = FALSE; webv.tag = 1002;//some tag value [webv setBackgroundColor:[UIColor clearColor]]; [webv setOpaque:NO]; [utvc addSubview:webv]; [webv releae]; } UIImageView *imageview = [cell viewWithTag:1001]; [imageview setImage:[UIImage imageNamed:@"bluelineBg.png"]]; UIWebView *webView = [cell viewWithTag:1002]; [webView loadHTMLString:[NSString stringWithFormat:@"<html><body style='font-family:Helvetica; font-size:14px;background-color: transparent; '><head><style> OL{ color: #555555 } </style></head><br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Mudra</b> : Prayer Pose<br><br><ol style=\"color:color: #999999;\"><li>Sit comfortably in easy pose with the legs crossed</li><li>Bring the palms together at the center of the chest</li><li>Inhale deeply</li><li>Chant 'Sat Nam' three times or chant the 'Long Time Sunshine' song to finish<br><br><i>Time per repetition : 15 Sec.</i></li></ol></body></html>"] baseURL:Nil]; 
0
source

Source: https://habr.com/ru/post/951119/


All Articles