-
Notifications
You must be signed in to change notification settings - Fork 137
heap memory
Heap memory is used to store Java objects and arrays.
Objects created using the new keyword are normally stored in heap memory.
Example:
Patient patient = new Patient();The Patient object is stored in heap memory.
The local reference variable patient is stored in the current method's stack frame.
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:
Patient object
Department object
ArrayList object
Bill objects added to the list
Example:
Patient patient = new Patient();Memory idea:
Stack:
patient reference
|
v
Heap:
Patient object
The reference variable points to the object.
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:
Patient patient = new Patient();The object and its non-static attributes are stored in heap memory.
Memory idea:
Heap:
Patient object
patientCode
patientName
retired
Each object has its own values.
Patient firstPatient = new Patient();
Patient secondPatient = new Patient();Memory idea:
Heap:
Patient object 1
Patient object 2
The two objects can store different patient details.
Arrays are objects and are stored in heap memory.
Example:
int[] patientCounts = new int[5];Memory idea:
Stack:
patientCounts reference
|
v
Heap:
int array with 5 elements
Object array:
Patient[] patients = new Patient[3];The array object is stored in heap memory.
The array initially contains three null references.
Collection objects are stored in heap memory.
Example:
List<Patient> patients = new ArrayList<>();Memory idea:
Stack:
patients reference
|
v
Heap:
ArrayList object
When patient objects are added:
patients.add(new Patient());Both the ArrayList and the Patient object are in heap memory.
Strings are objects.
Example:
String patientName = "Kamal";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.
Java automatically manages heap memory using garbage collection.
An object becomes eligible for garbage collection when no reachable reference points to it.
Example:
Patient patient = new Patient();
patient = null;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 is limited.
If the application creates too many objects and memory cannot be released, Java may throw:
java.lang.OutOfMemoryError: Java heap space
Example problem:
List<Patient> patients = new ArrayList<>();
while (true) {
patients.add(new Patient());
}The list keeps references to every object.
Garbage collection cannot remove them because they are still reachable.
A memory leak happens when objects are no longer needed but are still referenced.
Example:
private static final List<Patient> patients =
new ArrayList<>();If the application continuously adds patients and never removes them:
patients.add(patient);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.
A static attribute belongs to the class.
Example:
private static int generatedReportCount;There is only one shared value for the class.
All objects use the same static value.
PatientReport firstReport = new PatientReport();
PatientReport secondReport = new PatientReport();Both objects share:
generatedReportCount
A static reference can point to an object in heap memory.
Example:
private static List<String> reportNames =
new ArrayList<>();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.
A non-static attribute belongs to one object.
Example:
private String reportName;Every object has its own value.
PatientReport reportOne = new PatientReport();
PatientReport reportTwo = new PatientReport();Memory idea:
Heap:
PatientReport object 1
reportName
PatientReport object 2
reportName
Non-static attributes are stored as part of each object in heap memory.
| 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 |
A static method does not belong to an object.
Example:
public static Patient createPatient() {
return new Patient();
}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:
Patient patient = PatientFactory.createPatient();Memory idea:
Stack:
patient reference
Heap:
Patient object
A non-static method belongs to an object.
Example:
public String getPatientName() {
return patientName;
}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.
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
Example:
public void processPatient() {
int count = 1;
Patient patient = new Patient();
patient.setPatientCode("P001");
}Memory idea:
Stack:
count = 1
patient reference
Heap:
Patient object
patientCode = "P001"
The stack stores temporary method data.
The heap stores the object and its non-static attributes.
HMIS objects may contain sensitive data.
Examples:
private String patientName;
private String diagnosis;
private String investigationResult;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:
private static final List<Patient> allPatients =
new ArrayList<>();This collection may remain for the full application lifetime and prevent patient objects from being garbage collected.
- 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.
- 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.
Heap memory stores Java objects and their non-static attributes.
Example:
Patient patient = new Patient();Memory idea:
Stack:
patient reference
Heap:
Patient object
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:
Objects
Arrays
Collections
String objects
Non-static attributes
Shared referenced data