Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programming languages without garbage collector: where does the garbage go?

I'm currently studying algorithms and advanced data structures; since I'm familiar with C and it does provide a great level of control above implementation and pointer usage I'm using it to test the understanding of the subject so far.

When testing structures that need dynamic things like lists and trees I asked myself: since C doesn't have a garbage collector, if I don't call the free() function in order to deallocate all the variables I dynamically allocate, where does that memory go?

Other related questions incude (sorry for misusing some terms, I don't have much experience in low level abstraction):

  • Does the compiler use the actual hard drive resources (like a variable is in the x record of my drive) or it "instantiates" a portion of virtual memory to compile and run my programs?

  • Do I have lots of lists, trees and graphs in my hard drive, all of them involving counts from 0 to 100 or the strings "abcd" and "qwerty"?

  • Does the OS recognize said data as garbage or I'm stuck with this junk forever until I format the drive?

I'm really curious about it, I never went below the C level of abstraction.

like image 236
alemootasa Avatar asked Apr 06 '26 00:04

alemootasa


1 Answers

since C doesn't have a garbage collector, if I don't call the free() function in order to deallocate all the variables I dynamically allocate, where does that memory go?

This is not (and cannot really be) defined by the C11 standard (read n1570).

However, let's pretend you run an executable produced by some C compiler on some familiar operating system (like Linux or Windows or MacOSX). Actually you are running some process which has some virtual address space.

The virtual memory and paging subsystem of the operating system kernel would put most useful pages - the resident set size - of that virtual address space in RAM and configure the MMU; read about demand paging & thrashing & page cache & page faults.

When that process terminates (either by nicely exiting or by some abnormal situation, like a segmentation fault) the operating system is releasing every resources used by your process (including the virtual address space of that process).

Read Operating Systems: Three Easy Pieces for much more.

However, if you don't have any operating system, or if your OS or processor don't support virtual memory (think of some Arduino board) things can be widely different. Read about undefined behavior and runtime systems.

On Linux, you can query the address space of a process of pid 1234 by using proc(5). Run in a terminal cat /proc/1234/maps or use pmap(1). From inside the process, read /proc/self/maps as a sequential file. See this.

You could also study the source code of open source standard libraries like GNU libc (above syscalls(2) on Linux) or of musl-libc, or use strace(1) to understand what system calls are done.

like image 151
Basile Starynkevitch Avatar answered Apr 08 '26 15:04

Basile Starynkevitch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!