garbage collection

This is a discussion on garbage collection within the C Programming forums, part of the General Programming Boards category; The realloc question reminded me to ask about this. If memory is allocated/reallocated/freed many times (thousands) over, does the program's ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 12: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, 09: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21