简体   繁体   English

Java - Java8中的JVM垃圾收集器

[英]Java - JVM Garbage Collectors in Java8

I have a basic SpringBoot 2.1.5.RELEASE app. 我有一个基本的SpringBoot 2.1.5.RELEASE应用程序。 Using Spring Initializer, JPA, embedded Tomcat. 使用Spring Initializer,JPA,嵌入式Tomcat。 In terms of the Java Garbage Collection I guess that the option B of the service is better than the option A, since the object does not remain in the memory, right ? 就Java垃圾收集而言,我猜该服务的选项B优于选项A,因为该对象不会保留在内存中,对吧?

option A: 选项A:

@Override
    public List<HotelPriceSummary> overRanked7d(User user) {

        List<HotelPriceSummary> overRanked7dList =

                allNonFavoritedHotels(user)
                        .parallelStream()
                        .filter(HotelPriceSummary.overRanked7dHotelsPredicate())                            
                        .sorted(comparing((HotelPriceSummary cps) -> cps.getDailyPercentageChange()).reversed())
                        .collect(toList());

        return overRanked7dList;
    }

option B: 选项B:

@Override
    public List<HotelPriceSummary> overRanked7d(User user) {

        return

                allNonFavoritedHotels(user)
                        .parallelStream()
                        .filter(HotelPriceSummary.overRanked7dHotelsPredicate())                            
                        .sorted(comparing((HotelPriceSummary cps) -> cps.getDailyPercentageChange()).reversed())
                        .collect(toList());


    }

From JVM implementation point of view, the only difference is declaring a new object reference to the returned object. 从JVM实现的角度来看,唯一的区别是声明对返回对象的新对象引用。 Hence, no new object is being created in either case and no new memory is being used. 因此,在任何一种情况下都没有创建新对象,也没有使用新内存。 From coding perspective, option B is preferred. 从编码角度来看,选项B是首选。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM