35

I dropped out of the CS program at my university... So, can someone who has a full understanding of Computer Science please tell me: what is the meaning of Dirty and Resident, as relates to Virtual Memory? And, for bonus points, what the heck is Virtual Memory anyway? I am using the Allocations/VM Tracker tool in Instruments to analyze an iOS app.

*Hint - try to explain as if you were talking to an 8-year old kid or a complete imbecile. Thanks guys.

m0rtimer
  • 2,023
  • 1
  • 25
  • 31

2 Answers2

47

"Dirty memory" is memory which has been changed somehow - that's memory which the garbage collector has to look at, and then decide what to do with it. Depending on how you build your data structures, you could cause the garbage collector to mark a lot of memory as dirty, having each garbage collection cycle take longer than required. Keeping this number low means your program will run faster, and will be less likely to experience noticeable garbage collection pauses. For most people, this is not really a concern.

"Resident memory" is memory which is currently loaded into RAM - memory which is actually being used. While your application may require that a lot of different items be tracked in memory, it may only require a small subset be accessible at any point in time. Keeping this number low means your application has lower loading times, plays well with others, and reduces the risk you'll run out of memory and crash as your application is running. This is probably the number you should be paying attention to, most of the time.

"Virtual memory" is the total amount of data that your application is keeping track of at any point in time. This number is different from what is in active use (what's being used is marked as "Resident memory") - the system will keep data that's tracked but not used by your application somewhere other than actual memory. It might, for example, save it to disk.

blueberryfields
  • 45,910
  • 28
  • 89
  • 168
  • 5
    While this is correct for Mac OS X, there is no virtual memory or swapping to disk on iOS. – Steve Madsen Mar 29 '11 at 13:15
  • 2
    Developers are indirectly encouraged to work as if the two are not there. However, they are there, they're just not fully featured out, and not currently directly available to developers/application (that is, not without jail-breaking your iphone). – blueberryfields Mar 29 '11 at 16:04
  • 1
    Applications are never notified of swapping activity, regardless of the OS. The different between Mac OS X and iOS from a developer's perspective is that we can be a bit more sloppy on OS X, knowing that a) there is more system RAM and b) if we allocate too much, stuff can be swapped out before the system gets into real trouble. That's not true on iOS, and the system will kill your app if you over-allocate. As to if "virtual memory" and "swap" is technically possible on iOS, I don't doubt that it is, but I'll be surprised if iOS ever swaps anything. – Steve Madsen Mar 29 '11 at 20:21
  • As of iOS9 and later, the swapping mechanism has become more fleshed out and fully featured; full disk swapping still isn't in though, see https://developer.apple.com/videos/play/wwdc2018/416/ – blueberryfields Jul 17 '19 at 19:14
3

WWDC 2013 - 410 Fixing Memory Issues Explains this nicely. Well worth a watch since it also explains some of the practical implications of dirty, resident and virtual memory.

Robert
  • 37,670
  • 37
  • 171
  • 213