What Is Dirty Private Memory

What does Private_Dirty memory mean in smaps?

Memory is either private, meaning it is exclusive to this process, or shared, meaning multiple processes may have it mapped and in use (think shared library code, etc.). Memory can also be clean - it hasn't been modified since it was loaded from disk or provided as zero-filled pages or whatever, and so if it needs to be freed to provide memory pages for other processes, it can just be discarded, and reloaded/re-filled if it is ever needed again - or dirty, which means if it needs to be freed up, it must be written out to a swap area so that the modified contents can be recovered when necessary.

It's not necessarily unusual to see large amounts of private dirty data in a process. The problem is when the sum of all private dirty data across all processes in the system becomes a significant portion (exact numbers depend greatly on your workload and acceptable performance) of your overall physical memory, and stuff has to start being swapped in/out...

What is dirty private memory?

Talking about the memory non-decrease even after freeing some chunks, you'd better use mmap in anonymous mode like this:

mmap(NULL, chunck_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);

which maps no file descriptor and returns a pointer to a memory chunk which is immediately returned to the OS when you unmap it. However, mmap requires a system call, which is slower than malloc. So, you should use mmap for allocating large pages.

Shared_dirty vs Private_dirty in shared memory

The distinction between Clean and Dirty refers to whether or not the pages have been written-out to the backing store since being written to in memory. For a mapping of /dev/zero, pages are obviously never written-out, so Clean pages have only been read whereas Dirty pages have been written to.

For a shared mapping, the distinction between Private and Shared is whether the pages have only been referenced by the process you're examining, or whether they've been referenced by multiple processes.

So in summary:

  • Shared_Clean are the pages in the mapping that have been referenced by this process and at least one other process, but not written by any process;
  • Shared_Dirty are the pages in the mapping that have been referenced by this process and at least one other process, and written by at least one of those processes;
  • Private_Clean are the pages in the mapping that have been read and not written by this process but not referenced by any other process;
  • Private_Dirty are the pages in the mapping that have been written by this process but not referenced by any other process.

Pages can move from Clean to Dirty when they're written to, and from Private to Shared when another process references them.

If you map a real disk file, then pages can also move from Dirty to Clean when they're written-out by the kernel.

What is resident and dirty memory of iOS?

It's almost a year and I figured it out.

clean memory

clean memory are memories that can be recreated, on iOS it is memory of:

  • system framework
  • binary executable of your app
  • memory mapped files

Also notice this situation: when your app link to a framework, the clean memory will increase by the size of the framework binary. But most of time, only part of binary is really loaded in physical memory.

dirty memory

All memory that is not clean memory is dirty memory, dirty memory can't be recreated by system.

When there is a memory pressure, system will unload some clean memory, when the memory is needed again, system will recreate them.

But for dirty memory, system can't unload them, and iOS has no swap mechanism, so dirty memory will always be kept in physical memory, till it reach a certain limit, then your App will be terminated and all memory for it is recycled by system.

virtual memory

virtual memory = clean memory + dirty memory.

That means virtual memory is all the memory your App want.

resident memory

resident memory = dirty memory + clean memory that loaded in physical memory

resident memory is the memory really loaded in your physical memory, it mean all the dirty memory and parts of your clean memory.

conclusion

At any time, this is always true:

virtual memory == (clean memory + dirty memory) > resident memory > dirty memory

If you are worrying about the physical memory your App is taking(which is the key reason your App is terminated due to low memory), you should mainly focus on resident memory.

What is dirty memory in Instruments?

Dirty is a computer term used to denote cached data that needs to be sync'ed with the main memory. Don't worry, since this is automatically done by the hardware.

What do Dirty and Resident mean in relation to Virtual Memory?

"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.



Related Topics



Leave a reply



Submit