Allocate / free memory with Visual Studio C++

Ollivier TARAMASCO 0 Reputation points
2025-06-29T07:22:27.0766667+00:00

I have a very simple program

int main(void)

{ double* myVect = new double[10000000];

wait(2); // wait for 2 seconds

delete[] myVect;

wait(2);

return 0;

}

I execute this program in Debug mode, put a breakpoint on "return 0" and look at the diagnostic tools (sorry I've a French version of Visual Studio 2022).

OutilsDignostic

Why the memory isn't released?

Developer technologies | C++
Developer technologies | C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
{count} votes

2 answers

Sort by: Most helpful
  1. RLWA32 51,456 Reputation points
    2025-06-29T09:00:37.45+00:00

    I increased the interval between allocation, free and termination to make memory changes more visible in the diagnostic session. I also took snapshots before allocation, before free and after free. This is what was displayed.

    Memory

    So the memory is being freed when delete is called but the display you are looking at doesn't clearly reflect the activity due to the short lag between free and program termination.

    1 person found this answer helpful.
    0 comments No comments

  2. Bruce (SqlWork.com) 82,061 Reputation points Volunteer Moderator
    2025-06-30T16:19:29.9466667+00:00

    when you ask for an allocation that is larger than the heap has free memory, it requests more process memory form the O/S, thus going the process memory size. when you release memory to the heap (delete), the deleted memory is marked as free, but not returned to the O/S, so the process memory size does not change. You do have lots of free heap memory.

    0 comments No comments

Your answer

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