# Memory allocation when using permutedims

**URL:** https://discourse.julialang.org/t/memory-allocation-when-using-permutedims/105806
**Category:** New to Julia
**Tags:** performance, memory-allocation, tensors
**Created:** [November 5, 2023, 1:52am UTC](https://discourse.julialang.org/t/memory-allocation-when-using-permutedims/105806 "2023-11-05T01:52:38Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![jisutich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jisutich/32/33342_2.png) [@jisutich](https://discourse.julialang.org/u/jisutich)
#### Post date: [November 5, 2023, 1:52am UTC](https://discourse.julialang.org/t/memory-allocation-when-using-permutedims/105806/1 "2023-11-05T01:52:39Z")

</div>

Hi,

I am trying to do some operations on some large tensor (roughly 2^(24)-2^(26) complex elements). I need to use `reshape` and `permutedims` frequently. Considering the large size of the tensor I consider, I am a little worried about the memory allocation. My understanding is that when I use `reshape` it still points to the original data based on [julia - How to reshape Arrays quickly - Stack Overflow](https://stackoverflow.com/questions/25949718/how-to-reshape-arrays-quickly). However, it seems that when I use `permutedims`, a copy is created and this leads to extra memory allocation. Is there any way to permute dimensions without causing extra memory allocation?

Thanks

---

<div class="post-metadata">

### Author: ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)
#### Post date: [November 5, 2023, 6:28am UTC](https://discourse.julialang.org/t/memory-allocation-when-using-permutedims/105806/2 "2023-11-05T06:28:20Z")

</div>

Note there is a difference between reshaping and permuting the dimensions: Reshaping keeps the same “order” of the data and permuting axes does not. Standard arrays in Julia are always “contiguous” meaning their data is in the order corresponding to the indices.

What you are looking for is a lazy version of permutedims. I am not sure whether there is a “canonical” package for this but there is an implementation in Base called `PermutedDimsArray` and I found `transmute` from [`TransmuteDims.jl`](https://github.com/mcabbott/TransmuteDims.jl). Just understand that you make a tradeoff here: You save memory by not copying the array but will likely lose performance when you keep working with the lazily permuted array. For reference:

> [@PermutedDimsArray slower than permutedims?](https://discourse.julialang.org/t/permuteddimsarray-slower-than-permutedims/46401):
>
> I’m struggling to understand why one function for multiplying a batch of matrices with a single matrix is significantly slower than another. The only difference between the functions is that PermutedDimsArray is used in place of permutedims in the slower function. I would have thought permutedims would be slower given that it creates a copy of the array while PermutedDimsArray creates a new view. This makes me think I’ve misunderstood something fundamental with how Julia works. function batche…

Two other ideas:

- Maybe you can keep track of the permutation elsewhere and use this information in your code
- there is `permutedims!` which copies the permuted array to a _different_ location in memory. So if you can afford to allocate a temporay array once and keep it around, then you can perform all the `permutedims` you want and keep the arrays contiguous.
