Runtime vs compile time memory allocation in java

前端 未结 4 2104
野的像风
野的像风 2021-01-03 06:11

I am confused regarding whether memory allocation in java occurs at run time or compile time.

For example:

class Test{
  int a;
  public Test(){
             


        
4条回答
  •  清歌不尽
    2021-01-03 06:39

    Compile time no memory allocation happens. Only at load and runtime.

    Compile time generates .class files that's it.

    Remember you need to have a main class to run the program. When you run your program using Java with classpath to .class file, there will be steps like loading & linking etc.,

    Classloaders loads the files to permgen.

    When main method invoked, there will be stack created and local variables will be placed there

    When runtime encounters new it creates object on heap and allocates required memory there like memory required for Test.

提交回复
热议问题