1

I have code that basically opens a webpage + .ts file from a link and repeats it, but the problem is it increases memory usage each time and never removes the old data. After 2 Hours it uses more than 2GB. Any ideas on how I can fix this issue?

I'm using "Leaf.Xnet" Library for requests and this is how I create my threads:

new Thread(new ThreadStart(WebHelper.Check)).Start();

Main code:

public static void Check()
{
 HttpRequest request = null;
 while (Form1.isRuning)
 {
 Application.DoEvents();
 try
 {
 request = new HttpRequest();
 if (!ProxyManager.updating)
 {
 switch (ProxyManager.curProxyType)
 {
 case ProxyManager.proxyType.http:
 request.Proxy = HttpProxyClient.Parse(ProxyManager.NextProxy(ProxyManager.proxyType.http));
 break;
 case ProxyManager.proxyType.socks4:
 request.Proxy = Socks4ProxyClient.Parse(ProxyManager.NextProxy(ProxyManager.proxyType.socks4));
 break;
 case ProxyManager.proxyType.socks5:
 request.Proxy = Socks5ProxyClient.Parse(ProxyManager.NextProxy(ProxyManager.proxyType.socks5));
 break;
 }
 }
 else
 {
 Thread.Sleep(2000);
 Check();
 }
 request.UserAgentRandomize();
 request.AddHeader(HttpHeader.Referer, "https://somesite.com");
 request.KeepAlive = true;
 request.ConnectTimeout = Form1.timeOut;
 request.Reconnect = true;
 string html = request.Get(Form1.link, null).ToString();
 string auth = html.Substring(",[{\"src\":\"", "\"");
 string sign = html.Substring("144p.apt?wmsAuthSign=", "\"");
 if (auth != null && sign != null)
 {
 string auth2 = "";
 foreach (char item in auth)
 {
 if (item != '\\')
 auth2 += item;
 }
 auth = auth2;
 string cdn = auth.Substring("https://", ".");
 string id = auth.Substring("video/", "-");
 if (cdn != null && id != null)
 {
 Random rnd = new Random();
 request.Get(auth);
 Form1.sended++;
 WriteStat();
 }
 html = null;
 auth = null;
 auth2 = null;
 sign = null;
 }
 }
 catch (HttpException)
 {
 Check();
 }
 catch (ProxyException)
 {
 Check();
 }
 }
}
3
  • You are calling method recursively in case of errors. That's weird. Commented Aug 13, 2020 at 8:39
  • @Sinatr it's in case of HTTP error or proxy error recalling it will use another proxy and it will be fixed Commented Aug 13, 2020 at 8:41
  • "Rapidlt" <=== is this a typo in the title? Commented Aug 13, 2020 at 11:17

1 Answer 1

2

I am not entirely sure if this will fix your problem but for each thread that you start, you pretty much call an infinite number of executions of Check(). Since Check contains a while loop, the thread will run whatever is in side forever anyway, and now you're calling the method again on top of it. This means that everything that was created in the scope of the Check method will not be garbage collected and will increase your memory.

Replace all calls to Check() with continue which will stop the execution in the while loop and start over.

Also, consider not using Threads, but instead use Tasks.

Also you do not dispose your HttpRequest.

answered Aug 13, 2020 at 8:43
Sign up to request clarification or add additional context in comments.

2 Comments

Well replacing Calls with continue; fixed the problem performance reduced a bit but it's for a better cause. and can you please guide me on how to dispose the HttpRequest I have no idea
If you are using a recent version of C# you can just put using in front of the variable declaration. Remove HttpRequest request = null; and put instead using var request = new HttpRequest(); where your're currently creating the new HttpRequest. Putting using on something will automatically take care of the disposable pattern. Note that you can manually dispose anything that is disposable (= does not get garbage collected automatically) by just caling .Dispose() on the object.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.