In: Computer Science
Q.We design classes to create objects in java. Write step wise procedure for
(a).How these objects created in memory?
(b). Which area of memory is used for creation of the objects?
(c). What is difference between stack and heap memory in java?
Ques 1 How these objects created in memory?
In Java, all objects are dynamically allocated on Heap. This is different from C++ where objects can be allocated memory either on Stack or on Heap. In C++, when we allocate the object using new(), the object is allocated on Heap, otherwise on Stack if not global or static.
In Java, when we only declare a variable of a class type, only a reference is created (memory is not allocated for the object). To allocate memory to an object, we must use new(). So the object is always allocated memory on heap.
Java objects reside in an area called the heap. The heap is created when the JVM starts up and may increase or decrease in size while the application runs. When the heap becomes full, garbage is collected. During the garbage collection objects that are no longer used are cleared, thus making space for new objects.
Note that the JVM uses more memory than just the heap. For example Java methods, thread stacks and native handles are allocated in memory separate from the heap, as well as JVM internal data structures.
The heap is sometimes divided into two areas (or generations) called the nursery (or young space) and the old space. The nursery is a part of the heap reserved for allocation of new objects. When the nursery becomes full, garbage is collected by running a special young collection, where all objects that have lived long enough in the nursery are promoted (moved) to the old space, thus freeing up the nursery for more object allocation. When the old space becomes full garbage is collected there, a process called an old collection.
The reasoning behind a nursery is that most objects are temporary and short lived. A young collection is designed to be swift at finding newly allocated objects that are still alive and moving them away from the nursery. Typically, a young collection frees a given amount of memory much faster than an old collection or a garbage collection of a single-generational heap (a heap without a nursery).
Ques 2 Which area of memory is used for creation of the objects?
Before a Java object can be created the class byte code must be
loaded from the file system (with .class
extension) to
memory. This process of locating the byte code for a given class
name and converting that code into a Java class instance is known
as class loading. There is one class created for each type
of Java class.
All objects in Java programs are created on heap memory. An object is created based on its class. You can consider a class as a blueprint, template, or a description how to create an object. When an object is created, memory is allocated to hold the object properties. An object reference pointing to that memory location is also created. To use the object in the future, that object reference has to be stored as a local variable or as an object member variable.
Ques 3 What is difference between stack and heap memory in java?
JVM has divided memory space between two parts one is Stack and another one is Heap space. Stack space is mainly used for storing order of method execution and local variables.
Stack always stored blocks in LIFO order whereas heap memory used dynamic allocation for allocating and deallocating memory blocks.
Memory allocated to the heap lives until one of the following events occurs :
In contrast, the memory allocated to stack lives until the function returns. Below are the differences.
1 Basic
STACK
:- Stack memory is used to store items which have a
very short life like local variables, a reference variable of
objects
HEAP
:- Heap memory is allocated to store objects and
JRE classes.
2 Ordering
STACK
:- The stack is always reserved in a LIFO (last in
first out) order
HEAP
:- Heap memory is dynamic allocation there is no
fixed pattern for allocating and deallocating blocks in
memory
3 Size
STACK :- We can increase stack memory size by using
JVM parameter -XSS
HEAP
:- We can increase or decrease heap memory size by
using JVM option -Xms and -Xmx
4 Visibility
STACK :- Variables are visible to only to owner
thread
HEAP
:- It is visible to all threads
5 Exception
STACK :- JVM will
throw java.lang.StackOverFlowError
HEAP
:- JVM will throw java.lang.OutOfMemoryError
Hope u get the answer !!