`

Erlang memory architecture vs Java memory architecture

阅读更多

I read a really, really interesting article on memory management strategies for the Erlang VM. It was written as a thesis by Jesper Wilhelmsson: http://www.it.uu.se/research/publications/lic/2005-001/ I thought it might be nice to discuss the differences between Erlang's memory setup and Sun's Java VM.

As a real short introduction for those who have never heard of Erlang; it is a functional language that uses asynchronous message passing as its basis for concurrency. The message passing uses copy semantics, making distribution over more than one Erlang VM, running on more than one machine essentially transparent to the programmer.

Erlang and Java are similar in the sense that both use a virtual machine to abstract the hardware into a portable layer. Both languages employ machine independent byte code. Both run-time systems rely on garbage collection to free the programmer of doing memory management.

Threading overhead in Erlang is very low. I believe that the memory requirements for a thread in Erlang is about 512 bytes. In Java threads typically need about 512 kilobytes, about 1000 times more. For a programmer, the upshot is that creating threads to do some work asynchronously is not something you have to sit down and think about. Typical Erlang systems have thousands or tens of thousands of threads. There is no futzing with threadpools and executors like we do in Java.

From what little I have dabbled with it, I found Erlang to be a pleasant compromise between a functional language, and a language that allows you to write real-world applications. (I know I'll get flak for this) Robust distributed error handling is a pleasant surprise and writing a network server of any kind is actually easy. The state-machine approach to web servers makes rolling back on errors completely natural.

But this post is not about the programming model of Erlang. It is about the way the Erlang VM deals with memory.

The current Java virtual machine uses what an Erlang programmer would call a shared heap topology. There is one big heap that is used by all threads. Most memory is allocated on that heap. In addition to the heap, the JVM uses some specialised data areas like the code cache and the permanent generation. These too are shared between all threads.

By contrast, Erlang uses a private heap topology. Each thread has its own tiny heap that contains all data the thread uses and the thread's stack as well. All data for a thread is on that local heap. It is reserved when the thread is created. When the thread dies, the entire heap is simply returned to the pool of free memory.

Aside from the private heaps, all threads share access to a so-called binary heap and a message heap. These are specialised heaps. The binary heap is for allocating large chunks of arbitrary data that are likely to be shared between threads. This is where for example file input or network buffers live.

The message heap is a heap for data that is used in messages. Messages too are shared between processes. Messages are passed between threads by copying a pointer over from the sending thread to the receiving thread. The data for the message is stored on the message heap.

I was impressed by the Erlang memory model. It just strikes me as a lot more scalable than Java's single heap model. The language semantics and the memory model match beautifully.

For instance; the simple fact that heaps are private to a thread relieves the threads of all forms of lock checking on their own data. Add to that the fact that there are no destructive writes and suddenly there is no need for lock checking for shared data either.

The latest version of the Erlang VM take this yet another step further by having more than one scheduler. Once scheduler per physical processor to be precise. This eliminates another entire class of locks to check. Only when a scheduler is bored it needs to go out, gather a lock and get some processes off of another scheduler.

In Java, we have a lot to learn still. That said, we have a few nice things in Java that I miss working with large Erlang systems.

The Erlang VM will reallocate and grow heaps when thread accumulate a lot of data. However, the reallocation algorithm causes heap sizes to grow rapidly. Under high load, we have seen Erlang VM's eat up 16GB of RAM in a matter of minutes. Each release has to be carefully load tested to see if its memory requirements are still sane.

There are no mechanisms in the Erlang VM to curb the growth of the memory. The VM will happily allocate so much memory that the system shoots into swap, or that the virtual memory is exhausted. These may cause the machine to become unresponsive even to KVM console access. In the past we have had to power cycle machines to get access to them again.

The queue-based programming model that makes Erlang so much fun to write code for, is also it Achilles heel in production. Every queue in Erlang is unbounded. The VM will not throw exceptions or limit the number of messages in a queue. Sometimes a process stops processing due to a bug, or a process fails to keep up with the flow of messages being sent to it. In that case, Erlang will simply allow the queue for that process to grow until either the VM is killed or the machine locks up, whichever comes first.

This means that when you run large Erlang VM's in a production environment you need to have OS-level checks that will kill the process if memory use skyrockets. Remote hands for the machine, or remote access cards is a must-have for machines that run large Erlang VM's.

In summary, for every day performance I believe the private heap memory model to be a very powerful tool in Erlang's box. It cuts whole classes of locking mechanisms out of the run-time system and that means it will scale better than Java will for the same purpose. Java's hard limits on memory will save your bacon when your system is being flooded or DDoSed.

Kees Jan

PS. There is a command line switch for Erlang's VM to switch it from using the private heap topology to using the share heap topology.

PPS. I like Erlang and Java. They are hard to compare because the have so little in common for a developer. In general, I would use Java for most systems, though. Tool support is better and the number of libraries available is staggering. I would opt for Erlang in the case where I have a stream oriented messaging system. That's when the Erlang programming model really shines.

<!-- / message -->

分享到:
评论

相关推荐

    Erlang-or-java.rar_erlang

    标题中的“Erlang-or-java.rar_erlang”表明这是一个关于Erlang和Java之间通信的示例项目。Erlang是一种面向并发、分布式、容错的编程语言,常用于构建高可用性和高并发性的系统,而Java则是一种广泛应用的通用编程...

    erlang调用java

    `Erlang`和`Java`都是在各自领域有着独特优势的语言。`Erlang`以其强大的并发处理能力和容错性著称,而`Java`则有丰富的库支持和广泛的企业级应用。将两者结合,可以充分利用它们的优势。本文将详细探讨如何在`...

    erlang-java聊天

    【Erlang与Java交互实现聊天程序】 在IT领域,Erlang和Java都是重要的编程语言,各有其独特的优点和应用场景。本项目“erlang-java聊天”是一个初级水平的示例,展示了如何利用这两种语言进行交互,实现一个简单的...

    Erlang_Architecture_Behaviours:Erlang架构行为

    6. **项目学习与实践**:通过研究Erlang_Architecture_Behaviours-master中的源码,开发者可以学习如何在Erlang中实现这些架构,理解如何使用Erlang的特性来构建高可用、高性能的系统。此外,这个项目也可以作为模板...

    java php python erlang 千万级内存数据性能比较

    本文将深入探讨Java、PHP、Python和Erlang这四种语言在处理千万级内存数据时的性能差异。 首先,让我们从Erlang开始。Erlang是一种并发性极强的函数式编程语言,特别适合构建分布式、容错系统。在提供的文件"erlang...

    Erlang虚拟机内存管理

    Erlang核心开发者Lukas Larsson在2014年3月份Erlang Factory上的一个演讲详细介绍了Erlang内存体系的原理以及调优案例 根据siyao zheng博客上听写的资源进行的翻译,大致只翻译了80%但核心部分已经完整,希望对大家...

    基于erlang后台,java swing前端开发的qq聊天系统

    本系统是基于erlang开发的后台,java swing开发的前端的qq聊天系统,希望能给初学erlang的人带来一点小小的帮助,具体操作步骤见redme.txt文件 功能如下: 功能: 1.用户登录功能,账号和密码从服务端的mysql数据库...

    erlang编程 Introducing Erlang

    **Erlang编程:Introducing Erlang** Erlang是一种函数式编程语言,由爱立信在1986年开发,主要用于构建高可用性、容错性和并发性的分布式系统。"Introducing Erlang"是Simon St. Laurent撰写的一本入门级教程,...

    一个我自己学习Erlang的聊天室服务器及客户端代码

    本项目提供了一个使用Erlang编写的聊天室服务器端代码以及Java编写的客户端代码,这为我们深入理解Erlang的并发特性和Java与Erlang的交互提供了实践案例。 一、Erlang聊天室服务器端 1. 并发处理:Erlang的轻量级...

    erlang_版本24.3.4.4

    - **BEAM虚拟机**:Erlang的运行时系统,全称是BIFs (Built-In Functions)、Erlang、Assembler and Memory管理器。 - **OTP行为**:如Supervisor(监督者)、GenServer(通用服务器)、GenEvent(事件处理器)等,...

    c# 版ErlangOtp跨平台通信框架(Java版的转译)

    标题中的"C#版ErlangOtp跨平台通信框架(Java版的转译)"指的是一个用C#语言实现的框架,其目标是提供与Erlang OTP系统进行跨平台通信的能力。Erlang OTP(开放电信平台)是Erlang编程语言的一个核心组件,它包含了一...

    基于erlang的文件存储

    本项目“基于Erlang的文件存储”就是这样一个尝试,它利用Erlang强大的并发处理能力和分布式特性,为服务端提供稳定的基础架构,而客户端则通过Java的Swing组件提供用户友好的交互界面。以下是对该项目中涉及的技术...

    可在ubuntu上安装erlang的deb包

    6,安装失败,你骂我好了,并把你的ubunt版本、是否是在虚拟机和其下的虚拟机软件名称和版本与再其下的os名称和版本、硬件(cpu、memory、Mainboard、disk)等信息email给我,我帮你找能用的版本。 软件相关信息: ...

    程序员开发erlang的资料

    5. **Jinterface**:Jinterface是Erlang和Java之间的互操作库,它允许Erlang进程与Java虚拟机(JVM)中的对象进行通信。这对于既有Java基础又想利用Erlang优势的开发者来说,是一个强大的工具。 6. **Eclipse插件...

    Erlang 中的Module级别热部署

    ### Erlang中的Module级别热部署 #### 一、引言 Erlang 是一种专为构建高并发、容错性强的分布式系统而设计的编程语言。它的独特之处在于支持轻量级进程(也称为协程)和热部署能力。本文将深入探讨Erlang 中的 ...

    Erlang/OTP 26.2.1

    Erlang/OTP 26.2.1,Erlang,OTP,26.2.1

    erlang25.0 windows版本

    Erlang是一种高级编程语言,特别适用于并发、分布式和实时系统。它由Ericsson公司开发,主要用于构建高可用性、容错性和可扩展性的软实时系统。Erlang的25.0版本是该语言的一个更新,针对Windows操作系统进行了优化...

    erlang programming

    8. **Erlang与其他技术的集成**:Erlang可以与其他语言如Java、Python等集成,用于构建混合系统。例如,使用Erlang的Ranch和Cowboy库可以构建高性能的Web服务器和API。 9. **实时性与并发性**:Erlang的实时性使其...

    erlang安装包.zip

    erlang安装包

    erlang资源

    这个“erlang资源”包含两本PDF书籍——《Erlang并发编程》和《Erlang入门手册》,它们是深入理解和学习Erlang语言的关键资料。 《Erlang并发编程》这本书可能涵盖了以下知识点: 1. **并发模型**:Erlang的并发...

Global site tag (gtag.js) - Google Analytics