Java Memory Management

In this article we learn the java memory management and what is the difference between JAR and WAR files in Java.

Java Memory Management | Memory Management in Java

Java memory management or memory management in java is dynamic and automatic. When we create a new object in Java, Java automatically allocates the right amount of memory for that object in the heap. We don’t have to allocate any memory for any objects explicitly.

What happens when we are finished with that object and How do we de- allocate the memory that object uses?

Java memory management is automatic. Once we finish with an object, that object no longer has any live references to it (it won’t be assigned to any variables you’re still using or stored in any arrays). Java has a garbage collector that looks for unused objects and reclaims the memory that those objects are using. We don’t have to do any explicit freeing of memory we just have to make sure we are not still holding onto an object we want to get rid of.

Which are the different segments of memory?

1. Stack Segment: The stack segment contains the local variables and reference variables. Reference variables hold the address of an object in the heap segment.

2. Heap Segment: The heap segment contains all the objects that are created during runtime. It stores objects and their attributes (instance variables).

3. Code Segment: The code segment stores the actual compiled Java byte codes when loaded.

State the difference between JAR and WAR files in Java?

The difference between the JAR file and the WAR file are the following:

1. JAR file stands Java Archive file that allows us to combine many files into a single file. Whereas, WAR files stand for Web Application Archive files that store XML, java classes, and Java Server pages, etc., for Web Application purposes.

2. JAR files hold Java classes in a library. Whereas WAR files store the files in the ‘lib’ directory of the web application.

3. All the enterprise Java Bean classes and EJB deployment descriptors present in the EJB module are packed and stored in a JAR file with .jar extension.

4. Whereas the WAR file contains the web modules such as Servlet classes, GIFs, HTML files, JSP files, etc., with .war extension.

Other Popular Articles

What is Access Modifiers in Java?

Leave a Comment