Showing posts with label heap. Show all posts
Showing posts with label heap. Show all posts

Thursday, 23 May 2019

Is Java primitive data types stored in stack or heap?


Stack is a special region in computer memory, that stores temporary variables like local variables, parameters, function calls etc.,

Heap is a portion of memory where dynamic allocation and de allocations happen. All the Java objects stored in Heap.

In case of primitive data types, primitive variables defined locally (within a function, block) are stored on the stack, where as if a primitive variable defined as part of an object is stored in Heap.

public class Arithmetic {
 private int x; // Stored in Head
 
 public Arithmetic(int x) {
  this.x = x;
 }
 
 public int square() {
  int y = x * x; //y is stored in stack
  return y;
 }
}

In the above example, x is stored in heap, whereas y is stored ins tack


You may like

Saturday, 15 February 2014

Garbage Collection

JVM heap stores the objects created by an Application. In languages like “C”, You must explicitly free the memory by using free(), but in Java JVM takes care these tasks. No way you can explicitly free the memory in Java. Everything is done by JVM.

Garbage collection is a process of freeing memory used by un referenced objects.

A disadvantage of garbage-collected heap is that it adds an overhead that can affect program performance. Since in background, the Java Garbage collector thread keeps track of the objects like which are in use and which are not reference by any other etc.,

JVM perform the Garbage collection periodically, but you can suggest the JVM, to perform Garbage collection immediately by using “System.gc()”. But it is not guaranteed to run Garbage collection immediately, It is just a suggestion went to run Garbage Collector. It is the JVM, to decide how to respond for the request.

System.gc
Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.

Implementation of gc in System class
public static void gc() {
    Runtime.getRuntime().gc();
}

Simply it is calling the gc method of Runtime class. So the call System.gc() is equivalent to the call:
Runtime.getRuntime().gc()

Some Points to Remember
1. is calling to System.gc() runs the garbage collector immediately ?
No, Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse.

2. What is the difference between gc() in System class and Runtime class?
No difference. System.gc() calls the gc() of Runtime class internally.

3. Is garbage collector a dameon thread?
Yes, Garbage collector runs as a background thread

4. When does an object become eligible for garbage collection?
An object becomes eligible for Garbage Collection when no references can access it, I.e, reference count for the object is zero.


You may like
Object creation and destruction                                                 main method                                                 Home

Object Creation and Destruction

In java Objects are stored in heap.

How to create an object
   Syntax
      new ClassName(parameters);
'new' keyword is used to create an object in Java. parameters are optional.

   Example
      new Person()

Creationof object is not sufficient to access the object methods and instance properties. There must be a reference to the object, to access the instance methods and properties of an object.

How to assign an object to a reference
   Syntax
      ClassName reference = new ClassName(parameters);

   Example
      Person p1 = new Person()

By using the reference p1, we can access the instance behaviors and properties of an object.

One object can be referred by more than one reference like below
Person p2 = p1;

Now p1 and p2 point to the same object. Here the reference count for the object is 2, since p1 and p2 are referring to the same object.

If reference count for an object is zero, then the object is eligible for garbage collection. I.e, the memory used by the object is freed and used for other purposes.

Remember, even if the objects is created inside a method, or block also, it always stored in the heap, only references stored on stack.

Detailed view of Object creation and Destruction
1. Lets say there is a class called Person, and an object created like below
      Person p1 = new Person()

object of the class Person is created in the heap and reference p1 refers to the newly created object


2. Assigning one more reference to the previously created object.
       Person p2 = p1;
As per the above statement references p1 and p2 refers to the same object.

Reference count is 2


3. Create one more object
     Person p3 = new Person()


4. To remove reference for an object, there are two ways. One way set null to the reference, or make the reference to point to other object.
     p2= null;

Above statement makes the reference variable p2 to set to null.

5. p2 = p3;
Now p2 refers to the object pointed by the reference p3




6. p2 = null;
p3 = null;

Now the object which is previously pointed by reference variables p2 and p3 has reference count zero. So it is eligible for garbage Collection.


You may like


Increase heap size                                                 Garbage Collection                                                 Home