Delphi TStringBuilder prevents the server memory fragmentation

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Delphi 2009+ System.SysUtils provides a.Net like StringBuilder, used to store an array of characters.

Many people do not understand why to use TStringBuilder, string Delphi, have the function, almost all of the TStringBuilder   System.SysUtils and TStringHelper to facilitate the expansion of String, the use of more convenient String.

From the Delphi source code, TStringBuilder and string, the biggest difference is that, TStringBuilder can apply for the memory space in advance, and limits the maximum memory space, does not automatically release, while string is how much for how much memory, automatic release.

Thus the TStringBuilder relative to the String has 2 advantages:

1 in the development of the server software, use the TStringBuilder pool, can effectively prevent the memory fragmentation.

2 without the need for open storage space for the new string greater efficiency, pre application memory TStringBuilder to exceed string.

TServerString the following code inherited from the TStringBuilder, the memory space limits the size of MaxMemorySize,, when preparing written characters will exceed memory size, will automatically raise exceptions,

The server software can receive this abnormal after treatment.

The following code to test:


  1 unit Iocp.ServerString;
  2 
  3 interface
  4 
  5 uses SysUtils, Classes;
  6 
  7 type
  8   TServerStringPool=class;
  9   TServerString=class(TStringBuilder)
 10   protected
 11     class var FPool:TServerStringPool;
 12     constructor Create;
 13   public
 14     destructor Destroy;override;
 15   end;
 16 
 17   TServerStringPool=class
 18   protected
 19     FList:TThreadList;
 20   public
 21     constructor Create(Capacity:Integer);
 22     destructor Destroy;override;
 23     function GetString:TServerString;
 24     procedure FreeString(Value:TServerString);
 25   end;
 26 
 27 implementation
 28 
 29 const
 30   MaxMemorySize=16384;
 31 
 32 
 33 { TServerString }
 34 
 35 constructor TServerString.Create;
 36 var
 37   N:Integer;
 38 begin
 39   N:=(MaxMemorySize-InstanceSize) div SizeOf(Char);
 40   inherited Create(N, N);
 41 end;
 42 
 43 destructor TServerString.Destroy;
 44 begin
 45   FPool.FreeString(Self);
 46 end;
 47 
 48 { TServerStringPool }
 49 
 50 constructor TServerStringPool.Create(Capacity: Integer);
 51 var
 52   i: Integer;
 53   L:TList;
 54 begin
 55   inherited Create;
 56   L:=FList.LockList;
 57   try
 58     for i := 1 to Capacity do
 59     begin
 60       L.Add(TServerString.Create);
 61     end;
 62   finally
 63     FList.UnlockList;
 64   end;
 65 end;
 66 
 67 destructor TServerStringPool.Destroy;
 68 var
 69   i: Integer;
 70   L:TList;
 71 begin
 72   L:=FList.LockList;
 73   try
 74     for i := L.Count-1 to 0 do
 75       TStringBuilder(L[i]).Free;
 76   finally
 77     FList.UnLockList;
 78   end;
 79   FList.Free;
 80   inherited;
 81 end;
 82 
 83 procedure TServerStringPool.FreeString(Value: TServerString);
 84 begin
 85   FList.Add(Value);
 86 end;
 87 
 88 function TServerStringPool.GetString: TServerString;
 89 var
 90   L:TList;
 91 begin
 92   L:=FList.LockList;
 93   try
 94     if L.Count=0 then
 95     begin
 96       Result:=TServerString.Create;
 97     end
 98     else
 99     begin
100       Result:=TServerString(L[L.Count-1]);
101       L.Delete(L.Count-1);
102     end;
103   finally
104     FList.UnlockList;
105   end;
106 end;
107 
108 initialization
109   TServerString.FPool:=TServerStringPool.Create(10240);
110 finalization
111   TServerString.FPool.Free;
112 end.





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Elmer at December 09, 2013 - 1:58 AM