我在内存分配方面遇到了一些问题。 例如:
long mem1 = GC.GetTotalMemory(true);
//mem1 = 2954648
using(StreamReader sr = File.OpenText(url_File))
{
// File size ~ 350MB
while (!sr.EndOfStream)
{
AkcjaSeries.Data.Add(AkcjeRecord.Parse(sr.ReadLine(), columnDescription, datePattern, separators));
linesCount++;
if (linesCount%100000 == 0)
{
textBox.Text = linesCount.ToString();
}
}
}
textBox.Text = linesCount.ToString();
long mem2 = GC.GetTotalMemory(true); // = 760587356
GC.Collect();
GC.WaitForFullGCComplete(); // I use all this method
GC.WaitForPendingFinalizers(); // beceuse i realy need free memory
GC.WaitForFullGCApproach(); //
long mem3 = GC.GetTotalMemory(true); // = 760587356 (the same as mem2)
和班级看起来像:
public class AkcjaSeries : ObservableCollection<AkcjeRecord>
{
public static AkcjaSeries Data = new AkcjaSeries();
public const int MaxElementsInChart = 100;
private String name;
public String Name
{
get { return name; }
set { name = value; }
}
}
和
public class AkcjeRecord : DependencyObject
{
public static readonly string[] Separators = { "\t", " " };
public static readonly DependencyProperty DateProperty = DependencyProperty.Register("Date", typeof(DateTime), typeof(AkcjeRecord));
public static readonly DependencyProperty CostProperty = DependencyProperty.Register("Cost", typeof(double), typeof(AkcjeRecord));
public DateTime Date
{
get { return (DateTime)GetValue(DateProperty); }
set { SetValue(DateProperty, value); }
}
public double Cost
{
get { return (double)GetValue(CostProperty); }
set { SetValue(CostProperty, value); }
}
public static AkcjeRecord Parse(string line, int[] columnDestination, string dataPattern, string[] separators )
{
AkcjeRecord akcjaRecord = new AkcjeRecord();
string[] dividedLine;
dividedLine = line.Split(separators, StringSplitOptions.RemoveEmptyEntries);
akcjaRecord.Cost = Double.Parse(dividedLine[columnDestination[0]], CultureInfo.InvariantCulture);
string dateAndTime = dividedLine[columnDestination[1]] + dividedLine[columnDestination[2]];
DateTime date;
TimeSpan time;
date = DateTime.ParseExact(dividedLine[columnDestination[1]], dataPattern, CultureInfo.InvariantCulture);
time = TimeSpan.Parse(dividedLine[columnDestination[2]]);
date = date.Add(time);
akcjaRecord.Date = date;
return akcjaRecord;
}
@QUESTION
为什么当我打电话给methond 所以GC.Collect(); 我对此没有任何影响。 (我写评论需要多少内存程序) 我做错了什么? 顺便说一下......我每行只需要60%的数据 文件行示例:
20100729 09:34:15.948 70.07 167022 319
(我只解析1,2,3列)