Search code examples
macosmemory-managementwebviewwebkit

Why does a release'ed WebView on OS X continue to use Real Memory?


I made an NSWindow containing a WebView. It loads from a nib file and displays webpages in the WebView. After closing the NSWindow/WebView, they're released. If it's re-opened, a new NSWindow/WebView is created and loaded from the nib. Release When Closed is TRUE for the NSWindow.

First column is memory usage.

  1. 30MB - Application loads.
  2. 60MB - NSWindow/WebView created and loads from nib.
  3. 160MB - Browse many webpages.
  4. 158MB - NSWindow/WebView closed and released.
  5. 158MB - ...time passes...
  6. 160MB - NSWindow/WebView created and loads from nib.
  7. 200MB - Browse many webpages.
  8. 198MB - NSWindow/WebView closed and released.
  9. 198MB - ...time passes...
  10. 200MB - NSWindow/WebView created and loads from nib.
  11. 160MB - Browse many webpages.
  12. 158MB - NSWindow/WebView closed and released.
  13. 158MB - ...time passes...
  14. 160MB - NSWindow/WebView created and loads from nib.
  15. 200MB - Browse many webpages.
  16. 198MB - NSWindow/WebView closed and released.

As you can see, Real Memory (reported by System Preferences) does not decrease much after the NSWindow and its WebView are released. Furthermore, memory seems to expand up to around 200MB, and then oscillates between 160MB and 200MB until the app quits (WebKit is doing memory management behind the scenes I presume).

Why doesn't real memory reduce back to 30MB after I release my NSWindow/WebView? Is there any way to force it to do this? Or is this something I shouldn't even worry about?


Solution

  • You shouldn't worry about it as there is probably some behind the scenes memory management that is keeping the memory pools around just in case they are needed soon. A lot of memory managers will keep reserve pools of memory to increase application responsiveness when requesting from these pools (avoiding system call for increased heap size). This is likely happening and is probably why you see the application hit it's working set size and simply oscillate because of allocations/deallocations. This is normal behavior and unless you're seeing actual memory leaks then you shouldn't worry about it unless it's affecting application or system performance.