How to track memory allocation in vscode

Hi,

I recently have a small code which runs fast. But when I change the arguments the memory allocation grows quickly. I want to see where the memory allocation happens. I just find this
https://github.com/JuliaCI/Coverage.jl#memory-allocation

But it says start julia with

julia --track-allocation=user

I basically use vs code to run my julia script. So I am not sure how to do this step. Anyone can help?

Thanks

2 Likes

does the settings → extensions → Julia: additional args work?

1 Like

I tried to add

"julia.additionalArgs": [
	"julia --track-allocation=my_username"
],

in the settings.

But when I try to run my code, it gives me

ERROR: SystemError: opening file "C:\\Users\\my_username\\julia --track-allocation=my_username": No such file or directory

I think you don’t need to put "julia" in front of the additional arguments. Here’s an example of the correct syntax: julia.additionalArgs doesn't accepts multiple args · Issue #1006 · julia-vscode/julia-vscode · GitHub

1 Like

Hi,

I just tried

"julia.additionalArgs": [
	"--track-allocation=my_username"
],

But vs code collapsed and I got

The terminal process "D:\Julia-1.7.2\bin\julia.exe '-i', '--banner=no',
 '--project=C:\Users\my_username\.julia\environments\v1.7', '--track-allocation=my_username', 
'c:\Users\my_username\.vscode\extensions\julialang.language-julia-1.6.17\scripts\terminalserver\terminalserver.jl', 
'\\.\pipe\vsc-jl-repl-89a84739-4e1d-469e-a8d4-dff58ca164e5',
 '\\.\pipe\vsc-jl-cr-7d16baad-0cb8-4f37-a6a1-2a41d6944d45',
 'USE_REVISE=true', 'USE_PLOTPANE=true',
 'USE_PROGRESS=true', 'DEBUG_MODE=false'" terminated with exit code: 1.

Not sure what the error means. Thanks

You’re supposed to literally use --track-allocation=user; user isn’t supposed to be substituted by anything.

Allocation profiler and some profiler tweaks by pfitzseb · Pull Request #2890 · julia-vscode/julia-vscode · GitHub may also be of interest once it’s released.

3 Likes

Yes, this works. But I have some further question. In the link I post, it says

Then:

  • Run whatever commands you wish to test. This first run is to ensure that everything is compiled (because compilation allocates memory).
  • Call Profile.clear_malloc_data()
  • Run your commands again
  • Quit julia

Should I also quit vs code? I am confused about this step “Quit julia”.

Also, it says

Finally, navigate to the directory holding your source code. Start julia (without command-line flags), and analyze the results using

using Coverage
analyze_malloc(dirnames)  # could be "." for the current directory, or "src", etc.

Since I am using vs code, I just open my script in vs code and then click run. I am confused about “start julia (without command-line flags)”.

Thanks

Based on personal experience I would use the --track-allocation=user very lightly. Perhaps do it once to get a general idea of where the bulk of the allocation is occurring and then use @allocated on the block of code where allocation is happening to drill down further.

I would play around with different ways of implementing something and I’d wrap it in something like this

tmp = @allocated ... # block of code or function here
if tmp > 0
    error("Allocation occurred")
end

I often felt the results were unreliable in the .mem file the flag --track-allocation=user produces. The results of @allocated were consistent and reliable.

Using the Julia vscode extension profiler might also help you find where allocation is occurring.

Lastly - I found this website helpful when learning about how to identify allocation.

1 Like

Just use the terminal (Terminal->New terminal in the menu) in vscode and start julia from the terminal.
Then you can start Julia with any parameter and also quickly close (ctrl+d) it and restart it.

1 Like

after you quit Julia. You probably want to restart Julia without the flag --track-allocation=user; in order to run this.

using Coverage
analyze_malloc(dirnames)  

Some background: If you run Julia with the flag --track-allocation, it will create a *.mem file for each *.jl file that you called. These *.mem files are then analyzed by analyze_malloc

1 Like

Yes this works. Thanks

Got it. Thank you

Thanks for the suggestion! I will try it.