Hackernoon logoSteak Inside || Java Inside by@sakibsami

Steak Inside || Java Inside

Author profile picture

@sakibsamiSakib Sami

Steak Inside is a burger item made with a large piece of chicken, cheese and other things. I am a big fan of it.

Ah, this is not our today’s topic. Today we will discuss about what is inside Java ? How everything happens inside Java ?

If you are familiar with Java then you have already know how Java application starts. From main method,

public static void main(String args[]){
// Rest of instructions here

And in memory it becomes,

** Red mark space is reserved for main() method.

Application starts running within a thread by default. Java uses stack memory ( **green marked** ) to handle this.
Lets declare a variable of primitive type,

int x;
int a;

It will look like,

Means x and a will take place in main() method’s memory. That’s why you can’t access local variables of a method from another method.

Now if we call another method from main method such as,

public static void main(String args[]){
public static void check(){


Now what will happen ? Any idea ? Ok. This is the inside picture.

Different space will be allocated for check() method.

Assume we have a class named Book, lets have an instance of it in check() method.

public static void main(String args[]){
public static void check(){
Book b = new Book();

So it will create an instance in heap memory and keep a reference in check() method’s memory.

Java puts primitive types directly in local memory but objects in heap and keep a reference to it in method memory.

If we declare Book c and assign b to c `c = b;` it will create a reference to `b`’s object. So if you change anything of `c`, `b` value will be updated too. Because everything you pass in Java other than method parameters are pass by reference. Method parameters are pass by value.

After completing execution of the method `check()` automatically it will be removed from stack as well as all references.

Well, one thread is not enough for us, we need another thread.

public static void check(){
Thread t = new Thread(){
public void run(){
public static void background(){

And this will happen at memory level.

As we are running a different thread so it will create a different stack to handle everything of it such as `background()` method in thread 2 stack. As it has been called from second thread.

If you are a Java fan then you may know that `String` is immutable in Java. Immutable means you can’t modify it after creation. One of the reason of it is to save memory space. How ?

We have a `String` variable `p1` in `check()` method having value `abcd` and creates a memory space in String Pool, then we have declared `p2` having same value `abcd`. Now it won’t create another `abcd` in String Pool rather get a reference to it. If you concat another value lets say efg it will create a new instance and take memory in String Pool if it’s not exists. When method execution complete only references will be removed not the object.

Do you know how `==` operator and `equals()` differs in Java ? `==` checks for references but `equals` method checks value.

Now you may want to know how Garbage Collector ( GC ) cleans memory. Ok, it looks for unreferenced objects in heap and if found any GC removes it from memory. Within this lookup JVM pauses the application from execution (Pause The World). As this cleanup takes very minimal time we don’t realise it. But if the memory allocation is huge then this pause may affect your application and application may run slowly. And it’s the reason people developed ScyllaDB instead of using Cassandra. Note that ScyllaDB is written in C++.

That’s all for today, See you again.


The Noonification banner

Subscribe to get your daily round-up of top tech stories!