Any tips on finding about memory leaks please?

My code running in REPL causes memory usage (as shown in ‘top’) of julia to go up gradually. Calling gc() specifically doesn’y help much. Oftentimes, I have to restart julia.

The code basically reads in a DataFrames via readtable, then it allocates some memory and does some processing, then it may plot some graphs with Plots package, then repeats with a different data file, and so on. Some of the above procedures may be manual, with re-including the code file and re-launching the program function. Even with re-launching the program function, the memory usage still goes up through time. I don’t have global memory allocation, and all allocations are within functions. So I am quite suspicious that it is not my code leaking memory (am I correct in this?).

But anyhow, what can I do to find out about memory leaks?

1 Like

Are you working in pure Julia, or calling external libraries (eg in C) that allocate memory?

If you are only using Julia, memory usage per se should not be a concern, the garbage collection will take care of it as needed.

Thanks for replying.

I am using pure Julia (0.6.4), and its packages.

The problem for me is that memory usage can go very high so the system has to use swap space (on Ubuntu 18.04) at times. And there is no sign that gc kicks in to help. The only solution is to restart julia.

Hard to say more without the actual code, but I would first investigate memory usage of objects with Base.summarysize. Also see this question.

Thanks for the tip.

The summarysize of my data structures stay quite constant through the iterations, but the size of julia grows. I even tried to set some large data to “nothing” before quitting the function and then call gc(). All does not help much. Very strange.

Anyone know if this following type of statement can give gc trouble? I have yet done a specific test on it.

push!(getfield(myData, fields[i]), 0.)

1 Like

Are there any new tools in the horizon to analyse memory usage?

I often find it difficult to fix my memory leaks. As a result my HPC Node kills my scripts at random times due to out of memory. I now write scripts that save their status and just run them in a loop until they have completed their tasks.

But ideally I’d like to fix my leaks. Just setting all my references (that I can identify) to nothing and garbage collecting Doesn’t help much.

Are there any tools available to track down such leaks? Could it be caused by multithreading (I’ve seen suggestions along that on discourse before), the compiler? Me point is I don’t really know where to look.

What kind of precautions do people normally take in Julia when they have to handle very memory intensive tasks? I’m still fairly inexperienced with this so any suggestions will be appreciated.

1 Like

does your script use ccalls? If it’s pure Julia, you have to be doing something extreme.

Pure Julia. Briefly: I’m assembling a large LSQ system, ca 1M x 10K built from a rather complex model and dataset. There is therefore quite a lot of infrastructure that this builds on, which makes it difficult to track down the leak. I had expected that nulling all references would suffice but apparently not.

how do you know it’s a leak? (vs. you actually have that much memory need

Because when I restart the script and reload only that data I need (which I thought was all the data I didn’t set to null) the memory usage suddenly goes down by a factor 2-4. (For example)

Maybe this is not technically a leak except in the sense that’s i don’t know where the memory is being used.

I know this is all vague but please believe me when I say that there is no MWE.

1 Like

if you know how much data you need to load, is it possible to pre-allocate? that should prevent memory leak.

Does this problem exist running on the current release version of Julia?

I pre-allocate anything I can predict.

Thank you for the suggestion. I will try this.

Maybe (?) an important piece of information: I heavily use type unstable code (and function barriers for performance critical components.) and understand from a recent thread that this creates a lot of extra work for the Compiler. Could it also lead to higher memory usage and is it possible this left in the system? Would it be worthwhile to experiment with @nospecialize?

P.s: the kind of things I do is to dispatch in values using Val and to use {Any, Any} dictionaries. The need for (or maybe convenience of) this style of coding comes from the fact that I have many different „types“ of data to process.

What type unstable code are you heavily using? Rewriting it to be stable night help a ton.

You mentioned that you are also creating plots (Plots.jl?). Are you using the GR backend? If so, I use that to plot event displays in online reconstructions and also faced memory pile-ups. What you can try is this magic line, this fixed it totally and my code is running now for over a month without memory leaks:

GR.inline("png")

This is not possible without rewriting a fairly large package from scratch. Also - I don’t want to. I like the fact that I can choose to break type stability when it becomes convenient. This gives rise to some very elegant code design.

No, I’m not plotting at all.

Ah sorry I missed that you hijacked this thread :stuck_out_tongue_winking_eye: