Popular blog tags

一、释放mongodb进程占用的内存

1.用top命令查看系统占用内存的情况 

top -p $(pidof mongod)

output:

top - 10:52:52 up 5 days,  4:04,  1 user,  load average: 2.37, 2.49, 2.58
Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.2 us,  0.2 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 
KiB Mem :  4039176 total,   162032 free,  3437972 used,   439172 buff/cache
KiB Swap:   969964 total,   969964 free,        0 used.   335024 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND  
  514 mongodb   20   0 2364068 786760   8804 S   0.3 19.5  76:05.24 mongod 

 

2.强行收回内存办法

对于主要是写入的数据库,mongodb内存占满之后写入效率会变得不稳定 这个时候,你需要释放内存。

(1)重启mongod

(2)调用 db.runCommand({closeAllDatabases:1})来清除内存
(3)使用Linux命令清除缓存中的数据:echo 3 > /proc/sys/vm/drop_caches

echo 3 > /proc/sys/vm/drop_caches

output:

top - 10:58:33 up 5 days,  4:10,  1 user,  load average: 2.73, 2.50, 2.54
Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.7 us,  0.1 sy,  0.0 ni, 99.0 id,  0.1 wa,  0.0 hi,  0.0 si,  0.0 
KiB Mem :  4039176 total,   410448 free,  3441568 used,   187160 buff/cache
KiB Swap:   969964 total,   969964 free,        0 used.   381924 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND  
  514 mongodb   20   0 2364068 786944   8936 S   0.0 19.5  76:11.89 mongod 

3.自动打开Dos命令窗口并释放mongodb内存(windows)

实现 下面的c#代码可以用来定时释放内存,使用的时候注意把路径换成你服务器的路径,另外就是修改下释放频率 

class Program     {       

  static void Main(string[] args)       

  {         

    while (true)          

   {             

    new Thread(delegate()           

      {                  

   Console.WriteLine("开始释放");           

          Cmd(@"echo 正在启动MongoDB d: cd D:\mongodb\bin mongo use admin db.runCommand({closeAllDatabases:1}) ", "bye");         

            Console.WriteLine("释放完成");          

       }).Start();           

        // 三小时              

   Thread.Sleep(3 * 3600 * 1000);        

     }     

    }         

  /// <summary>

  /// 执行命令   

   /// </summary>        

 /// <param name="cmd">        

 /// <returns></returns>        

 static void Cmd(string cmd, string end)         

{             

Process process = new Process            

 {                 

StartInfo =                

 {                     

FileName = "cmd.exe",                    

 UseShellExecute = false,                     

RedirectStandardInput = true,                    

 RedirectStandardOutput = true,                     

RedirectStandardError = true,                    

 CreateNoWindow = true               

  }            

 };             

process.Start();            

 process.StandardInput.AutoFlush = true;             

process.StandardInput.WriteLine(cmd);             

process.StandardInput.WriteLine("exit");            

 var outPut = "";               

while (!(outPut = process.StandardOutput.ReadLine()).Contains(end))           

  {                

 Console.WriteLine(outPut);           

  }              

 Console.WriteLine(outPut);              

 if (process.HasExited == false)                

 //Process is still running.                 

//Test to see if the process is hung up.               

  if (process.Responding)                     //Process was responding; close the main window.                     process.CloseMainWindow();                

 else                     

//Process was not responding; force the process to close.                    

 process.Kill();              

 process.Close();        

 }     

}

 

二、MongoDB内存限制配置,控制内存使用

1.合理配置 WiredTiger cacheSizeGB

(1)查看Mongodb配置文件位置

sudo systemctl status mongodb

output:

# sudo systemctl status mongod
● mongod.service - MongoDB Database Server
   Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset:
   Active: active (running) since Tue 2020-06-02 06:48:02 CST; 5 days ago
     Docs: https://docs.mongodb.org/manual
 Main PID: 514 (mongod)
    Tasks: 42 (limit: 4681)
   CGroup: /system.slice/mongod.service
           └─514 /usr/bin/mongod --config /etc/mongod.conf

 

(2)修改配置:/etc/mongod.conf,限制到 1GB内存。

/etc/mongod.conf必须按 YAML参数格式修改配置文件,否则MongoDB启动会失败。

# Where and how to store data.
storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
      cacheSizeGB: 1

(3)配置完成后重启MongoDB。

2.mongodb在线修改cachesize

db.adminCommand({setParameter: 1, wiredTigerEngineRuntimeConfig: "cache_size=8G"})

 

MongoDB 内存用在哪

https://mongoing.com/archives/8781

参考链接:https://docs.mongodb.com/manual/reference/configuration-options/index.html#storage-wiredtiger-options