private void WriteFile2(CMyTask task) { string filename = string.Format("{0}\\{1}", TestDir, task.Name); WaveFileWriter writer = new WaveFileWriter(filename, new WaveFormat(8000, 16, 1)); foreach (var item in task.BufferList) { writer.Write(item, 0, item.Length); } //偷看一下task與其property的generation : 通通都是generation 2 (包括task自己) //writer是generation 0 //if (Counter % 1000 == 0) //{ // ShowGeneration(task); // ShowGeneration(task.Name); // ShowGeneration(task.BufferList); // ShowGeneration(writer); //} writer.Close(); writer.Dispose(); writer = null; //GC似乎只會自動回收 generation 最新的那層 //所以這函式內要是有做new,傳入的那個List就不會被快速回收到,要等很久 //傳入的參數跨thread,測驗過它的generation==2 if (Counter % 1000 == 0) { GC.Collect(2); } Interlocked.Decrement(ref Counter); }
private void ThreadConsumer() { while (Counter > 0) { //同一條thread的記憶體似乎都是同一代generation.... CMyTask task = null; if (TaskQueue.TryDequeue(out task)) { WriteFile2(task); } } }
private void DoProducerTest(int times) { for (int i = 0; i < times; i++) { CMyTask task = new CMyTask(); for (int j = 0; j < 50 * 3; j++) { var data = new byte[720]; task.BufferList.Add(data); } Interlocked.Increment(ref Counter); TaskQueue.Enqueue(task); } System.Threading.Thread consumer = new Thread(ThreadConsumer); consumer.Start(); }