UrbanPro
true

Learn Programming Languages from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Memory Management In JAVA

Pavan K.
07/02/2018 0 0

Memory Management in JAVA:

When you are starting the JVM then JVM will request some memory from the OS. if OS allocates the required memory then JVM will start otherwise the error message will be displayed and JVM will not start.

 

For example,

public class MemoryManagement {
public static void main(String str[]) {
System.out.println("**Main Started**");
Runtime rt = Runtime.getRuntime();
System.out.println("T : "+rt.totalMemory());
System.out.println("M : "+rt.maxMemory());
}
}

 

Execute as follows:

java -Xms512m MemoryManagement

java -Xmx1024m MemoryManagement

java -Xmx2048m MemoryManagement

-Xms stands for Initial Memory

-Xmx stands for Max Memory

 

When a JVM executes a program, it needs memory to store many things like:

i. Byte Codes and other information extracted from loaded class files.

ii. Objects created in your program.

iii. Methods Parameters.

iv. Values returned from methods.

v. Local variables declared.

vi. Intermediate resultsof computations.

The JVM organizes the memory into several runtime data areas. Some are:

1. Method Area:

  • When the class is loaded by the JVM then the class information will be stored into the method area.
  • Method area is also called as Class Heap.
  • Static Variable also gets the memory in method area.
  • Each instance of the JVM has one method area.
  • This area is shared by all threads running inside the virtual machine.
  • These memories should not be de-allocated during the execution of the application.

2. Heap Memory:

  • JVM places all objects into the heap memory.
  • Each instance of the JVM has one heap area.
  • This area is shared by all threads running inside the virtual machine.
  • If you have the reference of the object in your application then it is known as USED or LIVE object.
  • if you do not have the reference for the object in your application then the object is known as UNUSED or DEAD object and is called as Garbage.
  • There is no guarantee that memory for the UNUSED or DEAD objects will be de-allocated immediately.
  • When you create the object and required memory is not available in the HEAP then OutOfMemoryError will be thrown by JVM.

3. Stack Memory:

  • Stack Memory keep track of each and every method invocations. This is called Stack Frame.
  • Each thread has its own PC Register(Program Counter) and Java Stack Frame.
  • When the method or constructor is invoked then the implementation will be copied into the stack memory on the top of the stack. after completing the execution of the method or constructor, memory from the stack will be de-allocated.
  • Memory for local variables and method arguments are allocated in Stack Memory.
  • The program counter always keeps track of the current instruction which is being executed. After execution of an instruction, the JVM sets the PC to next instruction.
  • When you invoke method or constructor and required memory is not available in the STACK then StackOverflowError will be thrown by the JVM.
0 Dislike
Follow 2

Please Enter a comment

Submit

Other Lessons for You

AR ending verb(regular) in Spanish
Hablar- to speak Yo hablo Tú hablas él/ella/usted habla nosotros hablamos vosotros habláis ellos/ellas/ustedes hablan

Cash Flow Statement Definition and meaning for Class 12th Accountancy
A cash flow statement is an important tool used to manage finances by tracking the cash flow for an organization. This statement is one of the three key reports (with the income statement and the balance...

Culture in Shorts
1. Infostretch introduced a virtual engagement initiative called Infostretch's Got Talent, which was an intense 3-week event on various segments such as Dance, Fitness & Singing. Employees participated...

Advanced Mathematics for your children
Every one of you want to make a strong base in math of your children: At first, try to habituate you children by using the table- learning table, helps your children to make sharp their brain and they...

How To Write Your English Board Paper
Dear Students,Greetings!Your Board Papers are around the corner, and as usual, you've given the least time to your subject ENGLISH. But you do not have to take the stress. Here are some pointers for you...

Looking for Programming Languages Classes?

Learn from Best Tutors on UrbanPro.

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for Programming Languages Classes?

The best tutors for Programming Languages Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Programming Languages with the Best Tutors

The best Tutors for Programming Languages Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more