Search code examples
javac#heap-memorystack-memory

Which object is created in which part of memory?


public class Order
{
    static Customer cust = new Customer();
    string sEmpty = "";

    public static void main(String args[])
    {
        int iTotal = 10;
        string sProductName = "Salt";
        Ship shp = new Ship();
    }
}

At the above code, which object and reference is created in the which part of memory? (I mean Heap and Stack)

alt text
(source: c-sharpcorner.com)


Solution

  • Since you tagged your question Java, I'll assume you meant in Java. Straight from the horse's mouth:

    The Java virtual machine has a heap that is shared among all Java virtual machine threads. The heap is the runtime data area from which memory for all class instances and arrays is allocated.

    JVM Spec

    Here is a link to a previous SO question that goes into this in serious detail (and is a language-agnostic discussion on the topic).

    Here's a link to an article from C# corner detailing the issue in C#.