comparison OrthancServer/Sources/ServerContext.cpp @ 5346:566e8d32bd3a

Reduce the frequency of memory trimming from 100ms to 30s to avoid high idle CPU load
author Alain Mazy <am@osimis.io>
date Thu, 29 Jun 2023 09:43:20 +0200
parents b376abae664a
children 3c8286e5d07b aaf7c49a9ddc 16cbfefa15e9
comparison
equal deleted inserted replaced
5342:65d55cc86a41 5346:566e8d32bd3a
108 } 108 }
109 109
110 110
111 #if HAVE_MALLOC_TRIM == 1 111 #if HAVE_MALLOC_TRIM == 1
112 void ServerContext::MemoryTrimmingThread(ServerContext* that, 112 void ServerContext::MemoryTrimmingThread(ServerContext* that,
113 unsigned int sleepDelay) 113 unsigned int intervalInSeconds)
114 { 114 {
115 boost::posix_time::ptime lastExecution = boost::posix_time::second_clock::universal_time();
116
115 // This thread is started only if malloc_trim is defined 117 // This thread is started only if malloc_trim is defined
116 while (!that->done_) 118 while (!that->done_)
117 { 119 {
118 boost::this_thread::sleep(boost::posix_time::milliseconds(sleepDelay)); 120 boost::posix_time::ptime now = boost::posix_time::second_clock::universal_time();
119 121 boost::posix_time::time_duration elapsed = now - lastExecution;
120 // If possible, gives memory back to the system 122
121 // (see OrthancServer/Resources/ImplementationNotes/memory_consumption.txt) 123 if (elapsed.total_seconds() > intervalInSeconds)
122 malloc_trim(128*1024); 124 {
125 // If possible, gives memory back to the system
126 // (see OrthancServer/Resources/ImplementationNotes/memory_consumption.txt)
127 {
128 MetricsRegistry::Timer timer(that->GetMetricsRegistry(), "orthanc_memory_trimming_duration_ms");
129 malloc_trim(128*1024);
130 }
131 lastExecution = boost::posix_time::second_clock::universal_time();
132 }
133
134 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
123 } 135 }
124 } 136 }
125 #endif 137 #endif
126 138
127 139
437 449
438 listeners_.push_back(ServerListener(luaListener_, "Lua")); 450 listeners_.push_back(ServerListener(luaListener_, "Lua"));
439 changeThread_ = boost::thread(ChangeThread, this, (unitTesting ? 20 : 100)); 451 changeThread_ = boost::thread(ChangeThread, this, (unitTesting ? 20 : 100));
440 452
441 #if HAVE_MALLOC_TRIM == 1 453 #if HAVE_MALLOC_TRIM == 1
442 memoryTrimmingThread_ = boost::thread(MemoryTrimmingThread, this, 100); 454 LOG(INFO) << "Starting memory trimming thread at 30 seconds interval";
455 memoryTrimmingThread_ = boost::thread(MemoryTrimmingThread, this, 30);
443 #else 456 #else
444 LOG(INFO) << "Your platform does not support malloc_trim(), not starting the memory trimming thread"; 457 LOG(INFO) << "Your platform does not support malloc_trim(), not starting the memory trimming thread";
445 #endif 458 #endif
446 459
447 dynamic_cast<DcmtkTranscoder&>(*dcmtkTranscoder_).SetLossyQuality(lossyQuality); 460 dynamic_cast<DcmtkTranscoder&>(*dcmtkTranscoder_).SetLossyQuality(lossyQuality);