我这里有几段内存碎片整理的程序,希望高手可以告诉我里面用了什么算法,数据结构又如何?
procedure TForm1.Timer1Timer(Sender: TObject);
var ms : TMemoryStatus;
    l : longint;
    s:string;
begin
       ms.dwLength:=sizeof(ms);
       GlobalMemoryStatus(ms);
       MemBar.Position:=ms.dwAvailPhys shr 19;
       s:=Format('%d/%d',[ms.dwAvailPhys, ms.dwTotalPhys]);
       LMemInfo.Caption:=s;       tr.ReadBinaryData('KERNEL\CPUUsage',l, sizeof(l));
       LCPUStat.Caption:=Format('%d %%', [l]);
       s:='Memori bebas/Total '+s;
       lstrcpy(tnid.szTip, pchar(s));
       Shell_NotifyIcon(NIM_Modify, @tnid);
       if option.AutoDefrag then
       begin
           if (option.CPULimit=0) or (l<option.CPULimit) then
           begin
                if (GetTickCount-lastdefrag)<5000 then exit;
                if (membar.position shr 1)<option.MemLimit then
                begin
                            button2click(self);
                            LastDefrag:=GetTickCount;
                end;
           end;
       end;
       
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
     timer1.Enabled:=false;
     Button2.Enabled:=false;
     pie.Visible:=true;
     pie.MaxValue:=memlevel.position*2;
     Defragmem(memlevel.position,idle);
     pie.Visible:=false;
     Button2.Enabled:=true;
     timer1.Enabled:=true;     
end;

解决方案 »

  1.   

    没有什么特别的地方,而且你给出的信息太少,比如里面的全局变量tr是什么类型的你就没有给出,还有就是优化大师这样的软件用的内存整理,就是直接调用的ASP的一个对象,网上有相关的文章,你可以搜索看看。
      

  2.   

    没办法,这是一个作业。全部程序如下:
      protected
        procedure minimise(var msg: TMessage); message WM_SYSCOMMAND;
        procedure TaskBarHandler(var msg: TMessage); message MyWM_NotifyIcon;
      end;
    var
      Form1: TForm1;
      Totalmem : longint; //total memory dalam satuan megabyte
      Tr : TRegistry;
      tnid : TNotifyIconData;
      lastdefrag : longint;
      isFirst    : boolean;
      
    implementationuses info, option, about;{$R *.DFM}procedure TForm1.Info1Click(Sender: TObject);
    begin
         Form2.showmodal;
         showwindow(Application.Handle, SW_HIDE);     
    end;procedure TForm1.minimise(var msg: TMessage);
    begin
         case msg.WParam of
              SC_CLOSE : close;
              SC_MINIMIZE :
                          begin
                               showwindow(Application.Handle, SW_HIDE);
                               showwindow(Form1.Handle, SW_HIDE);
                          end;
              else
                  DefWindowProc(Form1.Handle, msg.msg, msg.WParam, msg.LParam);
         end;
    end;procedure TForm1.Exit1Click(Sender: TObject);
    begin
           Close;
    end;procedure TForm1.FormCreate(Sender: TObject);
    var ms : TMemoryStatus;
        trg : tregistry;
        i   : integer;
    begin
           LastDefrag:=GetTickCount;
           tnid.cbSize := sizeof(TNotifyIconData);
           tnid.Wnd := Form1.handle;
           tnid.uID := $2111;
           tnid.uFlags := NIF_MESSAGE or NIF_ICON or NIF_TIP;
           tnid.uCallbackMessage := MYWM_NOTIFYICON;
           tnid.hIcon := Application.Icon.Handle;
           Shell_NotifyIcon(NIM_ADD, @tnid);
           ms.dwLength:=sizeof(ms);
           GlobalMemoryStatus(ms);
           TotalMem:=(ms.dwTotalPhys shr 20) + 1;
           MemLevel.Max:=Totalmem;
           MemBar.Max:=TotalMem*2;
           Tr:=Tregistry.create;
           tr.RootKey:=HKEY_DYN_DATA;
           tr.OpenKey('PerfStats\StatData',false);
           pie.Visible:=false;
           trg:=tregistry.create;
           with trg do
                begin
                     RootKey:=HKEY_CURRENT_USER;
                     OpenKey(KeyName, true);
                     i:=ReadInteger('MemToFree');
                     if (i<MemLevel.Min) or (i>MemLevel.Max) then
                        begin
                             i:=MemLevel.Max shr 2;
                             WriteInteger('MemToFree', i);
                        end;
                     MemLevel.Position:=i;
                     closekey;
                     free;
                end;
         LInfo.Caption:=Format(
         'Defragmen RAM sebanyak %d Mb', [MemLevel.Position]);
    end;procedure TForm1.Timer1Timer(Sender: TObject);
    var ms : TMemoryStatus;
        l : longint;
        s:string;
    begin
           ms.dwLength:=sizeof(ms);
           GlobalMemoryStatus(ms);
           MemBar.Position:=ms.dwAvailPhys shr 19;
           s:=Format('%d/%d',[ms.dwAvailPhys, ms.dwTotalPhys]);
           LMemInfo.Caption:=s;       tr.ReadBinaryData('KERNEL\CPUUsage',l, sizeof(l));
           LCPUStat.Caption:=Format('%d %%', [l]);
           s:='Memori bebas/Total '+s;
           lstrcpy(tnid.szTip, pchar(s));
           Shell_NotifyIcon(NIM_Modify, @tnid);
           if option.AutoDefrag then
           begin
               if (option.CPULimit=0) or (l<option.CPULimit) then
               begin
                    if (GetTickCount-lastdefrag)<5000 then exit;
                    if (membar.position shr 1)<option.MemLimit then
                    begin
                                button2click(self);
                                LastDefrag:=GetTickCount;
                    end;
               end;
           end;
           
    end;procedure TForm1.MemLevelChange(Sender: TObject);
    begin
         LInfo.Caption:=Format(
         'Defragmen RAM sebanyak %d Mb', [MemLevel.Position]);
    end;procedure idle;
    begin
         Application.processMessages;
         Form1.Pie.progress:=Form1.Pie.progress+1;
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
         timer1.Enabled:=false;
         Button2.Enabled:=false;
         pie.Visible:=true;
         pie.MaxValue:=memlevel.position*2;
         Defragmem(memlevel.position,idle);
         pie.Visible:=false;
         Button2.Enabled:=true;
         timer1.Enabled:=true;     
    end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
         Shell_NotifyIcon(NIM_DELETE, @tnid);
    end;procedure TForm1.Option1Click(Sender: TObject);
    begin
         Form3.ShowModal;
         showwindow(Application.Handle, SW_HIDE);
    end;procedure TForm1.About1Click(Sender: TObject);
    begin
         Form4.showmodal;
         showwindow(Application.Handle, SW_HIDE);     
    end;
    procedure Tform1.TaskBarHandler(var msg: TMessage);
    begin
         case msg.LParamLo of
              WM_LBUTTONDOWN :
                             begin
                                  if not IsWindowVisible(form1.handle)
                                  then showWindow(form1.handle, sw_show);
                             end;
         end;
    end;procedure TForm1.FormShow(Sender: TObject);
    begin
         showwindow(Application.Handle, SW_HIDE);
    end;procedure TForm1.FormDestroy(Sender: TObject);
    var
       trg : tregistry;
    begin
           trg:=tregistry.create;
           with trg do
                begin
                     RootKey:=HKEY_CURRENT_USER;
                     OpenKey(KeyName, true);
                     WriteInteger('MemToFree',MemLevel.Position);
                     closekey;
                     free;
                end;
           tr.closeKey;
           tr.Free;
    end;procedure TForm1.FormPaint(Sender: TObject);
    begin
         if IsFirst and option.MinOnLoad then
         begin
              hide;
              IsFirst:=false;
         end;
    end;begin
         isFirst:=true;
    end.望高手指点,急急急
      

  3.   

    麻烦有空的帮小妹解决一下这个问题
    http://expert.csdn.net/Expert/topic/2385/2385608.xml?temp=.8815271
    先谢了