gen_server 相关的片段分析得也差不多了, 这篇作为一个简要的总结.这一系列相关的分析暂且告一段落(之后如有必要,还会回来的 ^^ ),下一个系列主要是以pool 相关, 包括但不仅限于开源项目pooler, poolboy, emysql 中的pool 的管理, rabbitmq-server 中的worker_pool 角色等.

lapse request

对于gen_server handle_call callback, 主要处理同步请求,如果gen_server 进程在Timeout 时间内无法处理完成或根本来不及处理,这个时候就会出现失效的请求. 也就是当handle_call 在开始处理某请求时, 该请求的调用者早已经不再需要此次处理.

这样的需求并不一定是需要的,需要还是根据需求场景的不同定. 而避免此种情形出现的方式也比较简单,不着急,慢慢分析:

判断调用者alive

在gen module 处理call 请求的函数 do_call/4, 在将请求发送给目标gen_server 进程后, 请求调用者就会同步receive 目标gen_server 进程的返回, 超时Timeout 之后, 调用进程以exit(timeout) 退出.是的,等gen_server 进程在Timeout 时间内无法完成或根本来不及处理请求, 请求的调用者按照默认的方式, 就会exit. 也就是说,在没有try catch 的情况下,借助erlang:is_alive/0 API , 是可以判断当前handle_call 处理的请求是否是有效的.

但是, 如果使用了try catch 或者是catch 请求的调用进程就很有可能不按照预先的期望exit 退出了,这个时候, 这种方式就已经没有效果了.

monitored_by

还是在gen module 处理call 请求的函数 do_call/4,在发送给目标gen_server 进程请求之前, 请求的调用进程会首先monitor gen_server 进程,并在Timeout 之后demonitor. 也就是, 某请求如果还是有效的话, 在gen_server 进程的 monitored_by lists 中,应该还有请求的调用进程存在.

long-lived

通常情况下,Erlang process 更被期望于以short-lived 的方式存在.

An Erlang process starts with a small stack and heap in order to support a huge number of processes in a system. The size is configurable and the default value is 233 words. In general, Erlang processes are expected to short-lived and have small amounts of live data. When there is not enough free memory in the heap for a pro- cess, it is garbage collected, and if less memory can be freed than required it grows.

但是对于gen_server 来说,一般都是作为long-lived 的进程角色.对于long-lived 的进程角色,需要对其资源利用谨慎处理(通常情况下,是对于GC).

而优化的方案,需要根据应用场景的不同而不同,可供选的大致有:

1, hibernation

2, fullsweep_after

A non-negative integer which indicates how many times generational garbage collections can be done without forcing a fullsweep collection. In low-memory systems (especially without virtual memory), setting the value to0 can help to conserve memory.

Riak 的官方文档中,对fullsweep_after 的参数设置为0.

3, min_heap_size

4, erlang:garbage_collect/1

参见RabbitMQ-server 中的background_gc module.

当然,最为理想的方案, 是尽可能将long-lived 的进程角色改为 short-lived . 参见riak_client 对gen_fsm 的使用.

参考

1, http://erlang.org/pipermail/erlang-questions/2011-October/061871.html

2, http://docs.basho.com/riak/latest/ops/advanced/configs/configuration-files/

3, http://www.erlang.org/doc/man/erlang.html#system_flag-2

4, Characterizing the Scalability of Erlang VM

5, Exploring Alternative Memory Architectures for Erlang- Implementation and Performance Evaluation (A How does the initial heap size affect the runtime? )

6, https://github.com/Eonblast/Emysql/pull/101

Erlang generic standard behaviours -- summary的更多相关文章

  1. Erlang generic standard behaviours -- gen

    在分析 gen_server (或者是gen_fsm )之前,首先应该弄明白,gen 这个module . -module(gen). -compile({inline,[get_node/1]}). ...

  2. Erlang generic standard behaviours -- gen_server system msg

    这是Erlang generic standard behaviors gen_server 分析的系列的最后一篇,主要分析gen_server module 辅助性的功能函数. 在gen_serve ...

  3. Erlang generic standard behaviours -- gen_server module

    在分析完gen module (http://www.cnblogs.com/--00/p/4271386.html)之后,就可以开始进入gen_server 的主体module 了.gen_serv ...

  4. Erlang generic standard behaviours -- gen_server hibernate

    hibernate 主要用于在内存空闲时,通过整理进程的stack,回收进程的heap 来达到回收内存节省资源的效果. hibernate 可用于OTP 进程以及普通进程, hibernate 的官方 ...

  5. Erlang generic standard behaviours -- gen_server noblock call

    在Erlang 系统中,经常需要gen_server 进程来处理共享性的数据,也就是总希望一个gen_server 进程来为多个普通进程提供某种通用性的服务,这也是gen_server 设计的初衷.但 ...

  6. Erlang generic standard behaviours -- gen_server terminate

    gen_server 主体 module 已经分析完了(http://www.cnblogs.com/--00/p/4271982.html),接着,分析下gen_server 中的terminate ...

  7. jQ1.5源码注释以及解读RE

    jQ作为javascript的库( ▼-▼ ), 尽善尽美, 代码优美,  值得学习.  这一周平常上班没啥事也看jQ1.5的代码, 今天周六差不多看完了(Sizzle部分还没看), 重新看了一下, ...

  8. Java性能提示(全)

    http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLi ...

  9. 《C++程序设计语言(英文第四版)》【PDF】下载

    <C++程序设计语言(英文第四版)>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230382177 内容简介 本书是C++领域经典的参 ...

随机推荐

  1. ubuntu下操作端口的方法

    最简单的一个操作:sudo ufw status可检查防火墙的状态,我的返回的是:不活动 sudo ufw version防火墙版本: ufw 0.29-4ubuntu1 Copyright 2008 ...

  2. spark的若干问题

    问题1:SPARK与HADOOP之间的关系? spark是一种高效处理hadoop分布式数据的处理引擎.借助hadoop的yarn框架,spark就可以运行在hadoop集群中.同时spark也可以处 ...

  3. sonarQube使用maven进行检查

    1.在maven的中找到setting配置文件.在setting.xml中增加sonarqube配置.如下: <profiles> <profile> <id>so ...

  4. 3.25课·········JavaScript的DOM操作

    1.DOM的基本概念 DOM是文档对象模型,这种模型为树模型:文档是指标签文档:对象是指文档中每个元素:模型是指抽象化的东西. 2.Window对象操作 一.属性和方法: 属性(值或者子对象): op ...

  5. jquery中篇

    一.attr 返回属性值 返回被选元素的属性值. 语法 $(selector).attr(attribute) 参数 描述 attribute 规定要获取其值的属性. 属性 • 属性 o attr(n ...

  6. Struts2 内核之我见

    Struts2 内核之我见 完整分析 Struts2 内核中文文档 本文首先探讨了 Struts2 核心控制器的源码,以帮助解读 Struts2 的工作流程.接着讲解相关外围类.最后对 Struts ...

  7. tomcat7 中的坑。 关于welcome-list和servlet-mapping

    web.xml中, 使用default servlet设置了针对静态资源后缀名的过滤. 并且设置了welcome-list, 使用jetty和tomcat6启动一切正常, 但是使用tomcat7则出现 ...

  8. Storm 执行异常 java.lang.RuntimeException: java.nio.channels.UnresolvedAddressException 问题解决

    最近写的 binlog2kafka storm job 上线在一个新的集群环境中(storm 0.9.0.1, kafka 0.8), storm job 运行时报出如下异常: java.lang.R ...

  9. matlab画圆

    MATLAB rectangle函数1 语法说明rectangle('Position', pos)rectangle('Position', pos, 'Curvature', cur)rectan ...

  10. php函数decbin

    decbin()将十进制转换为二进制.必须有一个十进制参数.