Testimonials for dineshonjava.com

"Recently I attended the online training course - Spring 4 MVC - conducted by Mr Dinesh Rajput personally, and I certify that he was able to give hands-on trainin on the basics of developing Spring MVC applications using STS-Gradle-Thymeleaf technologies. Having said that, I highly recommend DOJ to anybody in need of first-hand technology support."

Bhawani Shanker
Austria (European Union)
January, 2015

"Hi Dinesh, I follow your dineshonjava.com which is one of the best online resource to learn Java and related technologies. I really enjoyed going through your articles. These are helped me to learn frameworks like doing and implement them. Thank you so much."

By reader,
Murali k
Senior Software Engineer at UHG (Unified Healthcare Group)

"wow! this is an exhaustive look at Spring Boot’s Actuator on the Dinesh on Java blog."

Josh Long
Spring Community

"Amazing post with amazing blog.. In my learning phase mostly I prefer your blog well explain java concepts"

Yogendra Sharma

"I don’t know if I would call this complete, but it is exhaustive! Well done Dinesh, on this lengthy introduction to Spring Boot "

Josh Long
Spring Community

"Spring for Delphi. Cool! Eren Av?aro?ullar? has a nice post showing how to use dynamic languages (in particular, Groovy) with Spring. Dinesh Rajput has put together a blog introducing some of the features unique to the Spring Batch 2.x generation, which - while Spring Batch 2.x has been out"

Josh Long
Spring Community

"But it's also stuff that's very convenient to reference in a pinch and it's nice to see it all laid out in order with a little bit of narrative. The Dinesh on Java blog has a nice post on how to setup Spring Security. The Lucky Ryan blog has a really à propos post on migrating XML-based Spring MVC configuration"

Josh Long
Spring Community

"The Dinesh on Java blog has a nice post on how to use Spring Data"

Josh Long
Spring Community

"on server-side pagination with ZK (a widget-centric web framework), Spring Data MongoDB and Google Maps. Do I need to say anymore? Check it out! The Dinesh on Java blog has a nice, introductory post on using Spring MVC interceptors. Good stuff!"-

Josh Long
Spring Community

How work Garbage Collector in Java

In this tutorial we will explain how to garbage collector work in JVM. In the first tutorial of garbage collector we have seen what is garbage collection in java and its related some methods. In Java, allocation and de-allocation of memory space for objects are done by the garbage collection process in an automated way by the JVM.

Java Memory Management
Java Memory Management, with its built-in garbage collection, is one of the language’s finest achievements. It allows developers to create new objects without worrying explicitly about memory allocation and deallocation, because the garbage collector automatically reclaims memory for reuse. This enables faster development with less boilerplate code, while eliminating memory leaks and other memory-related problems. At least in theory.
Ironically, Java garbage collection seems to work too well, creating and removing too many objects. Most memory-management issues are solved, but often at the cost of creating serious performance problems. Making garbage collection adaptable to all kinds of situations has led to a complex and hard-to-optimize system. In order to wrap your head around garbage collection, you need first to understand how memory management works in a Java Virtual Machine (JVM).

Java Garbage Collection GC Initiation
Being an automatic process, programmers need not initiate the garbage collection process explicitly in the code. System.gc() and Runtime.gc() are hooks to request the JVM to initiate the garbage collection process.

Though this request mechanism provides an opportunity for the programmer to initiate the process but the onus is on the JVM. It can choose to reject the request and so it is not guaranteed that these calls will do the garbage collection. This decision is taken by the JVM based on the eden space availability in heap memory. The JVM specification leaves this choice to the implementation and so these details are implementation specific.

Undoubtedly we know that the garbage collection process cannot be forced. I just found out a scenario when invoking System.gc() makes sense. Just go through this article to know about this corner case when System.gc() invocation is applicable.

Java Garbage Collection Process
Garbage collection is the process of reclaiming the unused memory space and making it available for the future instances.

How work Garbage Collector in Java

Eden Space: When an instance is created, it is first stored in the eden space in young generation of heap memory area.

Survivor Space (S0 and S1): As part of the minor garbage collection cycle, objects that are live (which is still referenced) are moved to survivor space S0 from eden space. Similarly the garbage collector scans S0 and moves the live instances to S1.

Instances that are not live (dereferenced) are marked for garbage collection. Depending on the garbage collector (there are four types of garbage collectors available and we will see about them in the next tutorial) chosen either the marked instances will be removed from memory on the go or the eviction process will be done in a separate process.

Old Generation: Old or tenured generation is the second logical part of the heap memory. When the garbage collector does the minor GC cycle, instances that are still live in the S1 survivor space will be promoted to the old generation. Objects that are dereferenced in the S1 space is marked for eviction.

Major GC: Old generation is the last phase in the instance life cycle with respect to the Java garbage collection process. Major GC is the garbage collection process that scans the old generation part of the heap memory. If instances are dereferenced, then they are marked for eviction and if not they just continue to stay in the old generation.

Memory Fragmentation: Once the instances are deleted from the heap memory the location becomes empty and becomes available for future allocation of live instances. These empty spaces will be fragmented across the memory area. For quicker allocation of the instance it should be defragmented. Based on the choice of the garbage collector, the reclaimed memory area will either be compacted on the go or will be done in a separate pass of the GC.

What is the garbage collector in Java?

Garbage Collector is part of JRE that makes sure that object that are not referenced will be freed from memory. Garbage collector can be viewed as a reference count manager. if an object is created and its reference is stored in a variable, its reference count is increased by one. during the course of execution if that variable is assigned with NULL. reference count for that object is decremented. so the current reference count for the object is 0. Now when Garbage collector is executed, It checks for the objects with reference count 0. and frees the resources allocated to it.

Advantage of Garbage Collection
  • It makes java memory efficient because garbage collector removes the unreferenced objects from heap memory.
  • It is automatically done by the garbage collector(a part of JVM) so we don't need to make extra efforts.
How can an object be unreferenced?
There are many ways:
  • By nulling the reference
    Student s=new Student();

  • By assigning a reference to another
    Student s1=new Student();
    Student s2=new Student();
    s1=s2;//now the first object referred by s1 is available for garbage collection

  • By annonymous object etc.
    new Student();
finalize() method

The finalize() method is invoked each time before the object is garbage collected. This method can be used to perform cleanup processing. This method is defined in Object class as:
protected void finalize(){}

The Garbage collector of JVM collects only those objects that are created by new keyword. So if you have created any object without new, you can use finalize method to perform cleanup processing (destroying remaining objects).

gc() method

The gc() method is used to invoke the garbage collector to perform cleanup processing. The gc() is found in System and Runtime classes.

public static void gc(){}

Many people think garbage collection collects and discards dead objects.
In reality, Java garbage collection is doing the opposite! Live objects are tracked and everything else designated garbage.

When an object is no longer used, the garbage collector reclaims the underlying memory and reuses it for future object allocation. This means there is no explicit deletion and no memory is given back to the operating system. To determine which objects are no longer in use, the JVM intermittently runs what is very aptly called a mark-and-sweep algorithm.

Adding an existing project to GitHub using the command line

Step for existing project to GitHub using command line, following steps are required to this.

Step 1: Creating an account to the GitHub. Suppose
Username for GitHub: myGitUser
Password for GitHub: myGitPassword


Step 2: Access above url using mention username and password.

Step 3: Create New Repository to your account. To avoid errors, do not initialize the new repository with README. You can add these files after your project has been pushed to GitHub.

Suppose your repository name: myRepository

Step 4: Installing GitBash to you machine. for this link http://git-scm.com/download/win

Step 5: After installing GitBash open the command prompt for Git.


Step 6: In the Command prompt, change the current working directory to your local project.

Step 7: Initialize the local directory as a Git repository. As follows


Step 8: Add the files in your new local repository. This stages them for the first commit.


Step 9: Commit the files that you've staged in your local repository.


Commits the tracked changes and prepares them to be pushed to a remote repository

Step 10: At the top of your GitHub repository's Quick Setup page, click to copy the remote repository URL


Step 11: In the Command prompt, add the URL for the remote repository where your local repository will be pushed


# Sets the new remote

Step 12: Push the changes in your local repository to GitHub.


# Pushes the changes in your local repository up to the remote repository you specified as the origin