Search code examples
memorymemory-leaksstorageheap-memorydefinition

What is Semantical Memory Leak?


I understand the definition of a Memory Leak but couldn't find anything referred to Semantical or Semantic Memory Leak and the differences to memory leak.

Example for a memory leak:

#include <stdlib.h>

void function_which_allocates(void) {
    /* allocate an array of 45 floats */
    float *a = malloc(sizeof(float) * 45);

    /* additional code making use of 'a' */

    /* return to main, having forgotten to free the memory we malloc'd */
}
int main(void) {
    function_which_allocates();

    /* the pointer 'a' no longer exists, and therefore cannot be freed,
     but the memory is still allocated. a leak has occurred. */
}

Solution

  • Definition (Semantical garbage)

    A variable which the program will never use again, but still keeps a reference to it, is called semantic garbage.

    class Huge {
        Huge() { // Constructor:
                 // Allocates lots of data and stores
                 // it in the newly created object
        }
    }
    
    void f() {
        Huge semanticGarbage = new Huge();
        heavy.computation(new Indeed(100));
        System.exit(1);
    }
    

    All sophisticated GC algorithms contend in vain against semantic garbage.

    Reference: Technion CS 234319: Programming Languages Course

    (Lecture) Chapter 5 Storage 5.5 Automatic memory management - Semantical memory leak