problem with gc?

jklp via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed May 27 00:32:13 PDT 2015


On Wednesday, 27 May 2015 at 05:48:13 UTC, zhmt wrote:
> I am writing a echoclient, as below:
>
> Ptr!Conn conn = connect("127.0.0.1",8881);
> ubyte[100] buf;
> for(int i=0; i<N; i++)
> {
> 	scope string str = format("%s",i);
> 	conn.write((cast(ubyte*)str.ptr)[0..str.length]);
> 	conn.read(buf[0..str.length]);
> 	n++;
> }
> conn.close();
>
>
> When it loops about more than 100000 times,the throughput will 
> fall from 60000request/second to 26request/second.
>
> If the code changes a little as below:
>
>  scope string str = format("%s",10000); //changes to a constant
>
> It will runs normally with speed of 60000request/second. I test 
> for 13minutes, everything seems well.
>
>
>
> What happened when the code changes a little? Who will give an 
> explaination,Thanks a lot?

Have you tried to declare str in the upper scope ?

---
Ptr!Conn conn = connect("127.0.0.1",8881);
ubyte[100] buf;
string str;
for(int i=0; i<N; i++)
{
     str = format("%s",i);
     conn.write(cast(ubyte[]) str);
     conn.read(buf[0..str.length]);
     n++;
}
conn.close();
---

And also you could try to surround the whole block with 
`GC.disable` and `GC.enable`. This would help to determine if the 
GC is involved:

---
Ptr!Conn conn = connect("127.0.0.1",8881);
GC.disable;
ubyte[100] buf;
string str;
for(int i=0; i<N; i++)
{
     str = format("%s",i);
     conn.write(cast(ubyte[]) str);
     conn.read(buf[0..str.length]);
     n++;
}
GC.enable;
conn.close();
---


More information about the Digitalmars-d-learn mailing list