Thread: garbage collection

  1. #1
    Registered User joed's Avatar
    Join Date
    Mar 2004
    Posts
    59

    garbage collection

    The realloc question reminded me to ask about this. If memory is allocated/reallocated/freed many times (thousands) over, does the program's memory usage keep growing due to leftover garbage, or does the C runtime or operating system take care of it?

    Take the case of a program which compresses lines of text in memory, and decompresses/recompresses lines whenever changes are made (the benefit being memory savings, at the cost of speed.) Assuming garbage collection is automatic, how does it affect the performance of such programs?

    -Joe

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    When you call free(), that memory can be re-used in a future malloc / realloc / calloc call.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User joed's Avatar
    Join Date
    Mar 2004
    Posts
    59
    If I free 2 bytes, then never allocate another 2-byte chunk in my program, does the OS wait until the program ends to get the 2 bytes back? Or can another program allocate that 2-bytes?

    -Joe

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The program (in the form of the C run-time library) will typically keep that memory until the end of the program.

    If the program doesn't touch that memory for some period of time, the OS will likely swap that memory out to disk so it can be re-used by other programs.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    In linux, it postpones allocating memory until the last possible second. So if you allocate some memory and only use some of it, the linux kernel will not allocate it until you go to use it. From what I am told though, there is no "runtime" garbage collector like some other languages.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. garbage collection enabled c++
    By manav in forum C++ Programming
    Replies: 42
    Last Post: 02-15-2008, 01:42 PM
  2. Garbage Collection is not so complicated
    By Nalpdii in forum C Programming
    Replies: 2
    Last Post: 10-07-2007, 11:34 PM
  3. C++ Garbage Collection
    By vaibhav in forum C++ Programming
    Replies: 1
    Last Post: 11-27-2005, 10:26 AM
  4. Garbage Collection
    By Orborde in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2005, 11:18 PM
  5. SDL: Garbage collection (?) screwing me
    By Brian in forum Game Programming
    Replies: 4
    Last Post: 05-08-2005, 09:13 AM