CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2001
    Posts
    4

    Memory allocation

    A basic question :-

    An application allocates memory (from the heap) by using new. It terminates without deleting the assigned memory (from the heap). Now does that mean the OS will not use that memory at all which was allocated to the application for some other usage.

    Thanks



  2. #2
    Join Date
    Jul 2001
    Location
    Mumbai,India
    Posts
    382

    Re: Memory allocation

    Yes, the OS won't use that memory, since it has not been de-allocated. It will only be able to use that memory when the system is rebooted.

    Regards,
    The Beret.


  3. #3
    Join Date
    Feb 2001
    Location
    Sydney, Australia
    Posts
    1,909

    Re: Memory allocation

    In Windows all memory is owned by process. Then process is gone ALL memory was freed.

    Please - rate answer if it helped you
    It gives me inspiration when I see myself in the top list =)

    Best regards,

    -----------
    Igor Soukhov (Brainbench/Tekmetrics ID:50759)
    [email protected] | ICQ:57404554 | http://soukhov.com

    Member of Russian Software Developer Network http://rsdn.ru
    Best regards,
    Igor Sukhov

    www.sukhov.net

  4. #4
    Join Date
    Jul 2001
    Location
    Mumbai,India
    Posts
    382

    Re: Memory allocation

    Igor,

    Can you elaborate a bit on your statement?
    What happens if the Process is not terminated and the malloc or new is being done in a loop many times? Will it then lead to a memory leak? And does finally terminating the process, return that memory back to the O.S.?
    And what kind of behaviour will such a program exhibit in an UNIX system?

    Finally, can you provide some links to sites explaining the Memory Organisation in a Windows system.

    Thanks,
    The Beret.


  5. #5
    Join Date
    Jul 2001
    Posts
    4

    Re: Memory allocation

    Igor

    Do you mean the memory will free up in Windows and not in UNIX when the application/process terminates? Please comment

    Thanks


  6. #6
    Join Date
    Feb 2000
    Location
    Phoenix, AZ
    Posts
    450

    Re: Memory allocation

    This is not true. When the application is terminated all memory that the application has allocated will be freed up and the OS will use it again. The application frees all memory when the application closes even if delete has not been called


  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Memory allocation

    Igor's post is correct for Windows, but if this is your own program, please do not make it a habit of relying on the OS to clean up the memory.

    The OS may or may not clean up the memory when the application terminates. This all depends on the OS. For example, 16-bit DOS/Windows will not clean up the memory. I don't know about the various UNIX OSes.

    Anyway, relying on the OS to clean up the memory that you allocated is sloppy programming. It would (or should) never pass a code review by other fellow programmers. What if you are allocating the memory in a loop, and you slowly are exhausting the memory? Your app dies before the application reaches the end of the loop.

    What if you reuse the code that leaks the memory in another portion of your app, where the app does not immediately terminate? Again, you now have a memory leak.

    Always make sure to investigate and plug any memory leak in your program, regardless of whether the OS cleans up at the end.

    Regards,

    Paul McKenzie


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured