Learning Lambda

I think Java lambdas have some powerful functional programming support and this is an attempt to convert part of the ‘R’ code to Java.

I start with this line from the GC log file.

Total Memory Pools: 5

Pool: Code Cache (Non-heap memory)

Peak Usage : init:2359296, used:13914944, committed:13959168, max:50331648
Current Usage : init:2359296, used:13913536, committed:13959168, max:50331648

|------------------| committed:13.31Mb
+---------------------------------------------------------------------+
|//////////////////| | max:48Mb
+---------------------------------------------------------------------+
|------------------| used:13.27Mb

Pool: PS Eden Space (Heap memory)

Peak Usage : init:89522176, used:186449920, committed:234356736, max:234618880
Current Usage : init:89522176, used:36258088, committed:233570304, max:233635840

|--------------------------------------------------------------------| committed:222.75Mb
+---------------------------------------------------------------------+
|////////// || max:222.81Mb
+---------------------------------------------------------------------+
|---------| used:34.58Mb

Pool: PS Survivor Space (Heap memory)

Peak Usage : init:14876672, used:25217592, committed:35651584, max:35651584
Current Usage : init:14876672, used:0, committed:1114112, max:1114112

|---------------------------------------------------------------------| committed:1.06Mb
+---------------------------------------------------------------------+
| | max:1.06Mb
+---------------------------------------------------------------------+
| used:0b

Pool: PS Old Gen (Heap memory)

Peak Usage : init:954466304, used:272270080, committed:954466304, max:1886519296
Current Usage : init:954466304, used:269639088, committed:954466304, max:1886519296

|----------------------------------| committed:910.25Mb
+---------------------------------------------------------------------+
|////////// | | max:1.76Gb
+---------------------------------------------------------------------+
|---------| used:257.15Mb

Pool: PS Perm Gen (Non-heap memory)

Peak Usage : init:16777216, used:112329944, committed:210763776, max:1073741824
Current Usage : init:16777216, used:112329944, committed:112852992, max:1073741824

|------| committed:107.62Mb
+---------------------------------------------------------------------+
|//////| | max:1Gb
+---------------------------------------------------------------------+
|------| used:107.13Mb

tables <- htmlParse("memorypools.txt")

y <- xpathSApply(tables,"//b//text()[contains(.,'Pool: Code Cache')]//following::blockquote[1]",xmlValue)
y <- data.frame(y)

I use ‘R’ to parse the line first because I found it hard to use Java to parse the unstructured line with HTML tags

After the first phase the line looks like this.

[1] "Peak Usage : init:2359296, used:13914944, committed:13959168, max:50331648Current Usage : init:2359296, used:13913536, committed:13959168, max:50331648|------------------| committed:13.31Mb+---------------------------------------------------------------------+|//////////////////| | max:48Mb+---------------------------------------------------------------------+|------------------| used:13.27Mb"

Java 8 code

import java.io.IOException;
import java.nio.file.Files;
import java.util.Scanner;
import java.util.stream.Stream;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.Paths.get;

public class GCLogParser {

    private void readFile() throws IOException {

        Stream<String> s = Files.lines(get("gclog.txt"), UTF_8);
        s.forEach( this::parse  );
    }

    public static void parseDocument(Parseable parser,
                                     String data){
        parser.apply(data);
    }
    private void parse( String data ){

        Scanner s = new Scanner( data );
        s.findInLine("Current.*?[/|]");
        System.out.println(s.match().group());
    }

    public static void main( String... argv ) throws IOException {
        new GCLogParser().readFile();
    }

    @FunctionalInterface
    public interface Parseable{
         void apply( String s);
    }
}

This Java code gives me this line. This is just new syntax and constrcts and I don’t think I have used the functional style of programming properly here.

Current Usage : init:2359296, used:15775680, committed:15859712, max:50331648|

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: