×
Namespaces

Variants
Actions

如何压缩堆内存

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

兼容于
平台: S60 3rd Edition
S60 3rd Edition FP1

文章
翻译:
hoolee
最后由 hamishwillee 在 29 Aug 2012 编辑
  • 详细描述

我们可以通过CompressAllHeaps()方法回收用户堆栈的可用空间。它不会进行堆栈碎片整理,并会忽略锁定堆栈,因此十分高效。


User::CompressAllHeaps() 返回的内存数可能千变万化,无法预测,因此并非适用于任何地方。

检查进行内存分配(使用User::Alloc())的代码片段,并立刻调用CompressAllHeaps()和Heap().Compress()方法压缩内存。

log文件会记录空闲RAM容量的增长。这个方法只有在用户堆栈顶部有空闲内存时才会发挥生效。


  • 解决方案

下列代码片段演示了如何使用CompressAllHeaps方法:

//sample code
TInt retval;
TInt retval1;
TInt ibeforeFreeRAM;
TInt iafterFreeRAM;
TInt iafterFreeRAMCompress;
User::Alloc(882688);
HAL::Get(HAL::EMemoryRAMFree, ibeforeFreeRAM);
TBuf<50> temp;
TBuf<50> temp1;
TBuf<50> temp2;
DebugLog(_L("ibeforeFreeRAM "));
DebugLog(_L(""));
temp.AppendNum(ibeforeFreeRAM);
DebugLog(temp);
retval=User::CompressAllHeaps(); //using CompressAllHeaps method
HAL::Get(HAL::EMemoryRAMFree, iafterFreeRAM);
DebugLog(_L("iafterFreeRAM"));
DebugLog(_L(""));
temp1.AppendNum(iafterFreeRAM);
DebugLog(temp1);
User::Heap().Compress(); //using Heap Compress method
HAL::Get(HAL::EMemoryRAMFree, iafterFreeRAMCompress);
DebugLog(_L("iafterFreeRAM : Compress"));
DebugLog(_L(""));
temp2.AppendNum(iafterFreeRAMCompress);
DebugLog(temp2);
 
 
Output sample:
ibeforeFreeRAM 23875584
iafterFreeRAM 24113152
iafterFreeRAM : Compress 24113152
This page was last modified on 29 August 2012, at 06:42.
137 page views in the last 30 days.
×