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();
}
}
上面的类是我不能修改任何东西的类。
目标是减少 RAM 使用量,我检查 RAM 使用量。
如果我在 getTestReports 中删除 .clone(),RAM 使用量会从 800~MB 减少到 400~MB,但实际上我无法删除它,因为它的业务规则。
当我在其他类中调用 getTestReports 时,它直接需要 800~MB 的 RAM 消耗。
所以,我想问的是,无论如何要在其类之外的 getTestReports() 方法中删除 .clone(),例如 ReportImpl 类,它实现了 Report 接口并包含变量,例如 double[] legalData 和 getter 方法?