Reduced memory usage when reading data from datatable

I tried this way to read data from a data table, is there any other better way to store data in a list (to reduce memory usage).

Code:

foreach (System.Data.DataRow row in table.Rows) { Myclassdef data = new Myclassdef(); data.Data = new Dictionary<string, object>(); foreach (DataColumn columninfo in table.Columns) { object value = row[columninfo.ColumnName]; Myclassdef.Data.Add(columninfo.ColumnName, value); } } 
+4
source share
2 answers

Are you sure the memory problem is caused by DataTable ? You map everything in a custom class to a dictionary for each line. Thus, you need more than double the memory than using the DataTable .

However, if you are reading values ​​from a database and first loading everything into a DataTable to be able to loop everything to create your own class with a dictionary, then there is a better way in terms of memory Power consumption:

  • use DataReader to read these values ​​from the database. These are data streams without having to store anything in memory

     var list = new List<Myclassdef>(); using (var con = new SqlConnection(Settings.Default.ConnectionString)) { using (var cmd = new SqlCommand("SELECT ... WHERE Col1=@param1 ", con)) { cmd.Parameters.AddWithValue("@param1", "value1"); // ... con.Open(); using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { Myclassdef data = new Myclassdef(); data.Data = new Dictionary<string, object>(); for (int i = 0; i < reader.FieldCount; i++) { data.Data.Add(reader.GetName(i), reader[i]); } list.Add(data); } } } } 
+5
source

Are you asking if there is a more efficient approach, or are you asking if there is a more efficient way to store memory?

I am very skeptical of any other memory option, and that will give you a noticeable difference. Lists and vocabulary may not be the most efficient storage option, but they are also not sows.

If you want to know if there is a better approach that depends on what you are trying to do. If this data should be displayed to the user, then the conclusion is usually made about what becomes the visible size of the table (so that the scroll bars behave as expected), and only some of the immediately previous and subsequent entries are cached only from the viewport. As the user navigates while looking at the table, data is loaded as needed.

+3
source

Source: https://habr.com/ru/post/1494230/


All Articles