小编use*_*380的帖子

HttpClientHandler/HttpClient内存泄漏

我有10到150个长生命类对象,可以调用使用HttpClient执行简单HTTPS API调用的方法.PUT调用示例:

using (HttpClientHandler handler = new HttpClientHandler())
{
    handler.UseCookies = true;
    handler.CookieContainer = _Cookies;

    using (HttpClient client = new HttpClient(handler, true))
    {
        client.Timeout = new TimeSpan(0, 0, (int)(SettingsData.Values.ProxyTimeout * 1.5));
        client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", Statics.UserAgent);

        try
        {
            using (StringContent sData = new StringContent(data, Encoding.UTF8, contentType))
            using (HttpResponseMessage response = await client.PutAsync(url, sData))
            {
                using (var content = response.Content)
                {
                    ret = await content.ReadAsStringAsync();
                }

            }
        }
        catch (ThreadAbortException)
        {
            throw;
        }
        catch (Exception ex)
        {
            LastErrorText = ex.Message;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

运行这些方法2-3个小时后,包括通过 …

c# memory garbage-collection memory-leaks httpclient

20
推荐指数
1
解决办法
9419
查看次数

按Value排序ConcurrentDictionary

我可以像这样按值对ConcurrentDictionary进行排序:

static ConcurrentDictionary<string, Proxy> Proxies = 
    new ConcurrentDictionary<string, Proxy>();

Proxies.OrderBy(p => p.Value.Speed);
Run Code Online (Sandbox Code Playgroud)

这很好,除了我想将新的重新排序的列表设置为字典,有效地排序字典本身而不是仅仅接收已排序项的结果列表.

我尝试做这样的事情,但没有运气 - 字典仍然无序后:

Proxies = new ConcurrentDictionary<string,Proxy>(
    Proxies.OrderBy(p => p.Value.Speed));
Run Code Online (Sandbox Code Playgroud)

似乎这样做对字典没有影响.我也尝试将OrderBy结果转换为一个新的var,认为它可能对委托产生影响但仍然没有运气.

如何重新订购此ConcurrentDictionary,然后强制字典成为OrderBy的重新排序结果?

c# parallel-processing concurrency concurrent-programming c#-4.0

12
推荐指数
1
解决办法
1万
查看次数

正则表达式导致C#中CPU使用率异常高

在最近阅读了一个被称为"灾难性回溯"的现象之后,似乎我自己的正则表达式模式正在引发某种CPU问题.我使用这个表达式来扫描100k-200k字符的大型HTML字符串.该模式匹配IP:端口格式的IP地址(例如1.1.1.1:90).模式如下:

private static Regex regIp = new Regex(@"(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\." +
        @"(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4]" +
        @"[0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}" +
        @"[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])\:[0-9]{1,5}", 
        RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.CultureInvariant);
Run Code Online (Sandbox Code Playgroud)

表达式使用如下:

MatchCollection matchCol = regIp.Matches(response);

foreach (Match m in matchCol)
{ 
    doWorkWithMatch(m);
}
Run Code Online (Sandbox Code Playgroud)

通过这个正则表达式模式运行大约100个字符串后,它开始扼杀计算机并使用99%的CPU.是否有更合理的方法来构造此表达式以减少CPU使用并避免回溯?我不确定回溯是否正在发生,或者它是否只是同时执行正则表达式评估的太多线程的问题 - 所有输入都是受欢迎的.

.net c# regex

0
推荐指数
1
解决办法
906
查看次数