1

Is there a way to get notifications when the available memory goes below a certain limit (user configurable)? I have looked into the WM_HIBERNATE event but it seems the limit for this event is not user configurable and is hard-coded (160KB??). The only way I can think of is to poll the OS periodically (every 5 seconds??) for available memory through GlobalMemoryStats() API and if the available memory goes below the user configured limit then raise an event to the user. Is there a better way?

This notification has to work on devices starting with Windows CE 5.0 & Windows Mobile 6.5

AndroidDev
  • 5,193
  • 5
  • 37
  • 68
  • 1
    "The Windows Mobile 5 shell runs a low memory check routine periodically (on Pocket PC this is done every 5 seconds, on Smartphone its 30 seconds) and assesses the state of the system memory. The routine will check to see if any action is required to keep a reasonable amount of memory free (the OEM ultimately decides what a reasonable amount is)." : http://blogs.msdn.com/b/windowsmobile/archive/2006/08/16/702746.aspx – Mitch Wheat Mar 26 '12 at 15:33

2 Answers2

2

There's nothing inherent in the OS that's going to tell you that memory is below an arbitrary level, no. Either polling in your app or creating a service/driver that does it for you is probebly the best way. Which is "better" would depend on if only one process needs to know about the notification.

ctacke
  • 66,480
  • 18
  • 94
  • 155
2

If thats your application that can drain memory, then you can implement some code to intercept such low memory situations before system will do it. Some hints:

  • override your new, malloc or any other function that allocates memory and check each time if allocation will drain your memory.

  • if you are afraid you will slow down your app (and you should:)) then use some memory manager, like Dougs Lee malloc.c, you can modify is so each time it grabs another 64KB block from system memory you will check if system memory is not too low

  • you can read from registry what are the low memory levels, you will find these entries under: HKLM\System\Hibernate for WCE and HKLM\System\OOM for WM.

http://msdn.microsoft.com/en-us/library/ms911907.aspx

marcinj
  • 48,511
  • 9
  • 79
  • 100
  • Thank you for letting me know the registry entries, can we modify these values? I tried increasing the values cbLow and cpLow but I'm not getting the WM_HIBERNATE notification. Also just to let you know that I am writing a service for this task. – AndroidDev Mar 27 '12 at 09:19
  • 1
    I have never tried modifying them,I was just reading to get the value where my application should try recover memory before system takes actions. I was having issues with WindowsCE device that was showing heavy dialog to inform of low memory, this dialog was showing large bitmap file what was actually freezing device because there was no memory to load it. – marcinj Mar 29 '12 at 16:29