我记得在CSDN上看过一篇文章,说是使用程序保持在占用内存只有10K的水平,因为调用了一个整理内存的API,哪位老大能帮忙找到那篇文章,或者能告诉那个API函数,谢谢.

解决方案 »

  1.   

    procedure DefragMemory(MemoryLimit:Integer);
    var
     Pointers : Array [0..1024] of Pointer;
     Limit    ,
     I2,
     I        : Integer;
     P        : Pointer;
     Step     : Integer;
     Steps    : Integer;
    begin
     FAbort := False;
     If FWorking then Exit;
     FWorking:=True;
     Limit:=MemoryLimit;
     If Limit>1024 then Limit:=1024;
     If Assigned(FOnBeforeDefrag) then FOnBeforeDefrag(Self);
     { Calculating how many steps...}
     Steps:=(MemoryLimit*2); Step:=0; { Clean pointer...}
     For I:= 0 to Limit do Pointers[I]:=nil; { Allocating Memory }
     For I:=0 to Limit-1 do
     Begin
      P:=VirtualAlloc(nil, 1024*1024, MEM_COMMIT, PAGE_READWRITE + PAGE_NOCACHE);
      Pointers[I]:=p;
      asm
       pushad
       pushfd
       mov   edi, p
       mov   ecx, 1024*1024/4
       xor   eax, eax
       cld
       repz  stosd
       popfd
       popad
      end;
      Inc(Step);
      If Assigned(FOnProgress) then OnProgress(Self,Round((Step/Steps)*100));
      If FAbort then
      Begin
        For I2:=0 to I do
        Begin
         VirtualFree(Pointers[I2], 0, MEM_RELEASE);
        End;
        Step:=(MemoryLimit*2);
        FWorking:=False;
        If Assigned(FOnAfterDefrag) then FOnAfterDefrag(Self);
        Exit;
      End;
     end; { DeAllocating Memory } For I:=0 to Limit-1 do
     Begin
      VirtualFree(Pointers[i], 0, MEM_RELEASE);
      Inc(Step);
      If Assigned(FOnProgress) then OnProgress(Self,Round((Step/Steps)*100));
      If FAbort then
      Begin
        { Warning! : Force abort, w/o de-allocating memory }    
        Step:=(MemoryLimit*2);
        FWorking:=False;
        If Assigned(FOnAfterDefrag) then FOnAfterDefrag(Self);
        Exit;
      End;
     End; FWorking:=False; If Assigned(FOnAfterDefrag) then FOnAfterDefrag(Self);
    End;
      

  2.   

    http://www.delphibbs.com/delphibbs/dispq.asp?lid=1606059
      

  3.   

    我在程序中加上了这个,呵呵.内存果然变小了.不知道行不行.多谢楼上各位老大的帮助.
            
    if Win32Platform = VER_PLATFORM_WIN32_NT then
    begin
      SetProcessWorkingSetSize(GetCurrentProcess, $FFFFFFFF, $FFFFFFFF);
      application.ProcessMessages;
    end;