I am running Fedora 26
.
This is for a very strange assignment given by my algorithms professor. The assignment says:
Memory fragmentation in C:
Design, implement, and execute a C-program that does the following: It allocates memory for a sequence of3m
arrays size 800,000 elements each; then it explicitly deallocates all even-numbered arrays and allocates a sequence ofm
arrays of size 900,000 elements each. Measure the amounts of time your program requires for the allocation of the first sequence and for the second sequence. Choosem
to exhaust almost all of main memory available to your program."
The overall goal of this is to fragment memory then request slightly more than what is available as a contiguous chunk, forcing the operating system to compact or defragment memory.
In class I asked how we should go about doing this since memory is visualized and not actually contiguous, to which he replied: "Well you'll have to turn [virtual memory] off." Some other students asked in class how we should know when we have hit this "garbage collection," and he said that: "The timing for the second allocation should be greater than the first because of the time taken by garbage collection"
After searching around a bit, the closest thing I could find to disabling virtual memory was to disable swap memory with swapoff -a
. I disabled my desktop environment and compiled and ran my program from the native terminal (to avoid possible interference from other processes, especially a heavy one like the Desktop Environment). I did this and ran my program with increasing m
until I reached a point where the timing for the second allocation was greater than the first.
I ran the program with increasing m
and eventually found a point where the time for the second allocation was more than the time for the first allocation. Along the way however I hit a point where the process was killed before the second allocation. I checked dmesg
and saw that it was killed by oom
-killer. I found and read several articles about about oom
-killer and discovered that you could disable over allocation of memory by the kernel.
I did this and ran my program again, only this time I was not able to find a m
such that the timing of the second was higher than the first. Eventually with larger and larger m (though much smaller than when overallocation was enabled) malloc would fail and my program would terminate.
I have three questions, the first of which isn't really that important:
Is garbage collection the correct term for this? My professor is very adamant in saying that this is garbage collection, but I was under the assumption that garbage collection was something done by programming languages and that this would be considered more defragmenting.
Is compaction like he wants possible on a linux system?
Why was I able to reach a point where the time for the second allocation was higher than the first when I disable swap but still had overallocation of memory enabled? Did compaction actually take place? If so why was I not able to hit a point where compaction happened after I disabled overallocation of memory?