jdk8foreach mass creation object optimization

Keywords: Java Lambda

jdk8 foreach creation object optimization

lambda foreach creating objects

@Async
    public void asyncFullEsDoc() {
        List<Integer> docIdList = Arrays.asList(913,914);
        if (CollectionUtil.isNotNullOrEmpty(docIdList)){
            List<Document> documents = new ArrayList<>(500);
            docIdList.forEach(docId ->{
                QueryKnowledgeDocResponse doc = synchronizeRedisBaseDoc(docId);
                if (!StringUtils.isBlank(doc)){
                    Map<String, Object> docMap = BeanToMap.objectToMap(doc);
                    Document document = new Document();
                    document.setDocumentId(docId.toString()).setDocument(docMap);
                    documents.add(document);
                }
            });

            ...
        }
    }

Analysis, object release optimization

    ...
    
        List<Document> documents = new ArrayList<>(500);
            Document document = new Document();
            docIdList.forEach(docId ->{
                //For object release
                document.setDocumentId(null);
                document.setDocument(null);
                QueryKnowledgeDocResponse doc = synchronizeRedisBaseDoc(docId);
                if (!StringUtils.isBlank(doc)){
                    Map<String, Object> docMap = BeanToMap.objectToMap(doc);
                    document.setDocumentId(docId.toString()).setDocument(docMap);
                    documents.add(document);
                }
            });

    ...

In addList, the last value overwrites all previous values, but the values of each object in foreach are different.

Analysis, code optimization continues

    ...
        
    List<Document> documents = new ArrayList<>(500);
            Document document = null;
            for (Integer docId: docIdList) {
                document = new Document();
                QueryKnowledgeDocResponse doc = synchronizeRedisBaseDoc(docId);
                if (!StringUtils.isBlank(doc)){
                    Map<String, Object> docMap = BeanToMap.objectToMap(doc);
                    document.setDocumentId(docId.toString()).setDocument(docMap);
                    documents.add(document);
                }
            }
    ...

If I still want to create objects with lambda foreach

    ...
        List<Document> documents = new ArrayList<>(800);
            final Document[] document = new Document[1];
            docIdList.forEach(docId ->{
                QueryKnowledgeDocResponse doc = synchronizeRedisBaseDoc(docId);
                if (!StringUtils.isBlank(doc)){
                    Map<String, Object> docMap = BeanToMap.objectToMap(doc);
                    document[0] = new Document();
                    document[0].setDocumentId(docId.toString()).setDocument(docMap);
                    documents.add(document[0]);
                }
            });
    ...
    

Analysis

Object object= new Object();

Writing in 100 cycles means that you have 100 references corresponding to 100 objects, so 100 objects occupy memory for a period of time, knowing that the memory is not enough GC takes the initiative to recycle.

object = new Object();

Writing in 100 cycles means that you have called 100 objects 100 times with one reference respectively, so when the latter object init, the former object is already in "no reference state" and will be automatically recycled by GC (it is important that you have done GC recycling many times before the end of your cycle)

Better memory management is needed.

Posted by padanaram on Wed, 04 Dec 2019 19:14:09 -0800