How do I read a large file from disk to database without running out of memory

后端 未结 6 842
野性不改
野性不改 2021-01-13 14:53

I feel embarrassed to ask this question as I feel like I should already know. However, given I don\'t....I want to know how to read large files from disk to a database witho

6条回答
  •  清酒与你
    2021-01-13 15:31

    I think you may have a red herring with the size of the data. Every time I come across this problem, it's not the size of the data but the amount of objects created when looping over the data.

    Look in your while loop adding records to the db within the method button3_Click(object sender, EventArgs e):

    TextReader tr = GetData();
    using (CsvDataReader csvData = new CsvDataReader(tr, '\t'))
    

    Here you declare and instantiate two objects each iteration - meaning for each chunk of file you read you will instantiate 200,000 objects; the garbage collector will not keep up.

    Why not declare the objects outside of the while loop?

    TextReader tr = null;
    CsvDataReader csvData = null;
    

    This way, the gc will stand half a chance. You could prove the difference by benchmarking the while loop, you will no doubt notice a huge performance degradation after you have created just a couple of thousand objects.

提交回复
热议问题