Search code examples
c#.netmultithreadingwinformsxnet

Rapidly Increasing Memory usage C#


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();
        }
    }
}

Solution

  • 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.