Erlang Deep Dive
Now that we’ve covered the basic workings of Erlang, let’s dive deep into the nitty gritty. We looked at how to create a simple Erlang code which prints ‘Hello World’ on the screen and have also looked into the Erlang shell and ways to access it across different operating systems. Now let’s look at some more complex code in Erlang and discuss some key features of the language regarding these codes.
Most of the arithmetic operators used in Erlang are normal mathematical operators and perform the same functionality as they do in mathematics. We are not going to discuss a lot about these operators, however; it is important to discuss the operators that we don’t usually see in mathematics or other programming languages.
● =/=
The ‘exactly not equal to’ operator is the opposite of the ‘exactly equal operator’ that is present in other programming languages. This operator checks if the values on either side of it are exactly equal or not, and if they are not then the operator returns true, otherwise it returns false.
Eg. 4=/=4
Result: False
How to represent a text string in Erlang?
In the previous blog we saw how we must print a string value on the Erlang shell. We are now going to talk about initializing a string value in Erlang. You’ll be surprised how easily a string value is initialized and processed.
In Erlang, every character consumes 8 bytes of memory in a 32-bit machine and twice as much on a 64-bit machine. Since a string value is nothing but a combination of characters, from a memory standpoint the following two initialization statements are exactly the same:
a= “Hello”. b=[10,11,12,13,14].
Some operations in Erlang:
size(). —-> Gives the number of characters in the variable.
reverse(). —->Reverses the string values
timer(). –> Measures the time elapsed for a step to run in Erlang.
erlang: system_info(allocated_areas). —> Gives reports about the globally allocated structures.
erlang: process_info(self(),memory). —–>Memory information about individual processes.
Points to Ponder:
1. Like any other programming language, memory consumption and allocation can sometimes be a tricky issue in Erlang.
2. The typical approach to ensure that we do not face any memory issues are:
- Checking if the memory use of the operating system is not unexpectedly high.
- Use the Erlang Shell commands to ensure that the Erlang processes are not going out of control at any point of time. The processes often go in queues and overburden the memory allocation process with unnecessary pressure.
- Check the memory allocated to binaries as the normal processes are kept on the process heap and are garbage collected. The big binary data has a major impact on the system.