Decoding Scala without the Code
By: Jyoti Dhiman
Tags: Big Data, Java, Data Science, Scala, Data

Decoding Scala without the Code

Photo by Nathan Dumlao on Unsplash

I came across Scala while working with Spark, which in itself, is written in Scala. Coming from a Python background and with little to none Java knowledge, I found Scala a bit confusing in the beginning, but over time, it grew on me and now, it is my preferred language for most use cases.

With experience, I have picked up a few bits and pieces of scala and its workings. Please read on to find out a bit more about scala, mainly the non-coding part, how exactly code turns to execution. Maybe you would be using it for quite some time but something can still surprise you.

Also, I won’t go into how to code in Scala as I believe there is no better way to learn a language than to build something in it and which can be a discussion for another time.

What makes Scala cool?

My first impression of Scala was Java++ (okay, this is pretty lame), but I saw Scala as a better Java with an almost similar structure and lesser code.

Scala(which stands for scalable language) is a combination of object-oriented & functional programming language which runs on JVM. As of October 2020, the latest version of Scala is 2.13.3

Everything is an object in scala, in fact, scala is said to be more object-oriented than Java. Let’s see how:

Integers like 1, 2 are objects of Int class, and methods of Int class can be called on integers, For instance, you can call a method over integer like:

print(-1.abs) # absolute value of -1

1

Functions are objects of Function trait in Scala. (Read)

Scala allows for lazy evaluation, which can be used for the optimized use of resources. Scala’s lazy evaluation allows for expressions, code not to be evaluated until its first use. This feature of Scala is the basic principle in the workings of Spark, i.e. Spark is lazily evaluated.

Scala automatically does Type Interference, we don’t always need to define the type of a variable when declaring. In most cases, the type of variable can be detected at compile time by the compiler. (How type inference works)

Scala appears to be interpreted but it is actually compiled making it faster, though at the first glance Scala appears to be an interpreted language, at least to me. Because we have a scala shell available, where we can execute test code, just like Python, I also assumed scala to be an interpreted language.

The scala command starts an interactive shell where Scala expressions are interpreted interactively. The shell is REPL, which under the hood compiles the code & run the compiled code.

Scala has close integration with Java, so you can write code as a mix of Java & Scala if needed. Also, almost all the Java libraries can be imported into Scala.

Lastly, Scala is written in Scala, that is how cool it is! (reference)

How does Scala code work?

Scala code is compiled into Java Byte Code which is executed by the Java Virtual Machine (JVM) which essentially is how we can use a mix of Java & Scala code.

The JVM is an abstract computing machine that enables a computer to run a Java program. Since the scala code is compiled into a Java Byte Code, it is executed via JVM.

JVM is basically Java god(also for us scala peeps), the sooner you learn it, the better. It takes care of everything, from loading the code, verification of the code, execution of the code, memory management, allocation of resources from the OS, and lastly, a run time environment to do it all.

Memory Management in Scala

Scala runs on JVM, so it employs memory management similar to Java. All execution is dependent on memory & we can define memory management as allocation of resources from OS, and freeing up unnecessary memory to not run into OOM errors. (will be discussed later)

On a higher level, JVM memory consists of three parts: Heap, Stack & Other. Let’s try to understand each component one by one.

JVM Memory Architecture(Image created by me, on http://draw.io/)

Heap

Heap stores the actual objects and is created when JVM starts up. There is only one heap space which is shared across all JVM processes. Also, note that Garbage Collector(or GC), runs only on heap memory.

The heap can be of fixed or variable size, depending upon the GC strategy. User can set maximum heap size using the –Xmx option which is by default 64 MB.

Stack

The stack area is created with the thread. It stores local reference to the objects stored in the heap memory.

Due to its LIFO structure, a stack works perfectly for clearing up the space for methods, as for that, we don’t need to randomly access data.

Other

This section stores JVM specific code and data.

Garbage Collector(special mention):

Over time, unreferenced objects can cause heap memory to grow, cleanup of which is an essential component of memory management. But as a user, we don’t have to worry about cleaning up these unreferenced objects, called Garbage, ourselves. GC or Garbage Collector does this for us.

Garbage Collector is a daemon which cleans up unnecessary memory occupied by unused or unreferenced objects.