Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: Memory Leak

  1. #11

    Memory Leak

    One more variant: "Original split - optimized"

    First case (VERY long string):
    Code:
       928 (total items: 110121) - Original split
        64 (total items: 110110) - DelimitedText split
       427 (total items: 110121) - Harry Hunt split
        62 (total items: 110121) - Original split - optimized  (!)
    Second case (MANY average strings):
    Code:
       125 (total items: 111111) - Original split
        66 (total items: 110110) - DelimitedText split
       405 (total items: 111111) - Harry Hunt split
        64 (total items: 111111) - Original split - optimized   (!)
    :lol: :lol: :lol: :lol: :lol: :lol:

    CODE:
    [pascal]
    function TForm1.Split_4(const Source, Delimiter: String): TStringList;
    var
    DelLength: Integer;
    SearchPos, FoundAt: Integer;
    begin
    Result := TStringList.Create;
    DelLength:= Length(Delimiter);
    SearchPos:= 1; // initialize it

    FoundAt:= PosEx(Delimiter, Source, SearchPos);
    while FoundAt > 0 do
    begin
    Result.Add(Copy(Source, SearchPos, FoundAt - SearchPos));
    SearchPos:= FoundAt + DelLength;
    FoundAt:= PosEx(Delimiter, Source, SearchPos);
    end;

    Result.Add(Copy(Source, SearchPos, MaxInt));
    end;
    [/pascal]
    There are only 10 types of people in this world; those who understand binary and those who don't.

  2. #12

    Memory Leak

    So now it's xGTx's CLOOTIE Optimized Split version that wins? haha

    Clootie, your a genious! thats pretty dang fast.

    But now im thinking, as Harry said, delimiting by a string may be a bit slower... What if we change that new split function to delimit by a single char only.
    I have a 2005 CRF 250 so <^>(>&lt<^>
    <br />http://www.gtrpg.com/

  3. #13

    Memory Leak

    Quote Originally Posted by xGTx
    But now im thinking, as Harry said, delimiting by a string may be a bit slower... What if we change that new split function to delimit by a single char only.
    Well, this will be your home work for tomorrow
    There are only 10 types of people in this world; those who understand binary and those who don't.

  4. #14

    Memory Leak

    Code:
    Results&#58; &#91;Length of string split&#58; 720036&#93;
     Clootie Optimized Original                    172ms
     My un-optimized clootie optimized of original 266ms

    Alright i tried out by char and obviously its slower. So, CLOOTIE WINS
    I have a 2005 CRF 250 so &lt;^&gt;(&gt;&lt&lt;^&gt;
    <br />http://www.gtrpg.com/

  5. #15

    Memory Leak

    Why..? Why you wrote it..!

    [pascal]function TForm1.Split_5(const Source, Delimiter: String): TStringList;
    var
    SourceLength: Integer;
    SearchPos, FoundAt: Integer;
    Delim: Char;
    begin
    Assert(Length(Delimiter) = 1); // !
    Delim:= Delimiter[1];
    Result := TStringList.Create;
    SourceLength:= Length(Source);
    SearchPos:= 1; // initialize it
    FoundAt:= 1;

    while (FoundAt <= SourceLength) do
    begin
    if Source[FoundAt] = Delim then
    begin
    Result.Add(Copy(Source, SearchPos, FoundAt - SearchPos));
    Inc(FoundAt);
    SearchPos:= FoundAt;
    end;
    Inc(FoundAt);
    end;

    Result.Add(Copy(Source, SearchPos, MaxInt));
    end;
    [/pascal]
    There are only 10 types of people in this world; those who understand binary and those who don't.

  6. #16

    Memory Leak

    You optimized it!

    If i create a string list... Can i use it multiple times before freeing it (in the same function call) or do i have to free it each time i load new data set in it?

    Edit:

    I don't understand... My loading function is not freeing my string lists even though im clearly freeing them in the code! Ive managed to use 1.6 GB of ram in 60 seconds of loading. <--- Im talking about with the Split() function.. Clooties version. I tried it thru a few tests and it doesn't leak any memory, but in my load function it leaks tons. I went from Split back to Delphi's Delimited way and its not leaking anymore. So it's definitly the split function for some odd reason.

    With Clootie's Optimized split:
    Code:
    GTRPG Server Runtime Environment 0.3.5.0
    Loading Data.
    Loading Maps.
    2025 maps loaded in 64 seconds.
    Initializing Players. Maximum&#58; 500
    Socket awaiting connections.
    
    1.6 GB of RAM used.
    With Delphi's delimited:
    Code:
    GTRPG Server Runtime Environment 0.3.5.0
    Loading Data.
    Loading Maps.
    2025 maps loaded in 52 seconds.
    Initializing Players. Maximum&#58; 500
    Socket awaiting connections.
    
    157 MB of RAM used.

    But heres the code: Obviously i have the Split() parts commented out replaced by delphi's delimit
    [pascal]
    tSplit := TStringList.Create;
    nSplit := TStringList.Create;
    gSplit := TStringList.Create;

    //tSplit := Split(Buffer, '^');
    tSplit.Delimiter := '^';
    tSplit.DelimitedText := Buffer;


    SetLength(Buffer,0);

    K := 0;

    For X := 0 to MAP_WIDTH do
    For Y := 0 to MAP_HEIGHT do
    Begin
    //nSplit := Split(tSplit.Strings[K], ',');
    nSplit.Delimiter := ',';
    nSplit.DelimitedText := tSplit.Strings[K];
    Inc(K);

    // - Bunch of Map stuff loaded here -

    if nSplit.Count = 17 Then
    begin
    //gSplit := Split(nSplit.Strings[16],'|');
    gSplit.Delimiter := '|';
    gSplit.DelimitedText := nSplit.Strings[16];
    for B := 0 to gSplit.Count - 1 Do
    begin
    Int := StrToInt(gSplit.Strings[B]);
    // - More map Stuff -
    end;
    end;
    end;

    tSplit.Free;
    gSplit.Free;
    nSplit.Free;

    Result := True;[/pascal]


    Now, im no Delphi expert, far from it in-fact but one thing that does cross my mind... In the split function we have
    [pascal]Result := TStringList.Create;[/pascal]
    That kind of makes me believe a copy of the string list is being created and obviously never freed... I may be far off here but just a thought.

    Somewhat supporting my theory, I goto MSX's suggestion of passing the already made TStringList as a parameter and it again works, fast, and no memory leak!
    [pascal]procedure Split(const Source, Delimiter: String; CopyTo : TStringList);
    var
    DelLength: Integer;
    SearchPos, FoundAt: Integer;
    begin
    //Result := TStringList.Create;
    DelLength:= Length(Delimiter);
    SearchPos:= 1; // initialize it

    FoundAt:= PosEx(Delimiter, Source, SearchPos);
    while FoundAt > 0 do
    begin
    //Result.Add(Copy(Source, SearchPos, FoundAt - SearchPos));
    CopyTo.Add(Copy(Source, SearchPos, FoundAt - SearchPos));
    SearchPos:= FoundAt + DelLength;
    FoundAt:= PosEx(Delimiter, Source, SearchPos);
    end;
    //Result.Add(Copy(Source, SearchPos, MaxInt));
    CopyTo.Add(Copy(Source, SearchPos, MaxInt));
    end;[/pascal]


    BUT! I'd still like to know why the original doesn't work in the case above ?
    I have a 2005 CRF 250 so &lt;^&gt;(&gt;&lt&lt;^&gt;
    <br />http://www.gtrpg.com/

  7. #17

    Memory Leak

    You're creating StringLists inside a loop and try to free them ontside of it, so youre freeing only the last ones

  8. #18

    Memory Leak

    I thought it'd use the same memory if i used that same StringList, but ok, im going to change up the code a bit when i get home tonight thanks
    I have a 2005 CRF 250 so &lt;^&gt;(&gt;&lt&lt;^&gt;
    <br />http://www.gtrpg.com/

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •