Thread: Intentional memory leakage. Always a misuse?

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    228

    Intentional memory leakage. Always a misuse?

    I guess it's more of a general question about processes in an operating system, but it also fits into here, I think.

    Suppose I run a program that allocates heap memory, never frees it, but then exits.
    Correct me if I'm wrong, but whenever a process exits, the OS deallocates (almost) every memory chunks that are associated with it; including its heap, stack, and PCB. So, in that case, harm can only be done by programs that 'run in background' for a long period of time, and consume memory they do not actually use or need.
    Putting aside the risk of acquiring a bad habit, in terms of memory usage, what is the harm of not freeing heap memory in programs that are designated to perform a specific (small) task?

    I mean, for something as simple as:

    Code:
    int main(int argc, char* argv[]) {
    
        // do some specific task that consumes heap memory
    
        return 0;
    }

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> what is the harm ...
    You risk acquiring a bad habit.

    That aside, a modern OS will "reclaim" the memory cleanly.

    gg

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    If you do not know when to free allocated memory and leave it upto OS to clean after you - in most cases it means your program architecture has some basic flaws that in big project will make your software unmanageable.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    I can't speak much for the risk beyond what you've already said, but I'll tell you that I assume no resources are used with care when seeing one resource used without care.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is this small function an example of memory leakage?
    By Vespasian in forum C Programming
    Replies: 12
    Last Post: 04-25-2012, 01:04 PM
  2. How can detect memory leakage?
    By pronetin in forum C Programming
    Replies: 9
    Last Post: 10-07-2010, 06:37 AM
  3. Does this code has memory leakage?
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 04-08-2008, 08:13 PM
  4. Intentional memory leak?
    By axlton in forum C++ Programming
    Replies: 12
    Last Post: 03-26-2004, 06:57 PM
  5. memory leakage?
    By xagiber in forum C++ Programming
    Replies: 1
    Last Post: 02-04-2002, 02:57 PM