• Tech Travel Hub is your one stop, ultimate tour guide to all things tech, travel, visas and digital nomads
  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact
Menu
  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact
View Blog
April 5, 2023April 5, 2023

How Memory Allocation Happens in Java?

This post is all about a small introduction about how memory is allocated in java.
When JVM is getting started , Underlaying Operating system allocates a chunk of memory to JVM.
This chunk size is dependent on the version of OS, Version of java and total memory available to OS.
So whenever JVM gets some memory it divides the area into the below four parts:

JVM divides the memory into four main sections:

  • Heap area
  • Stack area
  • Code area
  • Static area

A quick way to explain java memory allocation is , java mainly keeps it data to heap and stack area. 
Heap Area/Memory:

Heap area is to keep the live objects and may contain reference variables.Instance variables are created in the heap.Mainly the heap area is the garbage collect able area.

Stack Area/Memory:
Stack area is to keep all methods and local variables and reference variables. Stack memory is always referenced in Last in First out order. Local variables are created in stack.

Code Area:
Code area is to store the actual code that will be executed as part of the program. It contains the bytecode.

Static:
This section contains the static data,method and blocks.

The problem arises when we talk about the variables. It is bit complicated to understand where they live.
Let me explain the variable part to make it easy…
Variables can be divided into two broad categories..

  • Instance variables
  • Local variables
Local variables mostly declared in methods, even the method parameters are local to method. As all the methods stay in stack, hence local variables stay in stack in the frame corresponding to the method. That is the reason Local variables are called stack variables.
 
again for instance variable they are mostly declared inside a class. They become active when the object is created. So instance variable stay in heap area,where the objects are stored.The memory allocation depends on the size of the type. say a int will take 32 bits, a long will take 64 bits. JVM will not consider the value of it, It will allocate 32 / 64 bit memory.
 
Methods:
For methods as I said earlier, JVM pushes themselves to stack.
So if your program have 3 methods
main()
dosometask1() // having 1 local variable
dosometask2()//having 2 local variables
JVM reads it and pushes the methods to the stack one by one.
so the stack will be ..
  • dosometask2()
  • dosometask1()
  • main()
with Local variables.
One more area we need to think, if the variable holds reference of an object. like..
public int calc()
{
Tree t=new Tree();

}

For this case, the method calc() will go to stack, with the variable ‘t’. But since we have created a new object here with new keyword, the object will go to heap and the reference will be created with this variable.

So if only give

public int calc()
{
Tree t;

}

so public method calc will be pushed to stack with a reference variable t but no object will be crated in the heap.again when JVM gets a command called new Tree(); only then the object in the heap will be created.

In the first look we might think that object creation is calling a method..Method name is Tree()

public int calc()
{
Tree t=new Tree();

}

Not exactly, It is calling the Tree constructor.(Constructor is nothing but a set of codes which runs when we instantiate an object)

 

 

 

Post navigation

Previous post
Next post

Umer Umer

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Simplified Concept on What Is Interface In Java: Detailed Facts In 5 Minutes
  • What is Integration Testing?
  • What is GEB-Groover Browser Functional Testing?
  • What is GEB- Detailed Facts in Just 5 Minutes
  • What is Extreme Programming(XP) Practice?

Categories

  • Adventure
  • Agile Methodology
  • All world Tour
  • Android Development
  • Ansible
  • App
  • App Development
  • Artificial Intelligence
  • AWS
  • Bitcoin
  • BlockChain
  • Blogging
  • Box Packing
  • Business
  • business
  • Business Communication
  • buying guide
  • C
  • car
  • Care
  • Certification
  • Certification
  • Chatbot
  • cinema
  • Cloud Tech
  • Content Marketing
  • Corporate
  • covid-19
  • crucible
  • Cryptocurrency
  • Database
  • DevOps
  • DevOps
  • Digital Marketing
  • Drawing
  • Dress
  • Educational
  • Employment
  • Entertainment
  • Exceptions In Java
  • Fashion
  • Festivals
  • Finance
  • Fitness
  • Food
  • Game
  • Gift ideas
  • Groovy
  • Health
  • Health
  • home
  • home improvement
  • HR
  • IllegalThreadStateException in Java
  • India Tour
  • Instagram
  • Ios
  • ISO
  • Java
  • Javascript
  • job
  • Kids Section
  • Kitchen
  • Law
  • Life Style
  • Lifestyle
  • Lifestyle Home Improvement
  • Main Stories
  • Management
  • Mattress
  • Motorable
  • Movie
  • nursing
  • Pendrive
  • Perspective
  • Pet
  • Popular
  • Programming
  • Python
  • Relationships
  • Selenium
  • SEO
  • Skin
  • SOAPUI
  • Social Media
  • Social Media Marketing
  • Software
  • software development
  • Software Development Model
  • Software Engineering
  • Software Testing
  • Sports
  • Startup
  • T Robot
  • TDD
  • Tech
  • Tech News
  • Tech News
  • Technology
  • Tool
  • Tour Guide
  • Tour Guide
  • Travel
  • UFT
  • Uncategorized
  • USB Cable
  • VPN
  • Web Development
  • Wordpress
  • Yoga
  • Youtube

Tags

Instagram Instagram Marketing Marketing SEO Thoptv App

Categories

  • Tech
  • Travel
  • Visas
  • Digital Nomad
  • Tech
  • Travel
  • Visas
  • Digital Nomad

Publishing

If you would like to publish on our site, please email admin@techtravelhub.com

Quick Links

  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact
  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact

Contact

Tech Travel Hub LLC

Address:
8 The Green STE A
Dover
Delaware, 19901

Phone: +1 302 956 9948