Java No.3 for, stream memory usage useful for business

Since it is troublesome to turn for, if you basically write all the list creation with stream To my senior, "stream is actually a for statement, so it's a waste of memory." I got a review saying that I was a little irritated, so I checked it.

Try using a simple memory measurement method

First of all, do nothing

DecimalFormat f1 = new DecimalFormat("#,###KB");
DecimalFormat f2 = new DecimalFormat("##.#");
long free = Runtime.getRuntime().freeMemory() / 1024;
long total = Runtime.getRuntime().totalMemory() / 1024;
long max = Runtime.getRuntime().maxMemory() / 1024;
long used = total - free;
double ratio = (used * 100 / (double)total);
String info = 
"Java memory information:total=" + f1.format(total) + "、" +
            "amount to use=" + f1.format(used) + " (" + f2.format(ratio) + "%)、" +
	    "Maximum available="+f1.format(max);
System.out.println(info);
Java memory information:total=125,952KB, usage=1,997KB (1.6%), Maximum available=1,864,192KB

Next, classify the numbers up to 10000 into 3 using for minutes.

List<Integer> list = new ArrayList<>();
List<Integer> devided1 = new ArrayList<>();
List<Integer> devided2 = new ArrayList<>();
List<Integer> devided3 = new ArrayList<>();
for(int i = 1; i<=10000 ; i++) {
	list.add(i);
}
for(Integer num : list) {
	if(num % 3 == 1) {
		devided1.add(num);
	}
	if(num % 3 == 2) {
		devided2.add(num);
	}
	if(num % 3 == 0) {
		devided3.add(num);
	}
}
Java memory information:total=125,952KB, usage=2,665KB (2.1%), Maximum available=1,864,192KB

Finally the same thing using stream

List<Integer> devided1 = list.stream()
				.filter(i -> i%3 ==1)
				.collect(Collectors.toList());
List<Integer> devided2 = list.stream()
				.filter(i -> i%3 ==2)
				.collect(Collectors.toList());
List<Integer> devided3 = list.stream()
				.filter(i -> i%3 ==0)
				.collect(Collectors.toList());
Java memory information:total=125,952KB, usage=3,995KB (3.2%), Maximum available=1,864,192KB

I'm sorry to say cheeky seniors. After all, if you repeat the same process three times, it will consume memory. As far as this number is seen, it seems that the usage amount of for is smaller even if the same processing is performed once.

I have doubts about the term "real for sentence" After all, it is not always good to use a new one.

However, I think the latter is the readability and ease of writing.

Recommended Posts

Java No.3 for, stream memory usage useful for business
[Java11] Stream Usage Summary -Basics-
Java application for beginners: stream
Measuring instance memory usage in Java
Memory measurement for Java apps using jstat
Examine the memory usage of Java elements
Return value when using Java No. 1 Optional.ifPresent that was useful in business
Ask for n business days later with JAVA
Is Java SE8 Silver useful for Java development work? ??
[JAVA] Stream type
For JAVA learning (2018-03-16-01)
[Java development] Java memory
[Java] Problem No. 2
[Java] Problem No.3
2017 IDE for Java
[Java] Problem No.1
Studying Java 8 (Stream)
Java Stream termination
Java for statement
[Java] Stream processing
Java 9 Optional :: stream
[For beginners] How to operate Stream API after Java 8
Filter the Java No. 2 Optional list that was useful in business and get the first value