This discussion is archived
5 Replies Latest reply: Sep 3, 2009 2:20 AM by 807557 RSS

allocation areas in static method main()

807557 Newbie
Currently Being Moderated
Hi,

"Class objects and their associated static memories, along with objects created by static initialisation [...] are allocated in immortal memory." (from Andy Wellings, Concurrent and Real-Time Programming in Java).

And now I have a question about the allocations in this example:
public class Main {
  static RealtimeThread firstThread;

  public static void main(String[] args) {
    RealtimeThread secondThread = new RealtimeThread();
    firstThread = new RealtimeThread(somePriority, someScopedMemory);

    firstThread.start();
    secondThread.start();
  }
}
1. The thread firstThread with all non-static objects declared inside firstThread is allocated in ImmortalMemory, because it is declared as static!?
2. The run()-method of firstThread is executed in ScopedMemory, because a ScopedMemory object is passed during creation of firstThread to its constructor!?
3. secondThread is allocated and its run()-method executed in HeapMemory!?
4. Is the static main() method executed in HeapMemory or in ImmortalMemory (because it is declared as static)?

Thanks,
Gordon

Edited by: Gordon_Realtime on Apr 29, 2009 3:35 AM
  • 1. Re: allocation areas in static method main()
    807557 Newbie
    Currently Being Moderated
    Gordon,

    Only static initializers are executed in ImmortalMemory:
    static Object x = new Object(); // allocated in Immortal
    static {
       // any allocation here is in Immortal
    }
    In addition Class objects and interned String objects are always in ImmortalMemory. So ...
    1. The thread firstThread with all non-static objects declared inside firstThread is allocated in ImmortalMemory, because it is declared as static!?
    No, it's allocated in heap because main executes with heap as the allocation context.
    2. The run()-method of firstThread is executed in ScopedMemory, because a ScopedMemory object is passed during creation of firstThread to its constructor!?
    Yes.
    3. secondThread is allocated and its run()-method executed in HeapMemory!?
    Yes.
    4. Is the static main() method executed in HeapMemory or in ImmortalMemory (because it is declared as static)?
    main() executes in heap.

    David
  • 2. Re: allocation areas in static method main()
    807557 Newbie
    Currently Being Moderated
    Are you all only talking about the RTSJ with respect to the "ImmortalMemory" and static initializers?

    This is not the case with non-realtime jvm's... right?
  • 3. Re: allocation areas in static method main()
    807557 Newbie
    Currently Being Moderated
    -snacker- wrote:
    Are you all only talking about the RTSJ with respect to the "ImmortalMemory" and static initializers?
    Yes.
    This is not the case with non-realtime jvm's... right?
    Non-RT JVMs don't have ImmortalMemory.

    David Holmes
  • 4. Re: allocation areas in static method main()
    807557 Newbie
    Currently Being Moderated
    Hello David,

    I would appreciate further assistance on this issue;
    I wrote a short test to see how the member variables declared as static is allocated as follows;
    I was expecting that the str2,3,4 would be allocated in the heap. But the output shows otherwise.

    What am I missing?

    Jason
    --------------------------------------------------------------

    import javax.realtime.MemoryArea;

    /**
    * A simple tester class trying to see how the JVM allocates memory
    */
    public class MemAllocator {
         private static MemAllocator memAllocator;
         
         static String str1;
         String str2 = "str2 - no static String its value assigned as it is declared";
         String str3 = new String("str3 - no static String created in new String()");
         String str4;
         
         static String str5;
         
         // Allocation in static initializers occur within ImmortalMemory
         static {
              str5 = "str5 - static String its value assigned in a static initializer";
         }
         
         /**
         * constructor
         */
         public MemAllocator() {
              str1 = "str1 - static String object its value assigned in the constructor";
         }
         
         private void printMemarea(Object obj) {
              System.out.println(obj.toString() + " allocated in " +
                        MemoryArea.getMemoryArea(obj));
         }
         
         private void runTests() {
              str4 = "str4 - no static String variable its value assigned in a class method";
              memAllocator.printMemarea(str1);
              memAllocator.printMemarea(str2);
              memAllocator.printMemarea(str3);
              memAllocator.printMemarea(str4);
              memAllocator.printMemarea(str5);
         }

         public static void main(String[] args) {
              memAllocator = new MemAllocator();
              memAllocator.runTests();
              System.exit(0);
         }

    }


    /**
    * === output
    * str1 - static String object its value assigned in the constructor allocated in javax.realtime.ImmortalMemory@186d4c1
    * str2 - no static String its value assigned as it is declared allocated in javax.realtime.ImmortalMemory@186d4c1
    * str3 - no static String created in new String() allocated in javax.realtime.HeapMemory@f9f9d8
    * str4 - no static String variable its value assigned in a class method allocated in javax.realtime.ImmortalMemory@186d4c1
    * str5 - static String its value assigned in a static initializer allocated in javax.realtime.ImmortalMemory@186d4c1
    */
  • 5. Re: allocation areas in static method main()
    807557 Newbie
    Currently Being Moderated
    joohwannoh wrote:
    What am I missing?
    In addition to static initializers executing in the context of Immortal Memory, certain kinds of objects are always allocated there, including: Class objects, string literals and interned Strings.

    Your examples all use string literals.

    David Holmes