In this article we will learn about Heap memory in Java and how to use heap,what is the purpose of heap memory in java.We shall discuss all these questions and this topic is one of the core concept in Java. A Heap is a memory area shared among all threads. The Java Heap is created on Virtual Machine startup. To create objects dynamically in a pool of memory called the heap. The heap is the run time data area from which memory for all class instances,variables and arrays is allocated.
If you need a new object you simply make it on the heap at the point that you need it.
For Example:
String s=new String("lucky");
In this case objects will be created one is on the heap and the second one is SCP(String Constant Pool) and 's' is referring to heap object.
Object creating in SCP is optional. If the object is not available then only new object will be created. Heap storage is reclaimed by the Garbage Collector. Heap may be of fixed size or may be expanded as required.
String s="lucky";
In this case only one object will be created if it is not already available and 's' is referring to that object. If an object already available with this can in SCP then 's' will refer that existing object only instead of creating new object.
In the above case object is not available for garbage collector even though the object does not have any reference variables. All the SCP objects will destroyed at the time of JVM shutdown.
In the case of heap there may be chance of duplicate String Objects
Example:
String s1 = new String("learn");
1.concat("programming");
String s2 = "learnprogramming";
System.out.println(s1);
System.out.println(s2);
Output:
learn
learnprogramming
At any point of time if we have heap object reference we can find it’s equivalent SCP Object byusing intern() method.If the equivalent SCP Object is not already available then intern() method will create a new object in the SCP.
Example:
class StringTest
{
public static void main(String[] args)
{
String s1 = new String("learn");
String s2 = s1.concat("programming");
String s3 = s2.intern();
String s4 = “learnprogramming”;
System.out.println(s3 == s4);
}
}
If you need a new object you simply make it on the heap at the point that you need it.
For Example:
String s=new String("lucky");
In this case objects will be created one is on the heap and the second one is SCP(String Constant Pool) and 's' is referring to heap object.
Object creating in SCP is optional. If the object is not available then only new object will be created. Heap storage is reclaimed by the Garbage Collector. Heap may be of fixed size or may be expanded as required.
String s="lucky";
In this case only one object will be created if it is not already available and 's' is referring to that object. If an object already available with this can in SCP then 's' will refer that existing object only instead of creating new object.
In the above case object is not available for garbage collector even though the object does not have any reference variables. All the SCP objects will destroyed at the time of JVM shutdown.
In the case of heap there may be chance of duplicate String Objects
Example:
String s1 = new String("learn");
1.concat("programming");
String s2 = "learnprogramming";
System.out.println(s1);
System.out.println(s2);
Output:
learn
learnprogramming
At any point of time if we have heap object reference we can find it’s equivalent SCP Object byusing intern() method.If the equivalent SCP Object is not already available then intern() method will create a new object in the SCP.
Example:
class StringTest
{
public static void main(String[] args)
{
String s1 = new String("learn");
String s2 = s1.concat("programming");
String s3 = s2.intern();
String s4 = “learnprogramming”;
System.out.println(s3 == s4);
}
}
If a program need more Heap memory than the automatic memory management system provides then Java Virtual Machine throws OutOfMemoryError. Heap memory does not need to be contiguous. All java objects live on the heap.When object contains another object variable that variable still contains just a pointer to yet another heap object.
Note: Keep in mind that,all java objects are constructed on the heap and that constructor must be combined with new keyword.
Recommended to Read:
No comments:
Post a Comment