Skip to main content
 首页 » 编程设计

c#之调用 Web 服务时服务器端内存泄漏

2024年01月01日40arxive

我有两个非常简单的 web 服务方法,我使用 json 每秒调用一次 IIS存在内存泄漏,w3wp进程每秒占用300-400k 可以做些什么来阻止泄漏?

方法如下:

[WebMethod] 
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)] 
public List<StationStatus> GetAllStationsValues(string networkID ,string stationIDs) 
{ 
    List<StationStatus> stationStats = new List<StationStatus>(); 
    foreach (string stationID in stationIDs.Split(',')) 
    { 
        if (Application[networkID + "-" + stationID] != null) 
        { 
            string[] vals = (string[])Application[networkID + "-" + stationID]; 
 
            stationStats.Add(new StationStatus() { NetworkID = networkID, StationID = stationID, ComponentName = vals[0].Split(':')[0], LatestValue = vals[0].Split(':')[1], Status = "0" });//////// 
        } 
    } 
 
    return stationStats; 
} 
 
[WebMethod] 
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)] 
public string GetValue(string networkID, string stationID) 
{ 
    int sec = DateTime.Now.Second; 
 
    if (Application[networkID + "-" + stationID] == null) 
    { 
        return ""; 
    } 
 
    string[] compValues = (string[])Application[networkID + "-" + stationID]; 
 
 
    string returnValue = ""; 
 
    foreach (string compValue in compValues) 
    { 
        returnValue += compValue + ","; 
    } 
 
    return returnValue.TrimEnd(','); 
} 

请您参考如下方法:

尝试使用 StringBuilder用于附加到您的 returnValue 字符串的类。