일부 응용 프로그램에서 메모리가 누출되는 문제가 있습니다. 이것은 오랜 시간 동안 실행될 때 진짜 문제가됩니다. AddFeature
를 호출 GC에서 DataRow를 수집하지 못했습니다.
public class FeaturesDataSource{
// Table used only for schema hosting
DataTable selectedFeaturesDataTable;
IDictionary<uint, MyFeatureType > _selectedFeaturesCache =
new Dictionary<uint, MyFeatureType>();
// Create table according to a defined schema...
// Adds or replaces an existing feature
public void AddFeature(int featureID, MyFeatureType featureValue) {
DataRow row = selectedFeaturesDataTable.NewRow();
row["ID"] = featureID;
row["Content"] = featureValue;
if (_selectedFeaturesCache.ContainsKey(featureID) {
_selectedFeaturesCache.Remove(featureID);
}
_selectedFeaturesCache.Add(featureID, row);
}
}
이 방법에서 볼 수 있듯이
는, 테이블의 스키마에 따라 새로운 데이터 행을 만들고 동일한 ID를 가진 기존 데이터 행을 대체 나는 기본적으로 다음과 같은 클래스가 있습니다. 내 응용 프로그램이 1 개체/초의 속도로 유형MyFeatureType
의 객체를 생성하고 동일한 ID 매번로
AddFeature
를 호출
// This data source gets updated in the following way
public void MethodCalledEverySecond(MyFeatureType featureValue){
// This data source contains only one object of type MyFeatureValue,
// replace the existing one by specifying a constant ID
myFeatureDataSource.AddFeature(1, featureValue);
}
이 항상 새 것으로 기존 데이터 행을 교체해야합니다. 런타임에 VS에 항상 1과 동일한 _selectedFeaturesCache
사전 수가 표시되지만이 경우 !dumpHeap -stat
은 항상 MyFeatureType
유형의 객체가 증가하는 것을 보여줍니다. 여기서 메모리는 항상 1과 같아야합니다. 여기에 잘못된 것이 있습니까? selectedFeaturesDataTable.NewRow()
은 이전 데이터 행에 대한 참조를 유지하여 GC가 수집하는 것을 차단합니까? 당신이 새 행을 만들 NewRow를 사용하면 NewRow 방법에 대한 설명서에 따르면
왜 MyFeatureType 개체가 많지 않아야합니까? AddFeature를 호출하기 전에이를 생성 할 수 있습니다. – Strelok
예, 클라이언트 요구 사항 인 기존의 개체를 대체하기 위해 개체를 만듭니다. – GETah
개체가 뿌리인지 여부를 확인 했습니까? ! gcroot {id} – PHeiberg