简体   繁体   English

For Loop - Out of Memory Exception

[英]For Loop - Out of Memory Exception

Actually,I got a requirement like save in Db the combinations of 9 digit number in encrypted format. 实际上,我有一个要求,比如save in Db以加密格式save in Db combinations of 9 digit numbercombinations of 9 digit number

So,I have used a very basic algorithm for encryption and thought of using for loop until 999,999,999 and save the records in DataTable and BulkCopy to SQL . 所以,我使用了一个非常基本的加密算法,并考虑使用for循环直到999,999,999 ,并将DataTableBulkCopy to SQL的记录保存BulkCopy to SQL

My Program goes like this : 我的程序是这样的:

DataTable DtData = new DataTable();
DtData.Columns.Add("ENCRYPTED_DATA", typeof(string));        

for (Int64 i = 1; i <= 999999999; i++)
{
    string Number = i.ToString("D9");

    string Encrypt = EncryptDecrypt.Encrypt(Number);

    DtData.Rows.Add(Encrypt);

    if (i % 100000 == 0)
    {
        GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
    }
}

But my problem is I'm getting out of memory exception after some time. 但我的问题是我在一段时间后out of memory exception
Is there a way to Garbage Collect or some other way and save the Memory Consumption? 有没有办法Garbage Collect或其他方式并节省内存消耗?
Actually the Code of GC.Collect is not at all reducing the Memory Usage as I can see in TaskManager. 实际上, GC.Collect代码完全没有减少内存使用量,正如我在TaskManager中看到的那样。

My PC RAM is 16GB and the approximate time taken for processing 8,000,000 records is 7 minutes . 我的PC RAM is 16GB ,处理8,000,000 records is 7 minutes所需的大概时间8,000,000 records is 7 minutes
After consuming 16GB it is giving out OutOfMemoryException as per TaskManager. 在消耗16GB之后,它根据TaskManager发出OutOfMemoryException
Is there a way to reduce the MemoryConsumption and make my forloop fully execute? 有没有办法减少MemoryConsumption并让我的forloop完全执行?

The line 这条线

GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);

does not free the data already in DataTable DtData . 不会释放DataTable DtData已有的DataTable DtData I don't know the size of strings that you are creating but you are creating thousands of strings and adding them to DataTable DtData . 我不知道你正在创建的字符串的大小,但是你创建了数千个字符串并将它们添加到DataTable DtData

These strings in DtData are never eligible for Garbage Collection within the scope of this loop. DtData中的这些字符串永远不会符合此循环范围内的垃圾收集。

You should periodically commit the numbers to the database as in the following 您应该定期将数字提交到数据库,如下所示

DataTable DtData = new DataTable();
DtData.Columns.Add("ENCRYPTED_DATA", typeof(string));        

for (Int64 i = 1; i <= 999999999; i++)
{
    string Number = i.ToString("D9");

    string Encrypt = EncryptDecrypt.Encrypt(Number);

    DtData.Rows.Add(Encrypt);

    //vary this number depending on your performance testing and application needs
    if (i % 100000 == 0)
    {
        //instead of this 
        //GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);


        //commit changes and refresh your DataTable

        DoSomeDatabaseCommitHere(DtData);
        DtData = new DataTable();

    }
}

You also can employ a Collection class - MSDN instead of DataTable for more options and asynchronous uploading. 您还可以使用Collection类 - MSDN而不是DataTable来获取更多选项和异步上载。

Your DtData is filling up and taking all your memory. 你的DtData正在填满你的所有记忆。

All the garbage collection in the world isn't going to help. 世界上所有的垃圾收集都无济于事。

Save your data now and then to the database and empty the DtData DataTable. 立即保存数据到数据库并清空DtData DataTable。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM