Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How memory allocation takes place in scala

As we know that unlike java, scala has everything as object.

For example we have-

object A{
    val arg1=1
    def myFun(arg2:Int)=arg1
}

class A{  
    val arg1=1
    def myFun(arg2:Int)=arg1
}

trait A{
    val arg1=1
    def myFun(arg2:Int)=arg1
}
  • Now as everything in scala is an object so how the memory allocation will take place?
  • Is everything going to get memory in heap other than the reference variables ?
  • And in java when the class instance is created then the methods and variables in that class gets the memory in heap. How does it happens for a Singleton Objects here?
  • If everything is in Heap, will it not affect the performance?
  • As in Java, Memory is divided into 5 sections i.e. Heap, Stack, MethodArea etc. What about in scala, how does the memory allocation takes place?
like image 930
Apoorv Verma Avatar asked Dec 18 '15 06:12

Apoorv Verma


People also ask

How does memory get allocated?

There are two basic types of memory allocation: When you declare a variable or an instance of a structure or class. The memory for that object is allocated by the operating system. The name you declare for the object can then be used to access that block of memory.

How the memory is allocated in heap?

The heap is a large area of memory available for use by the program. The program can request areas, or “blocks”, of memory for its use within the heap. In order to allocate a block of some size, the program makes an explicit request by calling the heap allocation operation. In Java or C++, this is the new operator.

How do you allocate a variable in memory?

When a variable is declared compiler automatically allocates memory for it. This is known as compile time memory allocation or static memory allocation. Memory can be allocated for data variables after the program begins execution. This mechanism is known as runtime memory allocation or dynamic memory allocation.

Is heap allocated automatically?

The heap is a memory used by programming languages to store global variables. By default, all global variable are stored in heap memory space. It supports Dynamic memory allocation. The heap is not managed automatically for you and is not as tightly managed by the CPU.


1 Answers

Scala runs on the JVM and it's base on the Java library.

scala file(*.scala) will be compiled to the java class bytecode and run these on JVM. for your example:

object A{
    val arg1=1
    def myFun(arg2:Int)=arg1
}

will be translated to(decompile bytecode by javap):

public class A$ extends java.lang.Object{
    public static final A$ MODULE$;
    private final int arg1;
    public static {};
    public int arg1();
    public int myFun(int);
    public A$();
}

class A{  
    val arg1=1
    def myFun(arg2:Int)=arg1
}

will be translated to(decompile bytecode by javap):

public class A extends java.lang.Object{
    private final int arg1;
    public int arg1();
    public int myFun(int);
    public A();
}

trait A{
    val arg1=1
    def myFun(arg2:Int)=arg1
}

will be translated to(decompile bytecode by javap):

public interface A{
public abstract void $line5$$read$A$_setter_$arg1_$eq(int);

public abstract int arg1();

public abstract int myFun(int);

}

so for your other memory questions, I think it's same as Java.

like image 109
chengpohi Avatar answered Sep 25 '22 09:09

chengpohi