# Out of Memory Sparse Matrix

**URL:** https://discourse.julialang.org/t/out-of-memory-sparse-matrix/107292
**Category:** New to Julia
**Created:** [December 7, 2023, 4:43pm UTC](https://discourse.julialang.org/t/out-of-memory-sparse-matrix/107292 "2023-12-07T16:43:32Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Tommaso\_Tenna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tommaso_tenna/32/205292_2.png) [@Tommaso\_Tenna](https://discourse.julialang.org/u/Tommaso_Tenna)
#### Post date: [December 7, 2023, 4:43pm UTC](https://discourse.julialang.org/t/out-of-memory-sparse-matrix/107292/1 "2023-12-07T16:43:32Z")

</div>

Good evening,  
I’m new to Julia and I was trying to rewrite a MATLAB code, to speed it up. The main problem is that the code generates an “Out of Memory” error and I don’t understand why.  
Outside a while loop I define

```julia
A=kron(J,Kx)+kron(Ay,J);
Id=I(N*N); #I use sparse matrices package
K=Id+(2.*D.*dt.*theta).*A;
R_app=Id-2.*D.*dt.*(1-theta).*A;

```

where J, Kx, Ay are sparse matrices, dt, D and theta are scalars and N is integer. Here everything works fast, without any errors.  
Then, inside the while loop, I update K and R\_app (due to the change of dt)

```julia
K=Id+(2.*D.*dt.*theta).*A;
R_app=Id-2.*D.*dt.*(1-theta).*A;

```

but it goes Out of Memory.  
Could someone help me, please? Thank you in advance!

P.S. I put the double \* just in this question, not in the real code.

---

<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: [December 7, 2023, 4:49pm UTC](https://discourse.julialang.org/t/out-of-memory-sparse-matrix/107292/2 "2023-12-07T16:49:52Z")

</div>

> [@Tommaso\_Tenna](#):
>
> P.S. I put the double \* just in this question, not in the real code.

The right solution is to quote your code: [PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530)

I’ve edited your post to fix this. Welcome to the Julia Discourse forum!

Also, it would be easier to help you if you posted a minimal example that is runnable (as opposed to using lots of undefined symbols like `Kx` and `Ay`): [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757)
