Reputation: 49
public class ReportDatabase {
private static final Map<String, Report> reportTypes;
private static final String[] reportTypeNameVersion = {
"Payroll Tax Report 1",
"Capital Gains Tax Report 1",
"Overall Expenditure Report 1",
"Dark Costs Report 1",
"Petty Cash Report 1",
"Enron Fund Report 1",
"COVID-19 Report 1",
"COVID-20 Report 1",
"Brawndo Investment Report 1",
"Philanthropy Report 1",
"Payroll Tax Report 2",
"Capital Gains Tax Report 2",
"Overall Expenditure Report 2",
"Dark Costs Report 2",
"Petty Cash Report 2",
"Enron Fund Report 2",
"COVID-19 Report 2",
"COVID-20 Report 2",
"Brawndo Investment Report 2",
"Philanthropy Report 2"
};
static {
reportTypes = new HashMap<>();
for (String name: reportTypeNameVersion) {
reportTypes.put(name, new ReportImpl(name.substring(0, name.length()-1), getReportCommission(), getReportData(), getReportData(), getReportData(), getReportData(), getReportData()));
}
}
public static Collection<Report> getTestReports() {
Collection<Report> originals = reportTypes.values();
List<Report> result = new ArrayList<>();
for (Report original: originals) {
Report report = (new ReportImpl(original.getReportName(),
original.getCommission(),
original.getLegalData().clone(),
original.getCashFlowData().clone(),
original.getMergesData().clone(),
original.getTallyingData().clone(),
original.getDeductionsData().clone()));
result.add(report);
for(int i = 0; i < original.getLegalData().length; i++) {
System.out.println(original.getLegalData()[i]);
}
}
return result;
}
private static double[] getReportData() {
double[] result = new double[100];
Random random = new Random();
for (int i = 0; i < result.length; i++) {
result[i] = random.nextDouble();
}
return result;
}
private static double getReportCommission() {
Random random = new Random();
return 1.0 + 99.0 * random.nextDouble();
}
}
The above class is a class that I cannot modify anything.
The goal is to reduce the RAM usage and I check the RAM usage.
If I remove .clone() in getTestReports, the RAM usage decreases from 800~MB to 400~MB, but actually I cannot remove it because of its business rule.
When I call the getTestReports in other class, it directly takes 800~MB RAM consumption.
So, what I want to ask is, is there anyway to remove .clone() in getTestReports() method outside of its class such as ReportImpl class which implements Report interface and contains variables such as double[] legalData and getter methods for them?
Upvotes: 0
Views: 146