# ERROR: OutOfMemoryError()

**URL:** https://discourse.julialang.org/t/error-outofmemoryerror/129229
**Category:** Performance
**Tags:** memory
**Created:** [May 21, 2025, 6:17pm UTC](https://discourse.julialang.org/t/error-outofmemoryerror/129229 "2025-05-21T18:17:57Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)
#### Post date: [May 21, 2025, 6:17pm UTC](https://discourse.julialang.org/t/error-outofmemoryerror/129229/1 "2025-05-21T18:17:57Z")

</div>

I got **ERROR: OutOfMemoryError()** while running this code. How can i mitigate it? Here 872 is number of mesh blocks of whose coordinates are x, y and z.

```julia
x = rand(16,872);
y = rand(4,872);
z = rand(18,872);

r = reshape(x, :, 1, 1)
θ = reshape(y, 1, :, 1)
ϕ = reshape(z, 1, 1, :)

X = r .* sin.(θ) .* cos.(ϕ)
Y = r .* sin.(θ) .* sin.(ϕ)
Z = r .* cos.(θ)

```

My laptop have 32Gb ram. My `lscpu` output looks like:

> **Summary**
>
> > shell\> lscpu  
> > Architecture: x86\_64  
> > CPU op-mode(s): 32-bit, 64-bit  
> > Address sizes: 46 bits physical, 48 bits virtual  
> > Byte Order: Little Endian  
> > CPU(s): 14  
> > On-line CPU(s) list: 0-13  
> > Vendor ID: GenuineIntel  
> > Model name: Intel(R) Core™ Ultra 7 155U  
> > CPU family: 6  
> > Model: 170  
> > Caches (sum of all):  
> > L1d: 352 KiB (10 instances)  
> > L1i: 640 KiB (10 instances)  
> > L2: 10 MiB (5 instances)  
> > L3: 12 MiB (1 instance)

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [May 21, 2025, 6:20pm UTC](https://discourse.julialang.org/t/error-outofmemoryerror/129229/2 "2025-05-21T18:20:53Z")

</div>

I mean, you’re creating two arrays with 763839184896 8-byte elements in them. If I got my orders of magnitude right, that’s 12 terabytes right there, no?

---

<div class="post-metadata">

### Author: ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)
#### Post date: [May 21, 2025, 6:30pm UTC](https://discourse.julialang.org/t/error-outofmemoryerror/129229/3 "2025-05-21T18:30:45Z")

</div>

> [@mbauman](#):
>
> you’re creating two arrays with 1551285646848 8-byte elements in them

Which two arrays; Xt and Yt?

---

<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: [May 21, 2025, 6:32pm UTC](https://discourse.julialang.org/t/error-outofmemoryerror/129229/4 "2025-05-21T18:32:48Z")

</div>

yes

---

<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: [May 21, 2025, 6:33pm UTC](https://discourse.julialang.org/t/error-outofmemoryerror/129229/5 "2025-05-21T18:33:38Z")

</div>

Did you intend for the `872` dimension to be shared or reduced over?

---

<div class="post-metadata">

### Author: ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)
#### Post date: [May 21, 2025, 6:36pm UTC](https://discourse.julialang.org/t/error-outofmemoryerror/129229/6 "2025-05-21T18:36:05Z")

</div>

I couldn’t understand what you mean by ⬇ _shared or reduced over_.

> [@Oscar\_Smith](#):
>
> Did you intend for the `872` dimension to be shared or reduced over?

---

<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: [May 21, 2025, 6:37pm UTC](https://discourse.julialang.org/t/error-outofmemoryerror/129229/7 "2025-05-21T18:37:34Z")

</div>

I’m trying to ask what you are intending this code to do.

---

<div class="post-metadata">

### Author: ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)
#### Post date: [May 21, 2025, 6:39pm UTC](https://discourse.julialang.org/t/error-outofmemoryerror/129229/8 "2025-05-21T18:39:32Z")

</div>

I want to write this code below 👇 using vectorisation @. so that i don’t iterate in _for loops_.

```julia
    for b in 1:872
        xs = x[:, b]
        ys = y[:, b]
        zs = z[:, b]

        for i in 1:length(xs), j in 1:length(ys), k in 1:length(zs)
            r, θ, ϕ = xs[i], ys[j], zs[k]
            X = r * sin(θ) * cos(ϕ)
            Y = r * sin(θ) * sin(ϕ)
            Z = r * cos(θ)

            push!(all_xs, X)
            push!(all_ys, Y)
            push!(all_zs, Z)
        end
    end

```

Is my x, y and z array reshaping code of first starting post wrong? 😇

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [May 21, 2025, 7:12pm UTC](https://discourse.julialang.org/t/error-outofmemoryerror/129229/9 "2025-05-21T19:12:19Z")

</div>

Well, you got it wrong. You rather need something like this, and possibly permute things to get the orderings right.

```julia
x = rand(16,872);
y = rand(4,872);
z = rand(18,872);

r = reshape(x, :, 1, 1, 872)
θ = reshape(y, 1, :, 1, 872)
ϕ = reshape(z, 1, 1, :, 872)

X = vec(r .* sin.(θ) .* cos.(ϕ))
Y = vec(r .* sin.(θ) .* sin.(ϕ))
Z = vec(r .* cos.(θ))

```

> [@raman\_kumar](#):
>
> using vectorisation @. so that i don’t iterate in _for loops_

Why do you want to? Vectorizing this kind of code most of the time makes it harder to reason about and there should be no fundamental differences in speed. If you want to run it on GPU I could see a point. But as you can already see, it’s easy to get things wrong.

---

<div class="post-metadata">

### Author: ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)
#### Post date: [May 21, 2025, 7:26pm UTC](https://discourse.julialang.org/t/error-outofmemoryerror/129229/10 "2025-05-21T19:26:09Z")

</div>

Indeed, the reshaping did not work. Always good to check shapes:

```julia-repl
julia> r = reshape(x, :, 1, 1);

julia> size(r)
(13952, 1, 1)

```

The closest analog to numpy would probably be

```julia
const newaxis = [CartesianIndex()]
r = x[:, newaxis, newaxis, :]
θ = y[newaxis, :, newaxis, :]
ϕ = z[newaxis, newaxis, :, :]

X = vec(r .* sin.(θ) .* cos.(ϕ))

```

Alternatively, you can use something like [Tullio.jl](https://github.com/mcabbott/Tullio.jl) or [TensorCast.jl](https://github.com/mcabbott/TensorCast.jl) and write it as

```julia
using TensorCast

@cast X[i⊗ j⊗ k⊗ b] := x[i, b] * sin(y[j, b]) * cos(z[k, b])

```

which closely matches your intended loop structure.

---

<div class="post-metadata">

### Author: ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)
#### Post date: [May 21, 2025, 9:44pm UTC](https://discourse.julialang.org/t/error-outofmemoryerror/129229/11 "2025-05-21T21:44:37Z")

</div>

> [@GunnarFarneback](#):
>
> `Z = vec(r .* cos.(θ))`

Yes, You pointed it correctly X and Y are correct. But your `Z = vec(r .* cos.(θ))` gives less elements than X and Y coordinate points. Thanks
