<< 2014年4月8日 | 首页 | 2014年4月10日 >>

浅谈Linux内存管理机制 - 51CTO.COM

作为一名Linux系统管理员,监控内存的使用状态是非常重要的,通过监控有助于了解内存的使用状态,比如内存占用是否正常,内存是否紧缺等等,监控内存最常使用的命令有free、top等,下面是某个系统free的输出:

 

  1. [root@linuxeye ~]# free 
  2.              total       used       free     shared    buffers     cached 
  3. Mem:       3894036    3473544     420492          0      72972    1332348 
  4. -/+ buffers/cache:    2068224    1825812 
  5. Swap:      4095992     906036    3189956 

 

每个选项的含义:

第一行:

total:物理内存的总大小

used:已经使用的物理内存大小

free:空闲的物理内存大小

shared:多个进程共享的内存大小

buffers/cached:磁盘缓存的大小

 

第二行Mem:代表物理内存使用情况

第三行(-/+ buffers/cached):代表磁盘缓存使用状态

第四行:Swap表示交换空间内存使用状态

 

free命令输出的内存状态,可以通过两个角度来查看:一个是从内核的角度来看,一个是从应用层的角度来看的。

从内核的角度来查看内存的状态

就是内核目前可以直接分配到,不需要额外的操作,即为上面free命令输出中第二行Mem项的值,可以看出,此系统物理内存有3894036K,空闲的内存只有420492K,也就是40M多一点,我们来做一个这样的计算:

3894036 – 3473544 = 420492

其实就是总的物理内存减去已经使用的物理内存得到的就是空闲的物理内存大小,注意这里的可用内存值420492并不包含处于buffers和cached状态的内存大小。

如果你认为这个系统空闲内存太小,那你就错了,实际上,内核完全控制着内存的使用情况,Linux会在需要内存的时候,或在系统运行逐步推进时,将buffers和cached状态的内存变为free状态的内存,以供系统使用。

从应用层的角度来看系统内存的使用状态

也就是Linux上运行的应用程序可以使用的内存大小,即free命令第三行 -/+ buffers/cached 的输出,可以看到,此系统已经使用的内存才2068224K,而空闲的内存达到1825812K,继续做这样一个计算:

420492+(72972+1332348)=1825812

通过这个等式可知,应用程序可用的物理内存值是Mem项的free值加上buffers和cached值之和,也就是说,这个free值是包括buffers和cached项大小的,对于应用程序来说,buffers/cached占有的内存是可用的,因为buffers/cached是为了提高文件读取的性能,当应用程序需要用到内存的时候,buffers/cached会很快地被回收,以供应用程序使用。

阅读全文……

标签 :

js window.onload 加载多个函数和追加函数 - 唔愛吃蘋果 - 博客园

window.onload 不能同时加载多个函数,后面会把前面的覆盖,解决办法:

 

 function addLoadEvent(func) {
  
var oldonload = window.onload;//得到上一个onload事件的函数
  if (typeof window.onload != 'function') {//判断类型是否为'function',注意typeof返回的是字符串
    window.onload = func;
  } 
else {  
    window.onload 
= function() {
      oldonload();
//调用之前覆盖的onload事件的函数---->由于我对js了解不多,这里我暂时理解为通过覆盖onload事件的函数来实现加载多个函数
      func();//调用当前事件函数
    }
  }
}

//(完整示例)使用如下:

function t(){
alert(
"t")
}
function b(){
alert(
"b")
}
function c(){
alert(
"c")
}
 
function addLoadEvent(func) {
  
var oldonload = window.onload;
  
if (typeof window.onload != 'function') {
    window.onload 
= func;
  } 
else {  
    window.onload 
= function() {
      oldonload();
      func();
    }
  }
}

addLoadEvent(t);
addLoadEvent(b);
addLoadEvent(c);
//等价于  window.onload =function() { t();  b(); c() ;}

阅读全文……

Auto refreshing properties from property files | Entelligentsia Blog

一个自动重新加载配置文件修改的Properties类:
 
 
class AutoRefreshingProperties extends Properties{
    private long LAST_LOADED = System.currentTimeMillis();
    private List listeners = Collections.synchronizedList(new ArrayList());
    public AutoRefreshingProperties(String absolutePathToPriopertiesFile) throws IOException{
        this(null, absolutePathToPriopertiesFile);
    }
    public AutoRefreshingProperties(Properties defaults, String absolutePathToPriopertiesFile) throws IOException{
        super(defaults);
        load(absolutePathToPriopertiesFile);
    }
    public AutoRefreshingProperties removeListener(FileChangeListener listener){
        listeners.remove(listener);
        return this;
    }
    public AutoRefreshingProperties addListener(FileChangeListener listener){
        listeners.add(listener);
        return this;
    }
    public synchronized void load(String absoluteFilePath) throws IOException {
        InputStream is = new FileInputStream(new File(absoluteFilePath));
        this.load(is);
        LAST_LOADED = System.currentTimeMillis();
        keepUpdated(absoluteFilePath);
    }
    abstract class FileWatcher implements Runnable{}
    private void keepUpdated(final String absoluteFilePath){
        Executors.newSingleThreadExecutor().submit(
                new FileWatcher(){
                    @Override
                    public void run() {
                        try {
                            while(true){
                                if (new File(absoluteFilePath).lastModified() &gt; LAST_LOADED){
                                    InputStream is =  new FileInputStream(new File(absoluteFilePath));
                                    load(is);
                                    LAST_LOADED = System.currentTimeMillis();
                                    notifyListeners();
                                }
                                Thread.yield();
                                Thread.sleep(2000);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
        );
    }
    private void notifyListeners(){
        final Properties properties = new Properties(this);
        for (FileChangeListener aListener : listeners){
            aListener.notifyFileChanged(properties);
        }
    }
}

阅读全文……

Using Multiple "buttons" Elements in IE6

IE6下点击类型为submit的Button,其他未点击的button也会提交,导致某些程序有问题,下面是一种解决办法。

Save this as buttonfix.js

function buttonfix() {
    var buttons = document.getElementsByTagName('button');
    for (var i=0; i<buttons.length; i++) {
        if(buttons[i].onclick) continue;
        
        buttons[i].onclick = function () {
            for(j=0; j<this.form.elements.length; j++)
                if( this.form.elements[j].tagName == 'BUTTON' )
                    this.form.elements[j].disabled = true;
            this.disabled=false;
            this.value = this.attributes.getNamedItem("value").nodeValue ;
        }
    }
}
window.attachEvent("onload", buttonfix);

On each page which you need the fix, include the following:

<!--[if lt IE 7]>
    <script type="text/javascript" src="buttonfix.js"></script>
<![endif]-->

If the problem is also present in IE7, instead use:

<!--[if IE]>
    <script type="text/javascript" src="buttonfix.js"></script>
<![endif]-->

阅读全文……

标签 : ,