# Does rand(Float64, 1000) returns an array of consecutive physical addresses?

**URL:** <https://discourse.julialang.org/t/does-rand-float64-1000-returns-an-array-of-consecutive-physical-addresses/70805>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [November 2, 2021, 12:45pm UTC](https://discourse.julialang.org/t/does-rand-float64-1000-returns-an-array-of-consecutive-physical-addresses/70805 "2021-11-02T12:45:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jerry4h](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jerry4h/32/30435_2.png) [@jerry4h](https://discourse.julialang.org/u/jerry4h)\
**Post date:** [November 2, 2021, 12:45pm UTC](https://discourse.julialang.org/t/does-rand-float64-1000-returns-an-array-of-consecutive-physical-addresses/70805/1 "2021-11-02T12:45:38Z")

</div>

I’m new to julia. I want to know how to apply for an array with contiguous physical addresses.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [November 2, 2021, 12:48pm UTC](https://discourse.julialang.org/t/does-rand-float64-1000-returns-an-array-of-consecutive-physical-addresses/70805/2 "2021-11-02T12:48:31Z")

</div>

Yes, it returns a `Vector{Float64}`, which is contiguous in memory.

---

<div class="post-metadata">

**Author:** ![jerry4h](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jerry4h/32/30435_2.png) [@jerry4h](https://discourse.julialang.org/u/jerry4h)\
**Post date:** [November 2, 2021, 1:00pm UTC](https://discourse.julialang.org/t/does-rand-float64-1000-returns-an-array-of-consecutive-physical-addresses/70805/3 "2021-11-02T13:00:42Z")

</div>

Incredible. What if applying for 320G memory with the rand(Float64, 40\*2^30)? I’m curious about what C interface is used at the bottom layer to ensure that the physical addresses are contiguous.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [November 2, 2021, 1:06pm UTC](https://discourse.julialang.org/t/does-rand-float64-1000-returns-an-array-of-consecutive-physical-addresses/70805/4 "2021-11-02T13:06:06Z")

</div>

> [@jerry4h](#):
>
> What if applying for 320G memory with the rand(Float64, 40\*2^30)?

If you don’t have that much memory available you will get an `OutOfMemoryError`:

```julia
julia> rand(Float64, 40*2^30)
ERROR: OutOfMemoryError()

```

> [@jerry4h](#):
>
> I’m curious about what C interface is used at the bottom layer to ensure that the physical addresses are contiguous.

`malloc` I believe.

---

<div class="post-metadata">

**Author:** ![jerry4h](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jerry4h/32/30435_2.png) [@jerry4h](https://discourse.julialang.org/u/jerry4h)\
**Post date:** [November 2, 2021, 1:10pm UTC](https://discourse.julialang.org/t/does-rand-float64-1000-returns-an-array-of-consecutive-physical-addresses/70805/5 "2021-11-02T13:10:41Z")

</div>

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [November 2, 2021, 1:18pm UTC](https://discourse.julialang.org/t/does-rand-float64-1000-returns-an-array-of-consecutive-physical-addresses/70805/6 "2021-11-02T13:18:42Z")

</div>

Note that while currently this uses `malloc` in the future it is completely possible that the mechanism could change to another allocator (eg gemalloc).

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 2, 2021, 3:07pm UTC](https://discourse.julialang.org/t/does-rand-float64-1000-returns-an-array-of-consecutive-physical-addresses/70805/7 "2021-11-02T15:07:55Z")

</div>

> [@jerry4h](#):
>
> I’m curious about what C interface is used at the bottom layer to ensure that the physical addresses are contiguous.

Technically, the operating system kernel allocates for you a consecutive sequence of _virtual_ addresses, but these may only correspond to consecutive “physical” addresses within a given [VM page](https://en.wikipedia.org/wiki/Memory_paging). Not that it makes any practical difference — accessing consecutive virtual addresses is fast.

Probably [TMI](https://youtu.be/ufe-uLKfEXw), but there is plenty of information online about [virtual address spaces](https://en.wikipedia.org/wiki/Flat_memory_model) if you are curious.

---

<div class="post-metadata">

**Author:** ![jerry4h](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jerry4h/32/30435_2.png) [@jerry4h](https://discourse.julialang.org/u/jerry4h)\
**Post date:** [November 2, 2021, 3:28pm UTC](https://discourse.julialang.org/t/does-rand-float64-1000-returns-an-array-of-consecutive-physical-addresses/70805/8 "2021-11-02T15:28:37Z")

</div>

Funny video! That is to say, the virtual address of the user mode is continuous, but for the rand function of the order of 320G space, there is no guarantee that the physical address is also continuous. Is my understanding correct?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 2, 2021, 3:56pm UTC](https://discourse.julialang.org/t/does-rand-float64-1000-returns-an-array-of-consecutive-physical-addresses/70805/9 "2021-11-02T15:56:38Z")

</div>

> [@jerry4h](#):
>
> but for the rand function of the order of 320G space, there is no guarantee that the physical address is also continuous. Is my understanding correct?

When you allocate an array with more than 4KiB of space (the page size, I think), the physical addresses might not be contiguous. (Again, this is a low-level operating-system/hardware thing — not specific to Julia!)

(On top of that, in some cases the operating system might “swap” memory out to disk, so it might not reside in RAM at all. You can also _request_ this for [memory-mapped files](https://en.wikipedia.org/wiki/Memory-mapped_file) using the [Mmap](https://docs.julialang.org/en/v1/stdlib/Mmap/) standard library.)
