# RAM needed to initialise large matrices

**URL:** <https://discourse.julialang.org/t/ram-needed-to-initialise-large-matrices/78302>\
**Category:** General Usage\
**Tags:** memory, memory-allocation\
**Created:** [March 22, 2022, 8:04pm UTC](https://discourse.julialang.org/t/ram-needed-to-initialise-large-matrices/78302 "2022-03-22T20:04:12Z")\
**Posts on this page:** 1\
**Showing post:** 28

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [March 23, 2022, 1:48am UTC](https://discourse.julialang.org/t/ram-needed-to-initialise-large-matrices/78302/28 "2022-03-23T01:48:40Z")

</div>

> [@Oscar\_Smith](#):
>
> Calling `zeros` doesn’t write to memory, so the OS doesn’t actually have to give you any memory.

I don’t think this is true? Calling `zeros(UInt8, 1000_000_000)` seems to first invoke the `undef` constructor to get `v = Vector{UInt8}(undef, (1000_000_000,))` which doesn’t have to allocate physical memory when it’s done via `mmap`. But this is followed by `fill!(v, zero(UInt8))` which would touch every byte in the array, causing many OS page faults which in turn allocate physical memory pages.

For another interesting thread on page faults, see here: [Julia slower in Windows - #16 by c42f](https://discourse.julialang.org/t/julia-slower-in-windows/21027/16)

(Edit: As a side note - it seems technically possible to do lazy zero fill in (a) the case that a large array happens to be allocated with [`mmap` and `MAP_ANONYMOUS`](https://man7.org/linux/man-pages/man2/mmap.2.html) somewhere deep in the allocator and (b) `zero(eltype(a))` happens to be a bits type with a bit pattern of zeros. I think this would only help in specialized circumstances, though.)

---

_[View the full topic](https://discourse.julialang.org/t/ram-needed-to-initialise-large-matrices/78302)._
