Simple low memory warning?

  • Does there exist a low-memory warning for Mac apps similar to the didReceiveMemoryWarning for the iPhone?  I have looked at NSCache and other related classes, but for my purposes it would be better to simply receive a low memory warning and then act on the warning.  Does such a warning exist for the Mac?

    Thanks in advance,
    Bill
  • On Apr 20, 2010, at 10:10 AM, Bill wrote:

    > Does there exist a low-memory warning for Mac apps similar to the didReceiveMemoryWarning for the iPhone?  I have looked at NSCache and other related classes, but for my purposes it would be better to simply receive a low memory warning and then act on the warning.  Does such a warning exist for the Mac?

    Unfortunately no, but that would be a good idea, especially in 32-bit apps. Have you filed an enhancement request?

    Nick Zitzmann
    <http://www.chronosnet.com/>
  • One possibility is to install a CFRunLoopObserverRef that would query for available memory, using task_info on mach_task_self().
    Then you can check if you start to are coming nearer to "dangerous territories". Not perfect though as usually the moment you can not alloc some memory it can be already too late

    laurent

    On Tuesday, April 20, 2010, at 07:17PM, "Nick Zitzmann" <nick...> wrote:
    >
    > On Apr 20, 2010, at 10:10 AM, Bill wrote:
    >
    >> Does there exist a low-memory warning for Mac apps similar to the didReceiveMemoryWarning for the iPhone?  I have looked at NSCache and other related classes, but for my purposes it would be better to simply receive a low memory warning and then act on the warning.  Does such a warning exist for the Mac?
    >
    > Unfortunately no, but that would be a good idea, especially in 32-bit apps. Have you filed an enhancement request?
    >
    > Nick Zitzmann
    > < http://www.chronosnet.com/
    >
    >
  • On Apr 20, 2010, at 10:17 AM, Nick Zitzmann wrote:

    >
    > On Apr 20, 2010, at 10:10 AM, Bill wrote:
    >
    >> Does there exist a low-memory warning for Mac apps similar to the didReceiveMemoryWarning for the iPhone?  I have looked at NSCache and other related classes, but for my purposes it would be better to simply receive a low memory warning and then act on the warning.  Does such a warning exist for the Mac?
    >
    > Unfortunately no, but that would be a good idea, especially in 32-bit apps. Have you filed an enhancement request?
    >

    No, haven't filed anything yet.  Thought I'd wait to see if anything like that existed first.

    My app has several tasks that collect data via a loop and display it on a table.  Objects are retained as they're displayed so the objects don't have to be recalculated next time they're displayed.  But if I received a low-memory warning, I could release that data, and recalculate it later if needed.  Seems simple enough, but I don't know how to find out when memory is low.

    -Bill
  • Le 20 avr. 2010 à 19:23, Bill a écrit :

    >
    > On Apr 20, 2010, at 10:17 AM, Nick Zitzmann wrote:
    >
    >>
    >> On Apr 20, 2010, at 10:10 AM, Bill wrote:
    >>
    >>> Does there exist a low-memory warning for Mac apps similar to the didReceiveMemoryWarning for the iPhone?  I have looked at NSCache and other related classes, but for my purposes it would be better to simply receive a low memory warning and then act on the warning.  Does such a warning exist for the Mac?
    >>
    >> Unfortunately no, but that would be a good idea, especially in 32-bit apps. Have you filed an enhancement request?
    >>
    >
    > No, haven't filed anything yet.  Thought I'd wait to see if anything like that existed first.
    >
    > My app has several tasks that collect data via a loop and display it on a table.  Objects are retained as they're displayed so the objects don't have to be recalculated next time they're displayed.  But if I received a low-memory warning, I could release that data, and recalculate it later if needed.  Seems simple enough, but I don't know how to find out when memory is low.
    >

    And what make NSCache and NSDiscardableContent not appropriate here ?

    -- Jean-Daniel
  • On Apr 20, 2010, at 10:34 AM, Jean-Daniel Dupas wrote:

    >
    > Le 20 avr. 2010 à 19:23, Bill a écrit :
    >
    >>
    >> On Apr 20, 2010, at 10:17 AM, Nick Zitzmann wrote:
    >>
    >>>
    >>> On Apr 20, 2010, at 10:10 AM, Bill wrote:
    >>>
    >>>> Does there exist a low-memory warning for Mac apps similar to the didReceiveMemoryWarning for the iPhone?  I have looked at NSCache and other related classes, but for my purposes it would be better to simply receive a low memory warning and then act on the warning.  Does such a warning exist for the Mac?
    >>>
    >>> Unfortunately no, but that would be a good idea, especially in 32-bit apps. Have you filed an enhancement request?
    >>>
    >>
    >> No, haven't filed anything yet.  Thought I'd wait to see if anything like that existed first.
    >>
    >> My app has several tasks that collect data via a loop and display it on a table.  Objects are retained as they're displayed so the objects don't have to be recalculated next time they're displayed.  But if I received a low-memory warning, I could release that data, and recalculate it later if needed.  Seems simple enough, but I don't know how to find out when memory is low.
    >>
    >
    > And what make NSCache and NSDiscardableContent not appropriate here ?

    They are not inappropriate, but they require a lot more work than a simple warning sent via a notification or other means.

    -Bill
  • Le 20 avr. 2010 à 19:36, Bill a écrit :

    >
    > On Apr 20, 2010, at 10:34 AM, Jean-Daniel Dupas wrote:
    >
    >>
    >> Le 20 avr. 2010 à 19:23, Bill a écrit :
    >>
    >>>
    >>> On Apr 20, 2010, at 10:17 AM, Nick Zitzmann wrote:
    >>>
    >>>>
    >>>> On Apr 20, 2010, at 10:10 AM, Bill wrote:
    >>>>
    >>>>> Does there exist a low-memory warning for Mac apps similar to the didReceiveMemoryWarning for the iPhone?  I have looked at NSCache and other related classes, but for my purposes it would be better to simply receive a low memory warning and then act on the warning.  Does such a warning exist for the Mac?
    >>>>
    >>>> Unfortunately no, but that would be a good idea, especially in 32-bit apps. Have you filed an enhancement request?
    >>>>
    >>>
    >>> No, haven't filed anything yet.  Thought I'd wait to see if anything like that existed first.
    >>>
    >>> My app has several tasks that collect data via a loop and display it on a table.  Objects are retained as they're displayed so the objects don't have to be recalculated next time they're displayed.  But if I received a low-memory warning, I could release that data, and recalculate it later if needed.  Seems simple enough, but I don't know how to find out when memory is low.
    >>>
    >>
    >> And what make NSCache and NSDiscardableContent not appropriate here ?
    >
    > They are not inappropriate, but they require a lot more work than a simple warning sent via a notification or other means.
    >
    > -Bill
    >

    I Don't know if it works, but what append if you create an NSCache and put a single object that implements discardContentIfPossible to send a notification ?

    -- Jean-Daniel
  • On Apr 20, 2010, at 10:36 AM, Bill wrote:

    >> And what make NSCache and NSDiscardableContent not appropriate here ?
    >
    > They are not inappropriate, but they require a lot more work than a simple warning sent via a notification or other means.

    The iPhone has a much simpler memory model than Mac OS (or Windows or Linux). It doesn’t have virtual memory, and it also has very limited RAM. This makes low-memory warnings necessary.

    With virtual memory, things are more complicated. The good news is that on Mac OS your process won’t “run out of memory” until it allocates about 3GB of heap if it’s 32 bit; if it’s 64-bit it will never run out of memory*. The bad news is that before that happens, the VM paging is likely to slow your machine to a crawl, as everything else on the system gets paged out.

    So the danger on Mac OS X is not running out of memory, it’s using too much RAM and swap space, causing performance to suffer. Avoiding this is tricky because it depends on the differences between address space, RAM and backing store. You can allocate a gigabyte of address space with no ill effects as long as you don’t map any of it in. But conversely, merely accessing a large already-malloced block might cause your process to block for hundreds of milliseconds if that address space was paged out.

    NSCache and NSDiscardableContent are high-level constructs that try to simplify these decisions and make them easier to use. Yes, they’re more complex than a simple “please free up memory right now” warning. But they make a lot more sense on Mac OS. (And in practice, they end up automating what you’d probably already be doing in response to a low-memory warning, i.e. looking for data that can be safely thrown away and freeing it.)

    —Jens

    * It will, however, eventually fill up your hard disk with VM backing store files. When this happens the OS starts suspending processes while it puts up a critical alert asking you to quit apps or free up disk space.
  • On Apr 20, 2010, at 11:48 AM, Jens Alfke wrote:

    >
    > On Apr 20, 2010, at 10:36 AM, Bill wrote:
    >
    >>> And what make NSCache and NSDiscardableContent not appropriate here ?
    >>
    >> They are not inappropriate, but they require a lot more work than a simple warning sent via a notification or other means.
    >
    > The iPhone has a much simpler memory model than Mac OS (or Windows or Linux). It doesn’t have virtual memory, and it also has very limited RAM. This makes low-memory warnings necessary.
    >
    > With virtual memory, things are more complicated. The good news is that on Mac OS your process won’t “run out of memory” until it allocates about 3GB of heap if it’s 32 bit; if it’s 64-bit it will never run out of memory*. The bad news is that before that happens, the VM paging is likely to slow your machine to a crawl, as everything else on the system gets paged out.
    >
    > So the danger on Mac OS X is not running out of memory, it’s using too much RAM and swap space, causing performance to suffer. Avoiding this is tricky because it depends on the differences between address space, RAM and backing store. You can allocate a gigabyte of address space with no ill effects as long as you don’t map any of it in. But conversely, merely accessing a large already-malloced block might cause your process to block for hundreds of milliseconds if that address space was paged out.
    >
    > NSCache and NSDiscardableContent are high-level constructs that try to simplify these decisions and make them easier to use. Yes, they’re more complex than a simple “please free up memory right now” warning. But they make a lot more sense on Mac OS. (And in practice, they end up automating what you’d probably already be doing in response to a low-memory warning, i.e. looking for data that can be safely thrown away and freeing it.)
    >
    > —Jens
    >
    > * It will, however, eventually fill up your hard disk with VM backing store files. When this happens the OS starts suspending processes while it puts up a critical alert asking you to quit apps or free up disk space.

    That helps and explains a lot.

    Thanks!

    -Bill
previous month april 2010 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today