Home of The JavaSpecialists' Newsletter

247Memory Puzzle with Lambdas

Posted: 2017-03-31Category: PerformanceJava Version: 1.8Dr. Heinz M. Kabutz
 

Abstract: Just like inner classes, lambdas might also suffer from memory issues, but to a lesser extent. In this puzzle you need to figure out which lambdas are affected.

 

Welcome to the 247th edition of The Java(tm) Specialists' Newsletter, sent to you from the beautiful Island of Crete. It is the season for wild asparagus, which grows naturally underneath olive trees on our mountains. Healthy, crunchy and delicious. Eaten raw or boiled. Even though most people come for the beaches, Crete has far more interesting food to offer at this time of year.

NEW: Refactoring to Java 8 Streams and Lambdas Workshop Are you currently using Java 6 or 7 and would like to see how Java 8 can improve your code base? Are you tired of courses that teach you a whole bunch of techniques that you cannot apply in your world? Check out our one day intensive Refactoring to Java 8 Streams and Lambdas Workshop.

Memory Puzzle with Lambdas

When someone with 315094 Twitter followers asks if I could post a Java puzzle, I am happy to oblige :) Here it is. Besides the "Hi from ... " messages, what other output do you see?

import java.util.concurrent.*;

import static java.util.concurrent.TimeUnit.*;

public class MemoryPuzzleWithLambdas {
  static class A {
    public Runnable get() {
      return new Runnable() {
        public void run() {
          System.out.println("Hi from A");
        }
      };
    }

    protected void finalize() throws Throwable {
      System.out.println("A finalized");
    }
  }

  static class B {
    public Runnable get() {
      return () -> System.out.println("Hi from B");
    }

    protected void finalize() throws Throwable {
      System.out.println("B finalized");
    }
  }

  static class C {
    private int count = 0;

    public Runnable get() {
      return () -> System.out.println("Hi from C #" + ++count);
    }

    protected void finalize() throws Throwable {
      System.out.println("C finalized");
    }
  }

  static class D {
    private static int count = 0;

    public Runnable get() {
      return () -> System.out.println("Hi from D #" + ++count);
    }

    protected void finalize() throws Throwable {
      System.out.println("D finalized");
    }
  }

  static class E {
    private int count = ThreadLocalRandom.current().nextInt();

    public Runnable get() {
      int count = this.count;
      return () -> System.out.println("Hi from E " + count);
    }

    protected void finalize() throws Throwable {
      System.out.println("E finalized");
    }
  }

  static class F {
    public Runnable get() {
      return this::friendly;
    }

    public void friendly() {
      System.out.println("Hi from F");
    }

    protected void finalize() throws Throwable {
      System.out.println("F finalized");
    }
  }

  public static void main(String... args) {
    ScheduledExecutorService timer =
        Executors.newSingleThreadScheduledExecutor();
    timer.scheduleAtFixedRate(new A().get(), 1, 1, SECONDS);
    timer.scheduleAtFixedRate(new B().get(), 1, 1, SECONDS);
    timer.scheduleAtFixedRate(new C().get(), 1, 1, SECONDS);
    timer.scheduleAtFixedRate(new D().get(), 1, 1, SECONDS);
    timer.scheduleAtFixedRate(new E().get(), 1, 1, SECONDS);
    timer.scheduleAtFixedRate(new F().get(), 1, 1, SECONDS);
    timer.scheduleAtFixedRate(System::gc, 1, 1, SECONDS);
  }
}
  

See if you can figure it out without first running the code :-)

Kind regards from Crete

Heinz

 

Related Articles

Browse the Newsletter Archive

About the Author

demo

Java Champion, author of the Javaspecialists Newsletter, conference speaking regular... About Heinz

Java Training

We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.

Java Consulting

Nobody ever wants to call a Java performance consultant, but with first-hand experience repairing and improving commercial Java applications - JavaSpecialists are a good place to start...

Threading Emergency?

If your system is down, we will review it for 15 minutes and give you our findings for just 1 € without any obligation.