diff OrthancServer/Sources/ServerContext.cpp @ 5152:c81f363d3aa3 malloc-trim

housekeeper thread to call malloc_trim and give back memory to the system
author Alain Mazy <am@osimis.io>
date Wed, 01 Feb 2023 18:38:39 +0100
parents 05112ff6ba22
children f5907aecbaed
line wrap: on
line diff
--- a/OrthancServer/Sources/ServerContext.cpp	Fri Jan 27 16:46:46 2023 +0100
+++ b/OrthancServer/Sources/ServerContext.cpp	Wed Feb 01 18:38:39 2023 +0100
@@ -104,6 +104,19 @@
   {
   }
 
+  void ServerContext::HousekeeperThread(ServerContext* that,
+                                        unsigned int sleepDelay)
+  {
+    while (!that->done_)
+    {
+      boost::this_thread::sleep(boost::posix_time::milliseconds(sleepDelay));
+      
+      // If possible, gives memory back to the system 
+      // (see OrthancServer/Resources/ImplementationNotes/memory_consumption.txt)
+
+      malloc_trim(256*1024);
+    }
+  }
   
   void ServerContext::ChangeThread(ServerContext* that,
                                    unsigned int sleepDelay)
@@ -143,8 +156,6 @@
           }
         }
       }
-
-      malloc_trim(0);
     }
   }
 
@@ -419,7 +430,8 @@
 
       listeners_.push_back(ServerListener(luaListener_, "Lua"));
       changeThread_ = boost::thread(ChangeThread, this, (unitTesting ? 20 : 100));
-    
+      housekeeperThread_ = boost::thread(HousekeeperThread, this, 100);
+
       dynamic_cast<DcmtkTranscoder&>(*dcmtkTranscoder_).SetLossyQuality(lossyQuality);
     }
     catch (OrthancException&)