VS Diagnostic Tools Shows that the memory consumed while creating a DataTable is not released even after GC.Collect()

Muppala Keerthi 1 Reputation point
2021-06-10T11:08:28.903+00:00

VS Diagnostic Tools Shows that the memory consumed while creating a DataTable is not released even after GC.Collect().

Code Snippet:

using (DataTable table = new DataTable())
{
for (int column = 0; column < 1024; column++)
{
table.Columns.Add("Name" + column);
}
for (int row = 0; row <= 75000; row++)
{
table.Rows.Add();
for (int column = 0; column < 1024; column++)
{
table.Rows[row][column] = "Kim" + row + column;
}
}
}
GC.Collect();

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,399 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Duane Arnold 3,216 Reputation points
    2021-06-10T15:19:38.767+00:00
    0 comments No comments

  2. Timon Yang-MSFT 9,606 Reputation points
    2021-06-11T07:22:19.263+00:00

    GC.Collect() cannot guarantee that all memory will be collected.

    Use this method to try to reclaim all memory that is inaccessible. It performs a blocking garbage collection of all generations.

    All objects, regardless of how long they have been in memory, are considered for collection; however, objects that are referenced in managed code are not collected.

    You can also take a look at the discussion on this issue in this post:

    .NET Garbage Collection behavior (with DataTable obj)


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.