7 Replies Latest reply on Mar 2, 2010 4:45 PM by 843789

    static memory allocation

    843789
      Hi,Consider the code shown,

      1. class A{
      2.
      3. public static void main(String ... args)
      4. { 
      5. int m;
      6. m = 30;
      7. }
      8. }

      Execution of which line allocates a memory for 'm'?
        • 1. Re: static memory allocation
          843789
          coolism wrote:
          Hi,Consider the code shown,

          1. class A{
          2.
          3. public static void main(String ... args)
          4. { 
          5. int m;
          6. m = 30;
          7. }
          8. }

          Execution of which line allocates a memory for 'm'?
          If I'm not mistaken, it's line 5 because that's when the object is created...
          • 2. Re: static memory allocation
            EJP
            And that isn't 'static memory allocation'. It is dynamic. If it was static you couldn't associate it with a line of code.
            • 3. Re: static memory allocation
              843789
              ejp wrote:
              And that isn't 'static memory allocation'. It is dynamic. If it was static you couldn't associate it with a line of code.
              ya true, it is not static allocation
              • 4. Re: static memory allocation
                843789
                coolism wrote:
                Hi,Consider the code shown,

                1. class A{
                2.
                3. public static void main(String ... args)
                4. { 
                5. int m;
                6. m = 30;
                7. }
                8. }

                Execution of which line allocates a memory for 'm'?
                There are no local variable declarations (as such) at the bytecode level. Every method has a maximum frame size n; 1 in your case. When the method is invoked its frame is allocated on the stack so as to accommodate n local variables at any given time. So allocation happens before the method body is entered.

                With kind regards
                Ben
                • 5. Re: static memory allocation
                  843789
                  coolism wrote:
                  Execution of which line allocates a memory for 'm'?
                  When main is called (by the Java runtime system). The m variable will be allocated on main's stack. It will be deallocated when the stack is discarded which happens when main is left.
                  • 6. Re: static memory allocation
                    843789
                    javap -c Test
                    Compiled from "Test.java"
                    public class Test extends java.lang.Object{
                    public Test();
                      Code:
                       0:   aload_0
                       1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
                       4:   return
                    
                    public static void main(java.lang.String[]);
                      Code:
                       0:   bipush  30
                       2:   istore_1
                       3:   return
                    
                    }
                    • 7. Re: static memory allocation
                      843789
                      Now if you change the code to
                      public class Test {
                      
                           public static void main(String... args) {
                                int m=30;
                           }
                      }
                      you would get
                      javap -c Test
                      Compiled from "Test.java"
                      public class Test extends java.lang.Object{
                      public Test();
                        Code:
                         0:   aload_0
                         1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
                         4:   return
                      
                      public static void main(java.lang.String[]);
                        Code:
                         0:   bipush  30
                         2:   istore_1
                         3:   return
                      
                      }
                      which is the same.

                      The idea is that it doesn't make a difference how you write it. Memory allocation happens when is decided after the JVM optimization and not your written code.