Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
OutOfMemoryError:Java堆空间可能来自不可变列表?_Java_Memory_Memory Leaks_Out Of Memory_Immutability - Fatal编程技术网

OutOfMemoryError:Java堆空间可能来自不可变列表?

OutOfMemoryError:Java堆空间可能来自不可变列表?,java,memory,memory-leaks,out-of-memory,immutability,Java,Memory,Memory Leaks,Out Of Memory,Immutability,我实现了一个名为BusinessData的类。构造函数接受许多双数组,但我希望双数组是不可变的,所以我实现了这个类a值对象。我对双数组使用了一个不可变的集合 然而,我得到了一个OutOfMemoryError:Java堆空间。我认为这与垃圾收集和将数组转换为列表,然后将其转换回数组有关。我这样做是因为我无法更改构造函数或方法签名 有没有使用更少内存的更好的解决方案?我知道我的实现使用了大量内存。多谢各位 public class BusinessData implements Business

我实现了一个名为BusinessData的类。构造函数接受许多双数组,但我希望双数组是不可变的,所以我实现了这个类a值对象。我对双数组使用了一个不可变的集合

然而,我得到了一个OutOfMemoryError:Java堆空间。我认为这与垃圾收集和将数组转换为列表,然后将其转换回数组有关。我这样做是因为我无法更改构造函数或方法签名

有没有使用更少内存的更好的解决方案?我知道我的实现使用了大量内存。多谢各位


public class BusinessData implements Business{

    // Using an immutable collection to make data of report immutable
    private final String name;
    private final double commissionPerEmployee;
    private List<Double> firstData;
    private List<Double> secondData;
    private List<Double> thirdData;

    public BusinessData(String name,
                      double commissionPerEmployee,
                      double[] firstData,
                      double[] secondData,
                      double[] thirdData) {

        this.name = name;
        this.commissionPerEmployee = commissionPerEmployee;

        if (firstData == null) {
            this.firstData = null;
        } else {
            this.firstData = Collections.unmodifiableList(DoubleStream.of(firstData).boxed().collect(Collectors.toList()));;
        }

        if (secondData == null) {
            this.secondData = null;
        } else {
            this.secondData = Collections.unmodifiableList(DoubleStream.of(secondData).boxed().collect(Collectors.toList()));;
        }

        if (thirdData == null) {
            this.thirdData = null;
        } else {
            this.thirdData = Collections.unmodifiableList(DoubleStream.of(thirdData).boxed().collect(Collectors.toList()));;
        }
    }

    @Override
    public String getBusinessName() {
        return name;
    }

    @Override
    public double getIncome() {
        return commissionPerEmployee;
    }


    @Override
    public double[] getFirstData() {
        if (firstData == null) {
            return null;
        } else {
            return firstData.stream().mapToDouble(x -> x).toArray();
        }
    }

    @Override
    public double[] getSecondData() {
        if (secondData == null) {
            return null;
        } else {
            return secondData.stream().mapToDouble(x -> x).toArray();
        }
    }

    @Override
    public double[] getThirdData() {
        if (thirdData == null) {
            return null;
        } else {
            return thirdData.stream().mapToDouble(x -> x).toArray();
        }
    }

    @Override
    public boolean equals(Object object) {
        if (object == null || getClass() != object.getClass()) {
            return false;
        }

        Business that = (Business) object;
        return Objects.equals(this.getBusinessName(), that.getBusinessName()) &&
                Objects.equals(this.getIncome(), that.getIncome()) &&               
                Objects.equals(this.getFirstData(), that.getLegalData()) &&
                Objects.equals(this.getSecondData(), that.getLegalData()) &&
                Objects.equals(this.getThirdData(), that.getLegalData());
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, commissionPerEmployee, firstData, secondData, thirdData);
    }
}

公共类BusinessData实现业务{
//使用不可变集合使报表数据不可变
私有最终字符串名;
私人最终双佣金;
私有列表数据;
私有列表数据;
私人名单第三数据;
public BusinessData(字符串名称,
双佣金再就业,
双重数据,
双[]秒数据,
双[]第三十数据){
this.name=名称;
this.commissionPerEmployee=佣金peremployee;
if(firstData==null){
this.firstData=null;
}否则{
this.firstData=Collections.unmodifiableList(DoubleStream.of(firstData).boxed().collect(Collectors.toList());;
}
if(secondData==null){
this.secondData=null;
}否则{
this.secondData=Collections.unmodifiableList(DoubleStream.of(secondData.boxed().collect(Collectors.toList());;
}
if(thirdData==null){
this.thirdData=null;
}否则{
this.thirdData=Collections.unmodifiableList(DoubleStream.of(thirdData.boxed().collect(Collectors.toList());;
}
}
@凌驾
公共字符串getBusinessName(){
返回名称;
}
@凌驾
公共收入{
回扣;
}
@凌驾
public double[]getFirstData(){
if(firstData==null){
返回null;
}否则{
返回firstData.stream().mapToDouble(x->x.toArray();
}
}
@凌驾
public double[]getSecondData(){
if(secondData==null){
返回null;
}否则{
返回secondData.stream().mapToDouble(x->x.toArray();
}
}
@凌驾
公共双[]getthirdata(){
if(thirdData==null){
返回null;
}否则{
返回thirdData.stream().mapToDouble(x->x.toArray();
}
}
@凌驾
公共布尔等于(对象){
if(object==null | | getClass()!=object.getClass()){
返回false;
}
业务that=(业务)对象;
返回Objects.equals(this.getBusinessName(),that.getBusinessName())&&
Objects.equals(this.getIncome()、that.getIncome())&&
Objects.equals(this.getFirstData()、that.getLegalData())&&
Objects.equals(this.getSecondData(),that.getLegalData())&&
Objects.equals(this.getthirdata(),that.getLegalData());
}
@凌驾
公共int hashCode(){
return Objects.hash(name,commissionPerEmployee,firstData,secondData,thirdData);
}
}

如果我将列表更改为静态,则不再显示错误。但是,我认为我不应该这样做。如果我将列表更改为静态,它不会再给我错误。然而,我认为我不应该这样做。