diff --git a/examples/main.c b/examples/main.c
index 1c340bbf7b5df0a4dadc428fa8e4eb4a8a83d18a..dec11ace5e80c47a4bb3e845b357271113bafcc5 100644
--- a/examples/main.c
+++ b/examples/main.c
@@ -1848,7 +1848,7 @@ int main(int argc, char *argv[]) {
   if (with_cosmology) cosmology_clean(e.cosmology);
   if (e.neutrino_properties->use_linear_response)
     neutrino_response_clean(e.neutrino_response);
-  if (with_self_gravity) pm_mesh_clean(e.mesh);
+  if (with_self_gravity && s.periodic) pm_mesh_clean(e.mesh);
   if (with_stars) stars_props_clean(e.stars_properties);
   if (with_cooling || with_temperature) cooling_clean(e.cooling_func);
   if (with_feedback) feedback_clean(e.feedback_props);