changeset 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 217863b09457
files NEWS OrthancServer/Sources/ServerContext.cpp OrthancServer/Sources/ServerContext.h
diffstat 3 files changed, 23 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Fri Jan 27 16:46:46 2023 +0100
+++ b/NEWS	Wed Feb 01 18:38:39 2023 +0100
@@ -12,6 +12,10 @@
 * New configuration "KeepAliveTimeout" with a default value of 1 second.
 * ResourceModification jobs (/modify + /anonymize) can now use multiple threads to speed up processing
   - New configuration "JobsEngineThreadsCount.ResourceModification" to configure the number of threads.
+* Introduced a new Housekeeper thread in Orthanc (different from the Housekeeper sample plugin).  This thread
+  regularly try to give back memory that Orthanc no longer uses to the system.  This reduces the overall memory
+  consumption however, only the memory at the end of a memory arena is given back to the system.  Fragmented memory
+  is not given back and therefore, the memory consumption may still stay high.
 
 REST API
 --------
--- 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&)
--- a/OrthancServer/Sources/ServerContext.h	Fri Jan 27 16:46:46 2023 +0100
+++ b/OrthancServer/Sources/ServerContext.h	Wed Feb 01 18:38:39 2023 +0100
@@ -187,6 +187,9 @@
     static void SaveJobsThread(ServerContext* that,
                                unsigned int sleepDelay);
 
+    static void HousekeeperThread(ServerContext* that,
+                                  unsigned int sleepDelay);
+
     void SaveJobsEngine();
 
     virtual void SignalJobSubmitted(const std::string& jobId) ORTHANC_OVERRIDE;
@@ -230,6 +233,7 @@
     SharedMessageQueue  pendingChanges_;
     boost::thread  changeThread_;
     boost::thread  saveJobsThread_;
+    boost::thread  housekeeperThread_;
         
     std::unique_ptr<SharedArchive>  queryRetrieveArchive_;
     std::string defaultLocalAet_;