0

I have learn in the few past days the issue with memory overcommitment (when memory overcommit is activated, which is usually a default), which basically means that:

 void* p = malloc(100);

the operative system gives you 100 contiguous (virtual) addresses taken from the (virtual) address space of your process, whose total range is OS-defined. Since that memory region has not been initialized yet, it doesn't count as ocuppied storage from a system-wide point of view, so it's a pure abstraction besides consuming your virtual addresses.

 memset(p, 0, 5);

That uses the first 5 bytes, so from the point of view of the OS, your process ocuppies now 5 extra bytes, and so the system has 5 bytes less of free storage. You have still 95 bytes of uninitialized storage.

The system only crash or start killing processes when the combined ocuppied storage (initialized) of every process is beyond what the OS can hold.

If my understanding is right at this regard, is there a way to "des-"initialize a region of memory when you are done with it, in order to increase the system-wide free space, without loosing the address region requested by malloc or aligned_malloc (so you don't increase fragmentation over time)?

The purpose of this question is more theoretical than practical and not about actually "freeing memory", but about freeing memory while conserving already assigned virtual addresses.

Source about the difference between requesting virtual addresses and ocuppying storage: https://www.win.tue.nl/~aeb/linux/lk/lk-9.html#ss9.6

PD: With knowing it for Linux to fill my curiosity I'm ok.

ABu
  • 10,423
  • 6
  • 52
  • 103
  • The system does this by itself with rarely used memory if it runs low. – tkausl Aug 26 '19 at 18:47
  • 1
    You don't need to care. Trust the OS to page out memory that was not used recently. – Jeffrey Aug 26 '19 at 18:50
  • @Jeffrey This is not about pagination, because it's not about RAM. The system crashes or starts a killing sequence in my understanding when it cannot hold more memory in both RAM and swap. – ABu Aug 26 '19 at 18:55
  • The total amount of RAM + swap will *not* be affect by memory accesses (like the memset above). the total RAM + swap will always be the total allocated memory + some rounding up. If you think not, please link doc to such overcommitment – Jeffrey Aug 26 '19 at 19:01
  • 1
    As long as you `delete` or `free` memory you no longer use, you rarely have to care. The OS will do the right thing most of the time. If you do *actually* have to care, then disabling overcommit *is* an option and in C++ you can catch `std::bad_alloc`. – Jesper Juhl Aug 26 '19 at 19:01
  • @JesperJuhl If I `delete` or `free` memory I'm loosing the requested address region and thus possibly increasing fragmentation, so reducing performance. – ABu Aug 26 '19 at 19:07
  • @Peregring-lk If you are worring about that you are most likely overthinking the problem. – Jesper Juhl Aug 26 '19 at 19:09
  • @Yunnosch https://www.win.tue.nl/~aeb/linux/lk/lk-9.html#ss9.6 – ABu Aug 26 '19 at 19:10
  • @Peregring-lk I've worked on several production quality code bases (that actually shipped) - on multiple platforms (Linux, Windows, AiX, Solaris) and I have (honestly) *never* had to worry about the behaviour of my OSs allocator. – Jesper Juhl Aug 26 '19 at 19:16
  • 1
    See if Linux `madvise(MADV_DONTNEED)`, `madvise(MADV_FREE)` or `posix_madvise(POSIX_MADV_DONTNEED)` do what you want. But don't meddle with malloced memory! Use madvise only on the pages you have `mmap`-ed manually! – Igor G Aug 26 '19 at 19:33
  • @IgorG That's a good info!! Thanks. Reading the man pages now. – ABu Aug 26 '19 at 19:37
  • 1
    @igor, IMO, you've just handed someone and over-powered gun so they could blow their own foot off. – Jeffrey Aug 26 '19 at 19:46
  • 1
    @Jeffrey, I see no harm in blowing virtual feet. That's one way to get experience. – Igor G Aug 26 '19 at 19:53

1 Answers1

0

No, there is no way.

On most systems, as soon as you allocate memory, it counts towards RAM or swap.

As your link shows, on Linux, you may need to access the memory once so that the memory actually gets allocated. But as soon as you do, the system must keep that memory available somewhere, in case you access it later.

The way to tell the system you are done with the memory is to actually free it.

Jeffrey
  • 11,063
  • 1
  • 21
  • 42