Mayday
Mayday

Reputation: 5146

Java Lambda performance vs declared Suppliers/Functions

I am starting to learn how to develop with Optional in java8. It might be it is somwehere documented, but I have been using google with no accurate result.

I have different possible implementations for the orElseGet method, and I am not sure if java makes a better memory handling in some case, or if its pretty much the same.

Lets say I have a method in a class with an Optional defined:

class myClass {

  final static private Supplier<Object> MY_SUPPLIER = () -> new Object();

  private void myMethod1 () {
    Optional<Object> x; // somehow Initialized
    Object y = x.orElseGet(() -> new Object());
  }

  private void myMethod2 () {
    Optional<Object> x; // somehow Initialized
    Object y = x.orElseGet(MY_SUPPLIER);
  }
}

From my humble point of view, this second should have better memory management in java, since it's only declared once the Supplier, and it is always used the same.

1) is this true?

Now, let's go further, and imagine we need to supply different object depending on parameters.

class myClass2 {

  final static private Function<String, Supplier<AnyCustomClass>> MY_SUPPLIER_PROVIDER = (p) -> () -> new AnyCustomClass(p);

  private void myMethod1 (String arg) {
    Optional<AnyCustomClass> x; // somehow Initialized
    AnyCustomClass y = x.orElseGet(() -> new AnyCustomClass(arg));
  }

  private void myMethod2 (String arg) {
    Optional<AnyCustomClass> x; // somehow Initialized
    AnyCustomClass y = x.orElseGet(MY_SUPPLIER_PROVIDER.apply(arg));
  }
}

In this case, depending on the argument, each time it returns different supplier.

2) Does java have better memory management here also?

3) Do they somehow get "cached" for arg taking same value?

EDIT

By watching Does a lambda expression create an object on the heap every time it's executed? I understand that my first class behaviour is answered. Since there are no local variables, it will create a singleton (at least oracle jvm)

How ever, I do not feel like that answer provides accurate information to answer my 2) and 3)

Upvotes: 3

Views: 1084

Answers (1)

Doctor Parameter
Doctor Parameter

Reputation: 1211

The difference between both methods is you're reusing the supplier, which will reuse objects thus save you some memory. The thing you need to be careful with here is possible threading issues. Since you're using the same memory you need to make sure different threads do not try to use the same object.

So to answer your questions:

1.) Yes, but you may have other issues.

2.) It the same as the first question? You're reusing a instance of the Function. This is'nt how you're supposed to be using Function in this context.

3.) It's 'cached' in that the instance of the function is reused.

In general I'd stay away from your second options, unless this is being called something like 1000 / second you're trading added complexity which is not going to translate into noticeable performance.

If you're interested in what the performance is going to look like write a unit test which calls this multiple times on different threads and monitor the run with the profiler.

Upvotes: 1

Related Questions