磨砺技术珠矶,践行数据之道,追求卓越价值

回到上一级页面:PostgreSQL内部结构与源代码研究索引页    回到顶级页面:PostgreSQL索引页

[作者 高健@博客园  luckyjackgao@gmail.com]

首先学习网上例子,进行体验性的试验:

首先不限制内存使用来进行下载:

[root@cent6 Desktop]# free -m
total used free shared buffers cached
Mem:
-/+ buffers/cache:
Swap:
[root@cent6 Desktop]#

然后,再下载约700M:

wget http://centos.arcticnetwork.ca/6.4/isos/x86_64/CentOS-6.4-x86_64-LiveCD.iso

然后看内存使用情况:

[root@cent6 Desktop]# free -m
total used free shared buffers cached
Mem:
-/+ buffers/cache:
Swap:
[root@cent6 Desktop]#

确实是用掉了700多M内存。

然后,重新启动,限制内存使用:

[root@cent6 Desktop]# service cgconfig status
Stopped #mount -t cgroup -o memory memcg /cgroup # mkdir /cgroup/GroupA
# echo 10M > /cgroup/GroupA/memory.limit_in_bytes
# echo $$ > /cgroup/GroupA/tasks

然后,再看内存状况:

[root@cent6 Desktop]# free -m
total used free shared buffers cached
Mem:
-/+ buffers/cache:
Swap:
[root@cent6 Desktop]#

再下载约700M:

wget http://centos.arcticnetwork.ca/6.4/isos/x86_64/CentOS-6.4-x86_64-LiveCD.iso

再看内存使用前后对比:

[root@cent6 Desktop]# free -m
total used free shared buffers cached
Mem:
-/+ buffers/cache:
Swap:
[root@cent6 Desktop]#

可以知道,大约的内存使用量为 1525-1494=31M。不过free命令观察到的结果是有误差的,程序执行时间长,free就是一个不断累减的值,由于当前shell被限制使用内存最大10M,那么基数很小的情况下,时间越长,误差越大。

下面,看看对PostgreSQL能否产生良好的限制:

再此之前,通过系统设定来看看对postgres用户进行wget操作时的内存的控制:

[postgres@cent6 Desktop]$ cat /etc/cgconfig.conf
#
# Copyright IBM Corporation. 2007
#
# Authors: Balbir Singh <balbir@linux.vnet.ibm.com>
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2.1 of the GNU Lesser General Public License
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See man cgconfig.conf for further details.
#
# By default, mount all controllers to /cgroup/<controller> mount {
cpuset = /cgroup/cpuset;
cpu = /cgroup/cpu;
cpuacct = /cgroup/cpuacct;
memory = /cgroup/memory;
devices = /cgroup/devices;
freezer = /cgroup/freezer;
net_cls = /cgroup/net_cls;
blkio = /cgroup/blkio;
} group test1 {
perm {
task{
uid=postgres;
gid=postgres;
} admin{
uid=root;
gid=root;
} } memory {
memory.limit_in_bytes=30M;
}
} [postgres@cent6 Desktop]$

还有一个文件,cgrules.conf,也很重要:

[postgres@cent6 Desktop]$ cat /etc/cgrules.conf
# /etc/cgrules.conf
#
#Each line describes a rule for a user in the forms:
#
#<user> <controllers> <destination>
#<user>:<process name> <controllers> <destination>
#
#Where:
# <user> can be:
# - an user name
# - a group name, with @group syntax
# - the wildcard *, for any user or group.
# - The %, which is equivalent to "ditto". This is useful for
# multiline rules where different cgroups need to be specified
# for various hierarchies for a single user.
#
# <process name> is optional and it can be:
# - a process name
# - a full command path of a process
#
# <controller> can be:
# - comma separated controller names (no spaces)
# - * (for all mounted controllers)
#
# <destination> can be:
# - path with-in the controller hierarchy (ex. pgrp1/gid1/uid1)
#
# Note:
# - It currently has rules based on uids, gids and process name.
#
# - Don't put overlapping rules. First rule which matches the criteria
# will be executed.
#
# - Multiline rules can be specified for specifying different cgroups
# for multiple hierarchies. In the example below, user "peter" has
# specified 2 line rule. First line says put peter's task in test1/
# dir for "cpu" controller and second line says put peter's tasks in
# test2/ dir for memory controller. Make a note of "%" sign in second line.
# This is an indication that it is continuation of previous rule.
#
#
#<user> <controllers> <destination>
#
#john cpu usergroup/faculty/john/
#john:cp cpu usergroup/faculty/john/cp
#@student cpu,memory usergroup/student/
#peter cpu test1/
#% memory test2/
#@root * admingroup/
#* * default/
# End of file
postgres memory test1/
#
[postgres@cent6 Desktop]$

在root用户,设置如下两个服务随系统启动:

chkconfig cgconfig  on

chkconfig cgred on

然后重新启动系统后,用postgres用户进行登录,进行检验:

[postgres@cent6 Desktop]$ free -m
total used free shared buffers cached
Mem: 2006 381 1625 0 25 134
-/+ buffers/cache: 221 1785
[postgres@cent6 Desktop]$ wget http://centos.arcticnetwork.ca/6.4/isos/x86_64/CentOS-6.4-x86_64-LiveCD.iso

执行完毕后,看内存状况,成功。

[postgres@cent6 Desktop]$ free -m
total used free shared buffers cached
Mem: 2006 393 1613 0 28 141
-/+ buffers/cache: 224 1782
Swap: 4031 67 3964
[postgres@cent6 Desktop]$

下面看对postgresql中执行sql 的限制如何:

步骤1:   对/etc/cgconfig.conf 文件和 /etc/cgrules.conf 文件的设置如前所述。
步骤2:   运行前查看内存状况:
[postgres@cent6 Desktop]$ free -m
             total       used       free     shared    buffers     cached
Mem:          2006        384       1622          0         26        138
-/+ buffers/cache:        219       1787
Swap:         4031         87       3944
[postgres@cent6 Desktop]$ 
步骤3: 开始处理大量数据(约600MB)
postgres=# select count(*) from test01;
count
-------
0
(1 row) postgres=# insert into test01 values(generate_series(1,614400),repeat( chr(int4(random()*26)+65),1024));

运行刚刚开始,就出现了如下的错误:

The connection to the server was lost. Attempting reset: Failed.
!>

这和之前碰到的崩溃情形一致。

 
PostgreSQL的log本身是这样的: 
[postgres@cent6 pgsql]$ LOG:  database system was shut down at -- :: CST
LOG: database system is ready to accept connections
LOG: autovacuum launcher started
LOG: server process (PID ) was terminated by signal : Killed
DETAIL: Failed process was running: insert into test01 values(generate_series(,),repeat( chr(int4(random()*)+),));
LOG: terminating any other active server processes
WARNING: terminating connection because of crash of another server process
DETAIL: The postmaster has commanded this server process to roll back the current transaction and exit, because another server process exited abnormally and possibly corrupted shared memory.
HINT: In a moment you should be able to reconnect to the database and repeat your command.
FATAL: the database system is in recovery mode
LOG: all server processes terminated; reinitializing
LOG: database system was interrupted; last known up at -- :: CST
LOG: database system was not properly shut down; automatic recovery in progress
LOG: redo starts at /9E807C90
LOG: unexpected pageaddr /946BE000 in log file , segment , offset
LOG: redo done at /9F6BDB50
LOG: database system is ready to accept connections
LOG: autovacuum launcher started

通过dmesg命令,可以看到,发生了Out of Memory错误,这次是 cgroup out of memory

[postgres@cent6 Desktop]$ dmesg | grep post
[ ] postgres
[ ] postgres
[ ] postgres
[ ] postgres
[ ] postgres
[ ] postgres
[ ] postgres
[ ] postgres
[ ] postgres
[ ] postgres
[ ] postgres
[ ] postgres
[ ] postgres
[ ] postgres
[ ] postgres
[ ] postgres
[ ] postgres
[ ] postgres
[ ] postgres
[ ] postgres
[ ] postgres
Memory cgroup out of memory: Kill process (postgres) score or sacrifice child
Killed process , UID , (postgres) total-vm:292300kB, anon-rss:8432kB, file-rss:3712kB
[postgres@cent6 Desktop]$

我怀疑自己的内存开得过小了,影响到一些基本的运行。PostgreSQL本身也需要一些资源(shared_buffers、wal_buffers都需要用一些内存)

所以我调整了参数   memory.limit_in_bytes=300M ,再次运行:
前述的sql问处理1200MB数据,成功结束,内存没有过多增长。

[作者 高健@博客园  luckyjackgao@gmail.com]

回到上一级页面:PostgreSQL内部结构与源代码研究索引页    回到顶级页面:PostgreSQL索引页

磨砺技术珠矶,践行数据之道,追求卓越价值

使用cgroups来控制内存使用的更多相关文章

  1. 在浏览器中将表格导入到本地的EXCEL文件,注意控制内存

    if ($export_flag == 1) { $rr = $this->mdl->test($test); header("Content-Type: application ...

  2. [C++] 重载new和delete——控制内存分配

      1.new和delete表达式的工作机理      1)new表达式实际执行了三步 string *sp=new string("aaaa"); ];//string采用默认初 ...

  3. C++之控制内存分配

    一.内存分配方式 在C++中,内存分成5个区,他们分别是堆.栈.自由存储区.全局/静态存储区和常量存储区.栈:在执行函数时,函数内局部变量的存储单元都可以在栈上创建,函数执行结束时这些存储单元自动被释 ...

  4. c++控制内存分配

    为了满足应用程序对内存分配的特殊需求,C++允许重载new运算符和delete运算符控制内存分配,通过定位new表达式初始化对象(好处是可以在某些场景下避免重新内存分配的消耗) 1.operate n ...

  5. SQL Server sp_configure 控制内存使用

    背景知识: sp_configure   显示或更改当前服务器的全局配置设置(使用 sp_configure 可以显示或更改服务器级别的设置.) 查看 全局配置值 方法 1.execute sp_co ...

  6. C++ Primer 笔记——控制内存分配

    1.当我们使用一条new表达式时,实际执行了三步操作. new表达式调用一个名为operator new ( 或者operator new[] ) 的标准库函数.该函数分配一块足够大的,原始的,未命名 ...

  7. 【C++ Primer | 19】控制内存分配

    重载new和delete 1. 测试代码: #include<iostream> #include<new> using namespace std; class A { pu ...

  8. 使用cgroups来控制磁盘IO带宽

    磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面:PostgreSQL内部结构与源代码研究索引页    回到顶级页面:PostgreSQL索引页 [作者 高健@博客园  luckyjackga ...

  9. 使用Jprofiler分析Java项目的内存开销情况并利用强制回收控制内存

    一.问题背景 自己开发的Java项目中占用太多的Heap Space.即使在Eclipse的虚拟机参数中设置"-Xms128m -Xms2048m -XX:MetaspaceSize=512 ...

随机推荐

  1. 在html的JavaScript部分计算,保留小数点后面的位数

    例: f_pbf = ((f_boday_fat/f_weight)*100).toFixed(1);      注:例子中的.toFixed(1)是所用函数,确保在所得结果中保留小数点后面一位数,若 ...

  2. 转: 根据屏幕分辨率,浏览器调用不同css

    <link type="text/csss" href="" rel="stylesheet"/> <link type= ...

  3. 使用mac版思维导图软件MindNode

    下载地址 http://pan.baidu.com/s/1hq3fUVq 思维导图又叫心智图,是表达发射性思维的有效的图形思维工具 ,它简单却又极其有效,是一种革命性的思维工具.思维导图运用图文并重的 ...

  4. iptables简单规则记录

    先来一句:好记性不如烂笔头! 1.iptables简介 iptables是基于包过滤的防火墙,它主要工作在osi模型的2,,4层,也可以工作在7层(iptables + squid) 2.原理 防火墙 ...

  5. Alpha 冲刺报告(7/10)

    Alpha 冲刺报告(7/10) 队名:洛基小队 峻雄(组长) 已完成:参考已有资源设计角色属性 明日计划:完善并编码 剩余任务:角色的属性脚本 困难:编码进展缓慢 -------------- 非易 ...

  6. JS 事件冒泡、捕获。学习记录

    作为一个转行刚到公司的新人,任务不多,这一周任务全部消灭,闲暇的一天也别闲着,悄悄的看起了书.今天写一下JS的事件冒泡.捕获. 也是今天看的内容有点多了,有些消化不了,就随手记录一下.纯属自我理解,如 ...

  7. Hadoop HA on Yarn——集群启动

    这里分两部分,第一部分是NameNode HA,第二部分是ResourceManager HA (ResourceManager HA是hadoop-2.4.1之后加上的) NameNode HA 1 ...

  8. 2.3 Python语言基础

    2.3 Python语言基础 1 语言语义(Language Semantics) 缩进,而不是括号 Python使用空格(tabs or spaces)来组织代码结构,而不是像R,C++,Java那 ...

  9. 小白学svn

    该博客是本人第一次在自己的电脑中部署svnserver后的一些心得,希望对小白们有所帮助.尽管本人之前有使用svn开发的经验,可是那都是使用百度开发人员平台的,我一直以为在自己的电脑中弄svnserv ...

  10. Curator 基本API

    package bjsxt.curator.base; import java.util.List; import java.util.concurrent.ExecutorService; impo ...