Skip to content

My Project Is Running Out of Memory

Harald Schilly edited this page Jan 23, 2018 · 6 revisions

How can you tell if your project is running out of memory?

  1. Use top in a terminal. Currently the easiest way to test this is to open a terminal (+New --> Terminal) in your project, then type top. Watch the processes listed there, especially the column labeled RES, which stands for "resident memory". If your project has 1000MB of RAM, which is the default in CoCalc, and the sum of the RES entries is close to 1000000, then you should definitely expect a memory allocation failure soon.

    You can type M into top to make it sort processes by memory, which will help in seeing how much is used. You can also hit the space bar a lot to make it update frequently. Type q when you are done. If you want to kill a process in top, type k then the process id (PID), which is in the first column.

  2. Project settings. You can also look in the Settings (wrench) tab of your project under "Shared RAM", which will say how much is "currently" used. This is only updated once every 30s though, and may not show spikes in attempted memory allocation.

  3. Use smem -ntk in a terminal. This command gives a useful breakdown of memory usage.

You may be able to work around a low-memory situation temporarily by killing selected processes or restarting your project.

But if you're hitting memory issues and just want more space, upgrade your project. The subscription are listed here. For example, for $7/month, you can upgrade from 1GB to 4GB, and have a lot more elbow room! If you're confused or have questions about upgrading, just ask at [email protected].


Software specific

Matplotlib

When you produce many graphics in a row and use the usual pyplot API, the intermediate graphics are not cleaned up. Run the following to clear up the memory:

import matplotlib.pyplot as plt
plt.close('all')

RSTAN

In case you're using rstan in R, you can try to switch the C++ compiler to clang++. Run the following in a cell to create appropriate config files in your project:

dir.create("~/.R", showWarnings = FALSE)
cat("CXX = clang++", "CXXFLAGS = -g0 -Os -march=native -mtune=native", file = "~/.R/Makevars", sep = "\n")
Sys.setenv(R_MAKEVARS_USER = normalizePath("~/.R/Makevars"))

Credits: https://github.com/sagemathinc/cocalc/issues/2337#issuecomment-355637033 by @bgoodri

Clone this wiki locally