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();
}
}
}
-
You are calling method recursively in case of errors. That's weird.Sinatr– Sinatr2020年08月13日 08:39:17 +00:00Commented 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 fixedfred the dud– fred the dud2020年08月13日 08:41:45 +00:00Commented Aug 13, 2020 at 8:41
-
"Rapidlt" <=== is this a typo in the title?Theodor Zoulias– Theodor Zoulias2020年08月13日 11:17:16 +00:00Commented Aug 13, 2020 at 11:17
1 Answer 1
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()withcontinuewhich 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.
2 Comments
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.Explore related questions
See similar questions with these tags.