0

I developed an app to download web pages from a site. For the huge web site, it will run a long time. I noticed that the occupied memory increased continuously(from 30M to 300M after download 2600 web pages), but there is no any memory leak in Instrument.

enter image description here

enter image description here

after download 3648 web pages, its info

enter image description here

enter image description here

After I stop the download procedure, occupied memory do not decrease for memory releasing.

I change all code

[NSString stringWithFormat:@"someThing"];

to

[[NSString alloc] initWithFormat:@"someThing"];

a little improvement, occupied memory drops to 300m

your comment welcome

Swift Dev Journal
  • 19,282
  • 4
  • 56
  • 66
arachide
  • 8,006
  • 18
  • 71
  • 134
  • Rob Napier is right, that you should focus on the “persistent” numbers, not transient or “total” numbers, which include memory that has been released. Your persistent numbers look fine. – Rob Jun 29 '19 at 16:46
  • As an aside, you seem to place a lot of weight on the absence of any reported leaks. Unfortunately, that captures only a very small fraction of the potential problems. The most common memory problem that plagues our projects is strong reference cycles (aka retain cycles), and those are not reported as a leak. See [How to debug memory leaks when Leaks instrument does not show them?](https://stackoverflow.com/questions/30992338/how-to-debug-memory-leaks-when-leaks-instrument-does-not-show-them/30993476#30993476) But, again, unless the “persistent” numbers increasing, don’t worry about it. – Rob Jun 29 '19 at 16:51

1 Answers1

1

This chart is the total number of allocations, not the current memory usage. The "persistent" column (allocations that haven't been released) is around 40MB. What it's telling you is that you have allocated at various times about 5.68GB of data and released almost all of it. That's why the red bar is almost entirely pale red, with just a little solid red on the left.

This suggests there may be ways to improve performance by reusing more memory rather than allocating and releasing it (which is very time-consuming). Or perhaps by avoiding unnecessary temporary allocations. But if you're not having a performance issue, there's no actual problem here.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • updated question, occupied memory went up to 1.4 GB after download 3648 web pages, even I stop downloading, the occupied memory never go down for deallocation – arachide Jun 29 '19 at 15:23
  • What you've posted here isn't helpful in debugging your memory issues; you've just posted total allocations (most of which are freed). It's also very unclear what you mean by your example starting "I change all code." Does usage drop from 1.4GB to 300MB with that change? Or do you mean something else? At a start, you need to dig into the memory graph in Instruments and look for what type of allocation is using most of your persistent (not transient) memory. Your screenshot is not helpful for that. Look at the stack traces on the right as you explore. – Rob Napier Jun 29 '19 at 15:39
  • Using `[NSString stringWithFormat:@"someThing"];` is very wasteful (it's equivalent to the incredibly cheaper `@"someThing"`), but it's unclear from your question whether you mean `stringWithFormat:` on a static string, or if you meant something else. Read over your question and think through "how would I answer this question, given only the information in the question?" If switching between the `stringWith` and `initWith` forms makes a big difference in your high-water mark (but the memory all comes back later), you probably have an autorelease pool you're not draining. – Rob Napier Jun 29 '19 at 15:42
  • 1.4G is not important, I just want to say the occupied memory went up to 0.4 or 1.4 GB, even I stop the download(press the stop button), the occupied memory do not go down for deallocation as I expected while there is no any memory leak – arachide Jun 29 '19 at 16:41
  • Likely because you're retaining it somewhere. Perhaps intentionally. Perhaps in a system cache you're not aware of (this is purgeable memory and is not relevant). You don't have to have a leak to increase memory usage; you can also keep storing things. But if you are having a memory problem, then the above comments discuss how to debug it. A snapshot of total (mostly transient) allocations doesn't help us help you. Similarly, a snapshot that just says "1.41GB" doesn't help us help you. – Rob Napier Jun 29 '19 at 21:40