4

Let's say we've got some code like this:

int main() {
    FILE *f_conf = fopen("conf.conf");
    /* File Open Error Handling */

    char *buf = malloc(128);
    /* Malloc Error Handling */

    fgets(buf, 128, f_conf);
    fclose(f_conf);

    pid_t pid = fork()
    /* Fork Error Handling */

    if (pid) {
        free(buf); /* Should this free be called? */
        return 0;
    }

    while (1) {
        /* Do Stuff */
    }

    return 0;
}

In general, I prefer to free memory prior to returning, since any impact to impact performance is minimal, and it helps with automated tooling to detect memory leaks. That said, the performance / best practices are less clear to me here, as it makes little sense to run these tools on a daemon. This leads me to two questions:

  1. Would freeing memory (such as *buf) of a parent process prior to return affect performance of the child? I'm thinking there could be some (either positive or negative) by the way copy-on-write is implemented.
  2. Is it considered good practice for the parent to free leftover resources?
3
  • 1
    This is actually a really interesting question. freeing the memory would probably cause it to get copied (depending on how the allocator is implemented, but most of the ones I know store metadata right before the allocated memory which will get updated by free), which ironically will use up extra physical memory.
    – Andrew Sun
    Commented May 28, 2019 at 23:45
  • @AndrewSun Yeah, I went back and forth on it too - my current thought is that it'll actually have minimal impact, because a kernel could avoid the copy if the parent frees (since most kernels only copy the page table to start), though I'm not sure what the implications of that would be according to the applicable standards, and/or if the kernel actually does that.
    – Tyzoid
    Commented May 28, 2019 at 23:46
  • 1
    it would depend on how the libc memory allocator is implemented and how large the allocation is; a smart allocator might skip touching the metadata and directly return the memory to the kernel (skipping the copy) if it can release an entire page, a not-so-smart allocator might touch the metadata and then return the memory to the kernel (which would do a copy, then free it)
    – Andrew Sun
    Commented May 28, 2019 at 23:49

1 Answer 1

1

I consider it good practice to limit the allocation of a thing to exactly the executable region that it is valid for. To me, the free is part of the documentation -- this thing is no longer valid or accessible. In all things programming, conciseness yields clarity; and clear programs can be improved and optimised; incoherent ones cannot.

In terms of performance across a fork, if you are down to measuring the page-cache effect, you are probably in a very dark place. Fundamentally, the process' pages will point to the same underlying page; if one relinquishes the page, the other gets it free-of-charge [ no point in copying if refcount == 1 ]. Realistically, all that happens is a few linked list nodes get updated, and you can't really measure it.

That said, if you are carrying around an allocation for an entire DVD or something, yeah, you might consider mapping it shared.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.