Hacker News new | ask | show | jobs
by smlckz 1633 days ago
> In C, a read from an uninitialized variable is an unspecified value and is allowed to be any value each time it is read. This is important, because it allows behavior such as lazy recycling of pages: for example, on FreeBSD the malloc implementation informs the operating system that pages are currently unused, and the operating system uses the first write to a page as the hint that this is no longer true. A read to newly malloced memory may initially read the old value; then the operating system may reuse theunderlying physical page; and then on the next write to a different location in the page replace it with a newly zeroed page. The second read from the same location will then give a zero value.

What? What is the benifit of such behaviour?

What does other OSes do in this regard?

1 comments

Lots of programs malloc a lot of memory, and do nothing with it for a while. This allows the os to wait for a low load time to handle memory allocation.
Is there much benefit from delaying the zeroing to first-write rather than first-read (which I think is effectively where Linux does it)?
I think the article is just incorrect and every system uses allocation on first read.
Probably not... but both ways are fine.