The below method has 2 for loops. Firstloop iterates for 36000 times and inner forloop iterates for 24 times, so the total number of records insertion will be 864000. The code execution runs for nearly 3.5 hours and terminates.(Records insertion failed) The Session closure and the transaction commit is made only after the end of outer for loop. It is found that the RAM consumption is nearly 6.9GB during execution
@SuppressWarnings("unchecked")
public void createBRResults() {
List<BusinessRules> businessRuleList = null;
Organization organization = null;
Brresults brresults = null;
Criteria criteria = null;
Criteria newCriteria = null;
String brStatus = "Live";
Date date = new Date();
Session session = sessionFactory.openSession();
Transaction transaction = null;
try {
if (callInfos == null) {
callInfos = getEntities(CallInfo.class);
}
for (CallInfo callInfo : callInfos) {
transaction = session.beginTransaction();
criteria = session.createCriteria(BusinessRules.class);
criteria.createCriteria("businessGoals").add(
Restrictions.eq("category", callInfo.getCategory()));
businessRuleList = criteria.list();
organization = callInfo.getOrganization();
for (BusinessRules businessRule : businessRuleList) {
brresults = new Brresults();
brresults.setBusinessRule(businessRule);
brresults.setCallInfo(callInfo);
brresults.setCreatedDate(date);
brresults.setModifiedDate(date);
brresults.setOrganization(organization);
brresults.setBrrStatus(brStatus);
brresults.setBrrValue(Math.random() < 0.5 ? 0 : 1);
session.save(brresults);
brresults = null;
}
criteria = null;
organization = null;
businessRuleList = null;
session.flush();
session.clear();
transaction.commit();
System.gc();
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
session.close();
}
}
I have attached last few lines of GC log to indicate the memory allocation
17034.595: [Full GC (Ergonomics) [PSYoungGen: 979968K->979964K(1862144K)] [ParOldGen: 5576158K->5576158K(5576192K)] 6556126K->6556123K(7438336K), [Metaspace: 23195K->23195K(1071104K)], 2.1041939 secs] [Times: user=8.61 sys=0.00, real=2.11 secs]
17036.699: [Full GC (Ergonomics) [PSYoungGen: 979968K->979964K(1862144K)] [ParOldGen: 5576158K->5576158K(5576192K)] 6556126K->6556123K(7438336K), [Metaspace: 23195K->23195K(1071104K)], 1.5930338 secs] [Times: user=9.39 sys=0.00, real=1.59 secs] 17038.292: [Full GC (Ergonomics) [PSYoungGen: 979968K->979964K(1862144K)] [ParOldGen: 5576158K->5576158K(5576192K)] 6556126K->6556122K(7438336K), [Metaspace: 23195K->23195K(1071104K)], 1.9376959 secs] [Times: user=8.51 sys=0.00, real=1.94 secs]
17040.230: [Full GC (Ergonomics) [PSYoungGen: 979968K->830418K(1862144K)] [ParOldGen: 5576158K->5576027K(5576192K)] 6556126K->6406445K(7438336K), [Metaspace: 23196K->23196K(1071104K)], 2.9929302 secs] [Times: user=17.97 sys=0.00, real=3.00 secs]
Heap PSYoungGen total 1862144K, used 889367K [0x0000000715d80000, 0x00000007bdc80000, 0x00000007c0000000)
eden space 979968K, 90% used [0x0000000715d80000,0x000000074c205e48,0x0000000751a80000) from space 882176K, 0% used [0x0000000787f00000,0x0000000787f00000,0x00000007bdc80000) to space 885760K, 0% used [0x0000000751a80000,0x0000000751a80000,0x0000000787b80000) ParOldGen total 5576192K, used 5576027K [0x00000005c1800000, 0x0000000715d80000, 0x0000000715d80000) object space 5576192K, 99% used [0x00000005c1800000,0x0000000715d56d38,0x0000000715d80000) Metaspace used 23223K, capacity 23502K, committed 24064K, reserved 1071104K class space used 2443K, capacity 2537K, committed 2560K, reserved 1048576K
But if i'm closing the session and opening again for every 24 iteration, records insertion succeeds and time it takes is 55 min. but the RAM utilization comes around 3GB.
Why garbage collection is not happening properly? What are the bugs in the code?