Memory allocation issue

193 views
Skip to first unread message

Colin Lea

unread,
Nov 26, 2014, 10:55:59 AM11/26/14
to julia...@googlegroups.com
I'm implementing an inference algorithm and am running into memory allocation issues that are slowing it down. I created a minimal example that resembles my algorithm and see that the problem persists.

The issue is that Julia is allocating a lot of extra memory when adding matrices together. This happens regardless of whether or not I preallocate the output matrix. 

Minimal example: 

Memory output of minimal example (using julia --track-allocation=user):

Am I misunderstanding something? Should I be performing the operation differently?

One thing I've played with is the matrix C. The indices are a sliding window (e.g. use C[t-10:t] for all t). When I remove C from the equation the performance increases by a factor of 2.5. However, it still uses more memory than expected. Could this be the primary issue?

Milan Bouchet-Valat

unread,
Nov 26, 2014, 12:17:32 PM11/26/14
to julia...@googlegroups.com
Matrix addition returns a copy (at least currently), so you want to
avoid using it in a tight loop. You can simply write another loop inside
the existing ones to go over 'out' an fill it element by element.


Regards

Tim Holy

unread,
Nov 26, 2014, 12:37:12 PM11/26/14
to julia...@googlegroups.com
Nice job using track-allocation to figure out where the problem is.

If you really don't want allocation, then you should investigate Devec.jl or
InPlaceOps.jl, or write out these steps using loops to access each element of
those matrices.

--Tim

Colin Lea

unread,
Nov 26, 2014, 1:28:31 PM11/26/14
to julia...@googlegroups.com
Thanks to you both! However, there is still another odd issue. 

These two functions should be the same, but take very different amounts of time/memory. Both 'T' and 'n_classes' are both of type Int64. 

@time (
    for t = 2:T
        for n = 1:n_classes
            for j = 1:n_classes
            end
        end
    end
)

@time (
    for t = 2:5000
        for n = 1:10
            for j = 1:10
            end
        end
    end
)

elapsed time: 0.063186286 seconds (18190040 bytes allocated)
elapsed time: 0.002261641 seconds (71824 bytes allocated)

Any insight on this?

John Myles White

unread,
Nov 26, 2014, 1:30:39 PM11/26/14
to julia...@googlegroups.com
Are these functions? Or are you timing expressions in the global scope?

 -- John

Colin Lea

unread,
Nov 26, 2014, 1:33:47 PM11/26/14
to julia...@googlegroups.com
That was in global scope. Should that matter?


On Nov 26, 2014, at 1:30 PM, John Myles White <johnmyl...@gmail.com> wrote:

?

John Myles White

unread,
Nov 26, 2014, 1:35:16 PM11/26/14
to julia...@googlegroups.com
Yes, the global scope is scarcely optimized and doesn't provide useful information about performance.

 -- John

Colin Lea

unread,
Nov 26, 2014, 1:46:26 PM11/26/14
to julia...@googlegroups.com
Ah, ok. Thanks! I ran those as functions and got similar timings.

Tim Holy

unread,
Nov 27, 2014, 5:06:44 AM11/27/14
to julia...@googlegroups.com
It's the whole "global variable" thing. Put all this in a function (with
locally-defined T and n_classes), and you won't see any difference.

--Tim

Tim Holy

unread,
Nov 27, 2014, 5:07:53 AM11/27/14
to julia...@googlegroups.com
I should have read the rest of the thread. By "similar timings" do you mean
now that they both are fast and neither allocates any significant memory? (This
is how it should be.) If not, you're still using a global variable.

--Tim

On Wednesday, November 26, 2014 01:46:22 PM Colin Lea wrote:
> Ah, ok. Thanks! I ran those as functions and got similar timings.
>
> > On Nov 26, 2014, at 1:35 PM, John Myles White <johnmyl...@gmail.com>
> > wrote:
> >
> > Yes, the global scope is scarcely optimized and doesn't provide useful
> > information about performance.>
> > -- John
> >
> > On Nov 26, 2014, at 10:33 AM, Colin Lea <coli...@gmail.com
<mailto:coli...@gmail.com>> wrote:
> >> That was in global scope. Should that matter?
> >>
> >>> On Nov 26, 2014, at 1:30 PM, John Myles White <johnmyl...@gmail.com
> >>> <mailto:johnmyl...@gmail.com>> wrote:
> >>>
> >>> ?

Colin Lea

unread,
Nov 28, 2014, 9:48:11 PM11/28/14
to julia...@googlegroups.com
Yup, to clarify they are both fast and neither allocated a significant amount of memory.

Thanks,
Colin
Reply all
Reply to author
Forward
0 new messages