0% found this document useful (0 votes)
26 views4 pages

Memory

Uploaded by

dev.jai.ak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views4 pages

Memory

Uploaded by

dev.jai.ak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

JVM Architecture

1. Java file is compiled to class file and when class file is executed it creates an instance of JVM.
2. Class Loader - loads application’s class files. It also loads built in classes of java like string,
collection etc
a. Load – App class loader, extension class loader (additional dependencies in class
path), bootstrap class loader (Java files)
b. Link – Verifies byte code compatibility to JVM, prepares memory (memory is
allocated to static variable and not instance variables), resolve
c. Initialize – Class variables are initialized
3. Byte code is fed to execution engine to execute the program

Runtime data areas (Memory of JVM)

1. Perm Gen – Contains class related data. By default 64 MB is allocated. From java 8 its called
Metaspace and its moved to separate memory in native OS. So by default there is no limit to
metaspace. It can even occupy full memory if needed. But it can be limited with some
parameters
2. Heap – Object storage. -Xms for min and -Xmx for max heap configuration
3. Stack – Stack Frame corresponding to method execution. Local variables reside here
4. Program Counter Register – Has thread state
Once data area is loaded and current instruction to be executed is ready, then java interpreter
interprets the current instruction in byte code and executes it.

Garbage collection
An object is eligible for garbage collection when it can no longer be accessed. This can happen when
the object goes out of scope or when the object's reference variable is assigned a null value

https://www.freecodecamp.org/news/garbage-collection-in-java-what-is-gc-and-how-it-works-in-
the-jvm/

https://www.digitalocean.com/community/tutorials/garbage-collection-in-java

https://www.geeksforgeeks.org/garbage-collection-java/
Memory leak cause and how to handle it
https://www.baeldung.com/java-memory-leaks

https://dzone.com/articles/how-to-find-and-fix-memory-leaks-in-your-java-appl

https://docs.oracle.com/en/java/javase/21/troubleshoot/troubleshooting-memory-
leaks.html#GUID-CCC45C28-F523-4C17-9A22-91CEC80F7A26

https://medium.com/@abhishekranjandev/becoming-a-memory-leak-detective-unraveling-and-
fixing-memory-leaks-in-your-spring-boot-859af9295654

Connection leak
A connection leak is a type of bug that occurs when a program fails to close a database connection
after it is finished using it. This can lead to the database running out of connections, which can
prevent other programs from connecting to it. You will reach maximum open connection limit before
you hit memory limits usually.

Create a memory leak


https://www.baeldung.com/java-create-detect-memory-leaks

String memory management – pool , heap with intern


Is string pool part of heap?
Does new string create in spring pool and heap both?
How to increase perm gen and heap size?
JVM memory management
How to resolve out of memory for heap and perm gen?
How class loader loads?
Where are static methods and variables stored in jvm?
Handle memory leakage? How to avoid
Identify memory leak?
Connection leak vs stream leak vs memory leak vs file leak?
Is string pool garbage collected?
Will memory leak happen when input stream is returned to ui for rendering if its not
close?

Static fields
Static fields are allocated memory in the specific part of the Metaspace (before Java 8, it was a heap
region called Permanent Generation, now, the metaspace is located outside of the heap, in native
memory, provided by the OS to a particular Application for its own usage) with name Class Area,
when the class is loading by the application class loader, during phases prepare and loading.
If the static field is primitive, the value is stored in the class area, and if it is non-primitive (Object)
type, it is stored in heap space, but the reference on this allocated object is stored in the class area.
After the unloading of the class, the free memory for that static field is also available for collection
by GC. If the field is final as well, that is, static final, it is kept in the constant pool under the class
area.

Class loader hierarchy


The Java class loader hierarchy is a hierarchical arrangement of class loaders that are responsible for
loading classes into the Java Virtual Machine (JVM). The hierarchy consists of three types of class
loaders in following order:

1. System class loader: The system class loader is a child of the extension class loader. It is
responsible for loading classes from the application's classpath. The classpath is a list of
directories and JAR files that the JVM searches for classes when it is loading a class.
2. Extension class loader: The extension class loader is a child of the bootstrap class loader. It is
responsible for loading classes from the JRE's lib/ext directory. This directory typically
contains extensions to the core Java classes, such as the JDBC driver classes.
3. Bootstrap class loader: The bootstrap class loader is the parent of all other class loaders. It is
responsible for loading the core Java classes, such as the java.lang package. These classes
are typically stored in the JRE's lib/rt.jar file.

The class loader hierarchy is important because it ensures that classes are loaded from the correct
location. For example, if a class is loaded by the bootstrap class loader, it cannot be overridden by a
class with the same name that is loaded by the system class loader. This prevents conflicts between
different versions of the same class.

The class loader hierarchy is a powerful tool that can be used to manage the loading of classes in a
Java application. By understanding how the hierarchy works, you can avoid conflicts between
different versions of the same class and ensure that your application is using the correct classes.

https://medium.com/@fullstacktips/what-are-class-loaders-and-different-types-of-class-loaders-in-
java-e12f05821bc2#:~:text=The%20class%20loader%20hierarchy%20is,Java%20Virtual%20Machine
%20(JVM).&text=At%20the%20top%20of%20the,in%20the%20JRE's%20lib%2Frt.

https://www.baeldung.com/java-classloaders

You might also like