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

Wednesday, 12 February 2014

Example of Garbage collection .

This post is dedicated to explain how Garbage collection works and what can cause memory leaks.
Here we will manually run a small program and will check how memory is allocated to objects and how they it
is collected back when they needed no more.

Lets create a Valueobject which we will access from a class .
below is a Employee Vo which contains name and id of a employees

Lets create a Test class to accesses this Vo .

The above class contains a method m1 and a main method.

When JVM starts it creates to portions to store data
1) heap to keep objects 
2) stack to keep primitive data types and references to heap.Each method will have there own heap

So when we will run the above Test class the memory snap at line number 4 will be

Heap
===================
Empty


Stack
=============
main method
---------------
args:null


so initially heap will be empty and stack will contain the args passed to main method.
Every method has its own stack.
At line number 5 it will store the value of val in stack . it will be in stack because val is of primitive data type.
now memory snap will.

Heap
===================



Stack
=============
main method
---------------
args:null
val:10


On next line i.e. line number 6 it will create a object of Employee and 
will store in heap and will maintain its reference in stack
So it will look like 

Heap
===================
1000X:EmployeeObj name=2000X , id=2
2000X:StringObj "Ravi"


Stack
=============
main method
---------------
args : null
val : 10
e : 1000X

Here 1000X and 2000X are memory addresses and since "Ravi"
is a object of String class it will be stored at different address


On next line number 7 it is calling a method m1 so a new stack will be created in a
stack and will hold the passed parameter i.e. object e

Heap
===================
1000X:EmployeeObj name=2000X , id=2
2000X:StringObj "Ravi"


Stack
=============

main method
---------------
args : null
val : 10
e : 1000X

m1 method
--------------------
e : 1000X----passed as reference 


Now in next line 12 it has changed the value of id from 2 to 5 
so it will be like

Heap
===================
1000X:EmployeeObj name=2000X , id=5
2000X:StringObj "Ravi"


Stack
=============
main method
---------------
args : null
val : 10
e : 1000X

m1 method
--------------------
e : 1000X


The reference of e will be the same 
At next line number 13 its creating a new object.
Remember When we use New key word always a new object is created
So the heap stack position will be like.


Heap
===================
1000X:EmployeeObj name=2000X , id=5
2000X:StringObj "Ravi"
3000X:EmployeeObj name=4000X , id=7
4000X:StringObj "Gaurav"


Stack
=============

main method
---------------
args : null
val : 10
e : 1000X

m1 method
--------------------
e : 3000X


Now at line number 14 its id value will be changed to 9
so memory snap will

Heap
===================
1000X:EmployeeObj name=2000X , id=5
2000X:StringObj "Ravi"
3000X:EmployeeObj name=4000X , id=9
4000X:StringObj "Gaurav"


Stack
=============

main method
---------------
args : null
val : 10
e : 1000X

m1 method
--------------------
e : 3000X


At line 15 method will be finished and its stack will be removed

Heap
===================
1000X:EmployeeObj name=2000X , id=5
2000X:StringObj "Ravi"
3000X:EmployeeObj name=4000X , id=9
4000X:StringObj "Gaurav"


Stack
=============

main method
---------------
args : null
val : 10
e : 1000X

So finaly the value of the id at line number (8) will 5
Heap
===================
1000X:EmployeeObj name=2000X , id=5
2000X:StringObj "Ravi"
3000X:EmployeeObj name=4000X , id=9
4000X:StringObj "Gaurav"

Stack
=============
main method
---------------
args : null
val : 10
e : 1000X


Now let at this point garbage collector runs
So first it will mark all the live object which can be referred from stack.
and will remove all other objects from memory

Heap
===================
1000X:EmployeeObj name=2000X , id=5
2000X:StringObj "Ravi"
3000X:EmployeeObj name=4000X , id=9
4000X:StringObj "Gaurav"

Stack
=============
main method
---------------
args : null
val : 10
e : 1000X


After running GC

Heap
===================
1000X:EmployeeObj name=2000X , id=5
2000X:StringObj "Ravi"
4000X:StringObj "Gaurav" not be collected 

Stack
=============
main method
---------------
args : null
val : 10
e : 1000X


String objects will not be removed since they are sorted in separate memory called String pool.


#hemant #kurmi #hemant #kurmi #hemant #kurmi
#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi

What is the maximum ram for 32 bits and 64 bits computer?


Some Amazing fact about memory (petabyte,exabyte,zettabyte) And How much data exists on the Web?

Monday, 10 February 2014

Using JConsole in java And creating memory dump by it.

Starting jconsole


The jconsole executable is in JDK_HOME/bin, where JDK_HOME is the directory where the JDK is installed.  If this directory is on your system path, you can start the tool by simply typing jconsole in a command (shell) prompt. Otherwise, you have to type the full path to the executable file


Now run the jconsole exe from cmd.




you will get a new window and it will ask to select a java exe to examine.


After selecting the java process you will get a window with the overview of the process





In upper tabs you can find the more options such as thread here you can find the number of theards and its memory.



On memory tab you will get more option to to view different memories.





You can also see the overview of the whole process



Now to create heap dump we have to enter the path at first parameter and true in second. Once we get a heap dump we can analyse it with different tools . In my next post i will explain hoe to analyse it with  Memory Analyzer (MAT) - Eclipse

You may also like:-

How to Reduce Garbage-Collection Pause Time?

4 Mains causes of Long Garbage Collection Pause?

How Application Performance get impacted by Garbage Collection?

Java Performance Tuning options Java heap

Why Memory leak in Java ?

What is a Memory Leak in java?

What is Garbage collector in java and how it works?

What is Garbage Collector Compaction in Java?

What are Java heap Young, Old and Permanent Generations?

What is difference between Web Server and Application Server ?

What is the maximum ram for 32 bits and 64 bits computer?


#hemant #kurmi #hemant #kurmi #hemant #kurmi
#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi

Heap analysis by Memory Analyzer (MAT) - Eclipse java


To install Memory Analyzer (MAT) in Eclipse just type the below url

Update Sitehttp://download.eclipse.org/mat/1.3.1/update-site/

In software install window of eclipse. And

then click install. After installation you will 

need to open heap dump created by jconsole.

for this just click open and select heap dump 

(Created by jconsole)and browse to the file

and open and you will 

get the below screen



now here you can get the suspect memory leak.


you can also see its details for more info.


Now there is a option of histogram to get more detail browsing of heap



There is also option of fetching data from OQL 








You may also like:-



How to Reduce Garbage-Collection Pause Time?


4 Mains causes of Long Garbage Collection Pause?


How Application Performance get impacted by Garbage Collection?


Java Performance Tuning options Java heap


Why Memory leak in Java ?


What is a Memory Leak in java?


What is Garbage collector in java and how it works?


What is Garbage Collector Compaction in Java?


What are Java heap Young, Old and Permanent Generations?


What is difference between Web Server and Application Server ?


What is the maximum ram for 32 bits and 64 bits computer?



Wednesday, 5 February 2014

Main causes of Out-Of-Memory Errors in java

Main causes of Out-Of-Memory Errors

When there is no enough space to create a new object in heap JVM through  a Out-of-memory errors
before giving  this error JVM surly runs garbage collector once to check for available memory which can be freed .The main causes for this error can be.




 It is possible that you have estimate less memory for your application for example your application need 2 Gb of memory but you have configured only 512 Mb so here you will get an OOME(Out-of-memory errors )

Due to Memoryleak :-

 Memory leak is responsible for decreasing the available memory for heap and can lead to out of memory error for more read What is a Memory Leak in java?

Memory fragmentation :-

It is possible that there may be space in heap but it may be not contiguous . And heap  needs  compaction . Rearrange its memory.

Excess GC overhead :-

Some JVM implementations, such as the Oracle HotSpot, will throw an out-of-memory error when GC overhead becomes too great. This feature is designed to prevent near-constant garbage collection—for example, spending more than 90% of execution time on garbage collection while freeing less than 2% of memory. Configuring a larger heap is most likely to fix this issue, but if not you’ll need to analyze memory usage using a heap dump

Allocating over-sized temporary objects:-


Program logic that attempts to allocate overly large temporary objects. Since the JVM can’t satisfy the request, an out-of-memory error is triggered and the transaction will be aborted. This can be difficult to diagnose, as no heap dump or allocation-analysis tool will highlight the problem. You can only identify the area of code triggering the error, and once discovered, fix or remove the cause of the problem.


Hemant kurmi 

You may also like:-

How to Reduce Garbage-Collection Pause Time?

4 Mains causes of Long Garbage Collection Pause?

How Application Performance get impacted by Garbage Collection?

Java Performance Tuning options Java heap

Why Memory leak in Java ?

What is a Memory Leak in java?

What is Garbage collector in java and how it works?

What is Garbage Collector Compaction in Java?

What are Java heap Young, Old and Permanent Generations?

What is difference between Web Server and Application Server ?

What is the maximum ram for 32 bits and 64 bits computer?

Monday, 3 February 2014

Why Memory leak in Java ?




Memory leaks is a common error in programming, especially when using languages that have no built-in automatic garbage collection, such as C and C++.  In these type of programming languages when you need memory then you have to allocate the memory from OS and when your task completed then you have to release the memory manually. Means developer is only responsible for memory management and it depends on once programming .
But languages like java  has there own automatic memory management. Java Virtual Machine periodically checks which objects in memory are still being used and which are not. Unused objects are removed from the memory  and the memory  is  reused again. This process is called garbage collection and the corresponding piece of JVM is called Garbage Collector, or GC.

So memory management in java is fully depended on garbage collector. But Garbage Collector will run according to it s algorithms used and developer can only request Garbage Collector . But is not necessary that it will run and will claim the memory.

Memory leaking often happens when writing small programs which use file IO. You open the file, write your data, but don't close it once you're done.



What is a Memory Leak in java?



Definition 1:-Unable  to release unreachable memory, which can no longer be allocated to new  process during execution of allocating process. It can mostly be cured by using GC techniques or detected by automated tools.

Definition 2:-Unable to release reachable memory, which is no longer needed for your program to function correctly. It is nearly impossible to be detected by automated tools or programmers who is not too familiar with the code. While technically it is not a leak, because according to GC it is useful to you and will not clear it.

Definition 3:-A memory leak is the gradual loss of available computer memory when a program (an application or part of the operating system) repeatedly fails to return memory that it has obtained for temporary use

So memory leak is like leakage in water tank the quantity of water will decreased day by day. And you will suffer with lack of quantity.


Now talking about on basic what is a leak let you have 20 water glass in you house and when you need you use them and when servant comes he will clean all the used water glasses and will keep in stand for future use. Now if you drink water and keep the used glass in other rooms where servant is not permitted to enter then they will be left uncleaned or you kept the water glass on table with some water then servant will think that you are using it and will not take that glass from your table. 

So the quantity will decrease from 20 to 18 . and if this continues then a day will come when you will search all the water glasses and will ask servant to clean. In java this servant is know as GARBAGE COLLECTOR


You may also like:-

How to Reduce Garbage-Collection Pause Time?

4 Mains causes of Long Garbage Collection Pause?

How Application Performance get impacted by Garbage Collection?

Java Performance Tuning options Java heap

Why Memory leak in Java ?

What is a Memory Leak in java?

What is Garbage collector in java and how it works?

What is Garbage Collector Compaction in Java?

What are Java heap Young, Old and Permanent Generations?

What is difference between Web Server and Application Server ?

What is the maximum ram for 32 bits and 64 bits computer?


#hemant #kurmi #hemant #kurmi #hemant #kurmi
#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi

Some Amazing fact about memory (petabyte,exabyte,zettabyte) And How much data exists on the Web?

Hemant Kurmi - 2021 - Some Amazing Facts About Memory: Petabyte, Exabyte, and Zettabyte

Some Amazing Facts About Memory: Petabyte, Exabyte, and Zettabyte As technology advances, our ability to store and process data has grown ...