春暖花开
雪化了,花开了,春天来了
posts - 149,comments - 125,trackbacks - 0

前阶段在调研mobile上的内存释放问题,根据大家的建议尝试了一些,但是没有什么效果。
于是跑到MSDN上去问相关的问题,得到了一些解答。

As far as I know, I don't think there is other way to meet your requirement.  Since each mobile application has 32M memory limitation, we have to do the performance manually, like deleting object which is not used and allocating Large Memory Blocks in Large memory Area.

For more information:
Windows CE .NET Advanced Memory Management
How the Windows Mobile 5.0 Shell Handles Low Memory Situations

我看了一部分,在How the Windows Mobile 5.0 Shell Handles Low Memory Situations中提到,

 

 

Hibernate

This is the amount of memory the shell tries to keep free at all times. If the amount of free memory falls below this value then the low memory check routine will try to free up memory. It will do this by first sending WM_HIBERNATE to all valid applications. When an application receives this message it should try to free as many resources as possible. When the low memory check routine runs again and the amount of free memory is still below the hibernate level then the shell will try to close the least recently used (LRU) application by sending a WM_CLOSE message. If the low memory check routine runs yet again and the amount of free memory is still below the hibernate level then the shell will call TerminateProcess on the LRU application that it last sent the WM_CLOSE message to.


我对这段话的理解是:当空闲内存小于Hibernate时,系统便会尝试释放内存。首先他会向所有有效的程序发送WM_HIBERNATE。应用程序收到该消息后,会尽量释放资源来释放内存。如果还低于这个值的话,将发送WM_CLOSE消息给最近很少使用的程序。如果还低于该值的,就强行关闭该程序了。

这个方法似乎有效,但是用户再起来程序的话,还是一样的效果,并不是达到了什么释放内存的效果。如何记录最近很少使用的程序,这个是不是有什么方法获得呢?

就可以像系统提示的那样,内存不足,请释放一些内存。手工做一些比较好。

也看了一些相近的程序的做法,似乎效果也没有大家说的那么好。

How the Windows Mobile 5.0 Shell Handles Low Memory Situations的最后,这样写的

What can my Application do?

The best thing your application can do when faced with a low memory situation is to play nicely with the rest of the device. 

1、If your application receives a WM_HIBERNATE message free up any resources not absolutely required. 
2、If you are planning on allocating a large amount of memory (or if a large allocation fails) you should call SHCloseApps, which will invoke the shell low memory check routine and will try to ensure that enough free memory exists. See below for an example:

#define MIN_MEMORY_TO_RUN 2*1024*1024

MEMORYSTATUS mst;

mst.dwLength  
= sizeof(MEMORYSTATUS);

GlobalMemoryStatus(
&mst);

If (mst.dwAvailPhys 
< MIN_MEMORY_TO_RUN)

{

    
// Try to free memory by asking Shell to shutdown apps

    
if (!SHCloseApps(MIN_MEMORY_TO_RUN))

    
{

        
// Handle the case where memory could not be freed

        …


专门查了一下SHCloseApps的用法
原型:BOOL SHCloseApps(  DWORD dwMemSought);
功能:
This function tries to free up memory for an application. If necessary, the shell closes down other applications by sending dwMemSought);
功能:
This function tries to free up memory for an application. If necessary, the shell closes down other applications by sending WM_CLOSE messages.
参数:dwMemSought Specifies, in bytes, the amount of memory to be freed.

通过了解dwMemSought Specifies, in bytes, the amount of memory to be freed.

通过了解SHCloseApps,我们就明白了上面那段代码什么意思了。

看来,内存释放并不是真正意义上的内存整理,看来我有的地方误解了!
posted on 2009-03-05 13:56 Sandy 阅读(1276) 评论(2)  编辑 收藏 引用 所属分类: windows学习

FeedBack:
# re: 关于mobile的内存释放
2009-03-05 14:13 | 亨德列克
高!  回复  更多评论
  
# re: 关于mobile的内存释放[未登录]
2009-03-05 16:54 | foxriver
PC上有个类似的函数HeapCompact,初看很像内存整理,仔细看也不是的。内存整理牵涉的操作太多,操作系统也不好办。  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理