Java Heap Memory¶
Introduction¶
Heap memory is used to store Java objects and arrays.
Objects created using the new keyword are normally stored in heap memory.
Example:
The Patient object is stored in heap memory.
The local reference variable patient is stored in the current method's stack frame.
What Is Stored in Heap Memory?¶
Heap memory stores:
- Objects
- Object attributes
- Arrays
- Collections
- String objects
- Non-static attributes
- Shared objects referenced by different methods or threads
Example:
Patient patient = new Patient();
Department department = new Department();
List<Bill> bills = new ArrayList<>();
The following objects are stored in heap memory:
Objects in Heap Memory¶
Example:
Memory idea:
The reference variable points to the object.
Non-Static Attributes in Heap Memory¶
Non-static attributes belong to an object.
Example:
public class Patient {
private String patientCode;
private String patientName;
private boolean retired;
}
When a Patient object is created:
The object and its non-static attributes are stored in heap memory.
Memory idea:
Each object has its own values.
Memory idea:
The two objects can store different patient details.
Arrays in Heap Memory¶
Arrays are objects and are stored in heap memory.
Example:
Memory idea:
Object array:
The array object is stored in heap memory.
The array initially contains three null references.
Collections in Heap Memory¶
Collection objects are stored in heap memory.
Example:
Memory idea:
When patient objects are added:
Both the ArrayList and the Patient object are in heap memory.
Strings in Heap Memory¶
Strings are objects.
Example:
The reference variable may be local to a method and stored in stack memory.
The string object is stored in heap memory.
String literals may be stored in the Java String Pool, which is part of heap memory.
Garbage Collection¶
Java automatically manages heap memory using garbage collection.
An object becomes eligible for garbage collection when no reachable reference points to it.
Example:
After patient becomes null, the object may become eligible for garbage collection if no other reference points to it.
Garbage collection:
- Finds unreachable objects
- Releases heap memory
- Runs automatically
- Does not run at an exact time chosen by the programmer
Heap Memory Error¶
Heap memory is limited.
If the application creates too many objects and memory cannot be released, Java may throw:
Example problem:
The list keeps references to every object.
Garbage collection cannot remove them because they are still reachable.
Memory Leak¶
A memory leak happens when objects are no longer needed but are still referenced.
Example:
If the application continuously adds patients and never removes them:
the list can grow for the entire application lifetime.
This is especially dangerous because it is static and shared.
HMIS applications should not keep patient records in unnecessary long-lived static collections.
Static and Non-Static Difference¶
Static Attributes¶
A static attribute belongs to the class.
Example:
There is only one shared value for the class.
All objects use the same static value.
Both objects share:
A static reference can point to an object in heap memory.
Example:
The ArrayList object is in heap memory.
The static field keeps a shared reference to that object.
Because the static field may live as long as the class remains loaded, the object may also remain reachable for a long time.
Non-Static Attributes¶
A non-static attribute belongs to one object.
Example:
Every object has its own value.
Memory idea:
Non-static attributes are stored as part of each object in heap memory.
Static vs Non-Static Summary¶
| Feature | Static | Non-static |
|---|---|---|
| Belongs to | Class | Object |
| Number of copies | One shared copy | One copy per object |
| Object required | No | Yes |
| Shared between objects | Yes | No |
| Lifetime | Usually while class is loaded | While object is reachable |
| Can cause long-lived references | Yes | Less likely unless object is retained |
| Attribute data | Shared class-associated value | Stored inside each object |
Static Method and Heap Memory¶
A static method does not belong to an object.
Example:
The method itself does not create a permanent stack or heap area.
When the method runs:
- A stack frame is created.
new Patient()creates aPatientobject in heap memory.- The reference is returned.
Example:
Memory idea:
Non-Static Method and Heap Memory¶
A non-static method belongs to an object.
Example:
The object and its attribute patientName are in heap memory.
When the method runs:
- A stack frame is created.
- The current object reference
thisis available. - The method reads the object's heap data.
HMIS-Style Example¶
public class InvestigationReport {
private static int totalReportsGenerated;
private Department department;
private Investigation investigation;
private List<PatientInvestigation> results =
new ArrayList<>();
public void generateReport() {
totalReportsGenerated++;
results.add(new PatientInvestigation());
}
public static int getTotalReportsGenerated() {
return totalReportsGenerated;
}
}
Memory idea:
Heap:
InvestigationReport object
department
investigation
results reference
ArrayList object
PatientInvestigation objects
Shared static data:
totalReportsGenerated
Heap and Stack Working Together¶
Example:
public void processPatient() {
int count = 1;
Patient patient = new Patient();
patient.setPatientCode("P001");
}
Memory idea:
The stack stores temporary method data.
The heap stores the object and its non-static attributes.
Sensitive Data in Heap Memory¶
HMIS objects may contain sensitive data.
Examples:
Sensitive objects may remain in heap memory while they are reachable.
Avoid:
- Unnecessary static patient collections
- Long-lived references to patient data
- Caching sensitive values without a clear need
- Storing passwords as plain text
- Keeping large report results longer than required
Example of a risky static collection:
This collection may remain for the full application lifetime and prevent patient objects from being garbage collected.
Best Practices¶
- Remove references to objects that are no longer needed.
- Avoid unnecessary static collections.
- Clear large result lists when they are no longer required.
- Do not create objects repeatedly inside unnecessary loops.
- Use pagination for large reports.
- Avoid loading full entity graphs when a DTO is enough.
- Do not store sensitive patient data longer than necessary.
- Use appropriate bean scopes.
- Avoid memory leaks caused by long-lived references.
- Monitor heap usage in large HMIS reports.
Important Points¶
- Objects are stored in heap memory.
- Arrays are stored in heap memory.
- Collections are stored in heap memory.
- Non-static attributes are stored inside objects.
- Static fields are shared by the class.
- A static reference can keep a heap object alive.
- Garbage collection removes unreachable objects.
- Too many reachable objects may cause
OutOfMemoryError. - Stack references can point to heap objects.
- Heap memory is shared between threads.
Summary¶
Heap memory stores Java objects and their non-static attributes.
Example:
Memory idea:
Static and non-static difference:
Static:
One shared value belonging to the class.
Non-static:
One value per object stored as part of the object.
Main heap contents: