Benchmarking speed, safety, ease of use, memory footprint and reasons to use Erlang

Aarya Joshi
An analysis of Erlang
7 min readApr 22, 2019

After going through a series of posts on installation, usage and writing different programs in Erlang, we now proceed to analyze its performance. Erlang is mostly used in telecommunication applications, having support for concurrency and distribution.

For our previous posts, please refer to below links:

Installation,Simple Program Execution and Package Manager

Data Types, Expressions and Abstraction

Debugging and Testing Support

Intermediate Representations, Toolchain and Internals of Erlang

Speed:

Erlang Compiler provides many attractive features, concurrent Garbage collection, string concatenation without allocating more memory, etc. These may be useful features when trying to run a continuous server, desktop or mobile OS but also result in slow application of the program.

A Research on C++ and Erlang for Motorola Telecoms Software found that Pure Erlang was twice as fast as C++, owing to its fast and lightweight process management, but Erlang seems to perform worse with mathematical functions. To test this theory, we refer to a previously conducted experiment by Stack Overflow user “ mkrieger1”.

The Experiment: To find the first triangular number with more than 1000 divisors. A triangle number is a series obtained by continuous summation of the natural numbers; 1(1), 3(1+2), 6(1+2+3), 10(1+2+3+4), 15(1+2+3+4+5).

As we see the results above, it’s obvious that Erlang performs much worse than C and Python. If we take C as the standard, then Erlang performs 337% worse.

To be fair, this is a mathematical function test, something Erlang was not intended for when created and is an issue that can be mediated by using HiPE. This is useful when implementing mathematical functions, but does have its limitations with Garbage collection and Scalability. This seems quite ironic because Erlang is built to make Scalable concurrent programs.

All objects in Erlang are immutable, a feature that makes the program less confusing — but also adds execution time — as each operation on any part of a string object would mean the entire thing would have to be copied to a new location. (Solution) However, this can also be solved by using NIfs (Native Implemented Functions), an interoperability feature that allows the programmer to use C functions inside an Erlang program. Going with the results of the Benchmarking test above, this would mean we could achieve the execution speeds of C, without losing the parallelism and concurrency with Erlang.

Safety :

Erlang is mainly intended for real-time systems where required response times are in order of milliseconds. It is widely used in distributed systems. For this kind of systems, safety is a crucial requirement. One of the key components for a language to be called a safe language is how it handles errors. Moreover, run-time errors are crucial in real-time systems.

Erlang provides various constructs to handle run-time errors.

Run-time errors are those errors which occur when the program is run. One of the examples of run-time error can be when an operator is applied to arguments of wrong datatypes. Erlang has built-in methods to handle run-time errors.

To prevent run-time error within a process, we can use, “catch” or “try”. If an exception occurs, then the program will exit safely providing the reason behind the error.

Erlang has a real-time garbage collector. Memory is allocated automatically when it is required and is also deallocated automatically when that memory is no longer required. Having the automatic garbage collector, it prevents the typical errors associated with the memory.

Memory Leak

Memory leak refers to the memory which is allocated but not used. Coming to Erlang, it is very poor and always requests memory from the OS. If the allocated memory is not used properly by Erlang, we call it a memory leak.

Identifying a memory leak

When we start an application on the Erlang node, it usually consumes few MB of RAM from the OS. We can see the RAM consumption using top -p <beam PID> command.

Ease of Use

Erlang is a programming language which writes the errors in the same language in which we write the code, unlike other languages. Learning the syntaxes of Erlang is the easy part. Learning the Erlang libraries and using them is a bigger task. Sequential language processing is not difficult. This is where the beauty of Erlang syntax shows. Match expressions make code beautiful. Next comes the use of case functions. By returning objects from case expressions, the programmer is forced to decide what is correct for all branches. I think this reduces bugs.

The fundamental issues related to concurrency affect any software. For example, how to coordinate access to shared resources is a problem that could affect any program. Many of the developers tend to ignore it. This is because the basic solutions provided by most programming languages are good enough. When you have some real strong requirements in terms of concurrency, you might adopt a functional programming language. Then comes Erlang.

Successful companies use Erlang and Elixir. Elixir is a concurrent, functional and general-purpose programming language that runs on the Erlang Virtual Machine. Erlang is a stable and trusted language which is used to run core procedures within major systems. It is invented at Ericsson. Erlang is used by many industries which include Fintech, Blockchain, Internet of things (IoT) and Security.

Companies choose Erlang and Elixir because of the ease with fault-tolerant and scalable programs deployable in a distributed network can be written. Nowadays we are seeing a wider adoption of Elixir in production, especially for those companies which are used to the syntax of Ruby.

Everyone knows WhatsApp. It is the most popular messaging application used by billions of users daily. But, did you ever think of the backend of this application? WhatsApp uses a surprisingly smaller number of engineers to build and manage this. How do you think they are managing all this? They use Erlang. One of the best attributes of Erlang is concurrency. When it comes to programming languages, Erlang is the best multi-tasker. With other programming languages, it is not possible to run multiple messages and multiple parallel conversations with the efficiency of Erlang. There are other advantages with Erlang as well. Bugs and updates can be fixed and installed without any downtime. Erlang is built to solve many very specific problems like building highly scalable large systems without compromising on reliability. These are the properties that make Erlang very appealing.

Memory Footprint

Memory footprint refers to the amount of main memory that a program uses or references while running. The word footprint generally refers to the extent of physical dimensions that an object occupies, giving a sense of its size.

erlang:memory().

When looking at memory usage in a running system, the most basic function to get information from is erlang:memory(). It returns the current memory usage of the system. From the below image, we can observe that in total 14435024 bytes are allocated to the Erlang process. There are two sub-categories processes and system. The remaining categories are either a component of a system or a component of processes.

Tells details about Erlang memory usage

To program efficiently, it is important to have knowledge about how much memory different data types and operations require. It is implementation dependent on how much memory the Erlang data types and other items consume, but here are some figures for the erts-5.2 system.

The unit of measurement here is the memory words. There exist both a 32-bit and 64-bit implementation, and a word is therefore 4 bytes or 8 bytes respectively.

Memory size of different data types

Reasons to Use Erlang:

1. Asynchronous: The basic nature of real world is asynchronous and similar is Erlang. The main feature that helps to prove that Erlang is asynchronous is its free processes.(it can run more than 32000 processes concurrently)

2. Supervision: There are many different strategies to supervise the processes such as restart strategies, thresholds. These strategies help recover data from corner cases or overloading, for postmortem analysis, maintain traces of the problems for later trouble shooting.

3. Resource Management: The main default process scheduler operates with O(1) scaling complexity. This features helps for resource monitoring, scheduling strategies.

4. Horizontal Scalability: This feature helps to distribute the processes across the machine through intra and inter communications of the machine. The built-in database which is Mnesia is also distributed by its nature itself.

5. Vertical Scalability: This feature is very useful to distribute jobs to the various processing resources on the same machine.

6. Live debugging: We are able to log the errors from the live nodes which can help even in later trouble-shooting activities. At this time we are able to have full access to any process’ running state. There are some built-in error reporting tools which can be very useful.

7. Code Hot-Swapping: It is an ability to update or upgrade the code live during operations.

Examples where Erlang is greatly used:

· Whatsapp: They use Erlang to run messaging services and are able to achieve 2 million connected users for each server.

· Motorola: Erlang is used for call processing products in the public-safety industry.

· Amazon: Erlang is used by amazon to implement SimpleDB, which is database service as a part of the Amazon Elastic Cloud Compute (EC2).

--

--