/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.launcher.daemon.server;

import com.google.common.collect.ImmutableList;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.launcher.daemon.configuration.DaemonServerConfiguration;
import org.gradle.launcher.daemon.server.expiry.AllDaemonExpirationStrategy;
import org.gradle.launcher.daemon.server.expiry.AnyDaemonExpirationStrategy;
import org.gradle.launcher.daemon.server.expiry.DaemonExpirationResult;
import org.gradle.launcher.daemon.server.expiry.DaemonExpirationStrategy;
import org.gradle.launcher.daemon.server.health.HealthExpirationStrategy;
import org.gradle.launcher.daemon.server.health.LowMemoryDaemonExpirationStrategy;

import java.util.concurrent.TimeUnit;

public class MasterExpirationStrategy implements DaemonExpirationStrategy {

    private static final Logger LOGGER = Logging.getLogger(MasterExpirationStrategy.class);

    private final DaemonExpirationStrategy strategy;

    public MasterExpirationStrategy(Daemon daemon, DaemonServerConfiguration params, HealthExpirationStrategy healthExpirationStrategy) {
        ImmutableList.Builder<DaemonExpirationStrategy> strategies = ImmutableList.<DaemonExpirationStrategy>builder();

        strategies.add(healthExpirationStrategy);
        strategies.add(new DaemonIdleTimeoutExpirationStrategy(daemon, params.getIdleTimeout(), TimeUnit.MILLISECONDS));

        try {
            strategies.add(new AllDaemonExpirationStrategy(ImmutableList.of(
                LowMemoryDaemonExpirationStrategy.belowFreeBytes(1024 * 1024 * 1024),
                new DaemonIdleTimeoutExpirationStrategy(daemon, params.getIdleTimeout() / 8, TimeUnit.MILLISECONDS)
            )));
        } catch (UnsupportedOperationException e) {
            LOGGER.info("This JVM does not support getting free system memory, so daemons will not check for it");
        }

        strategies.add(new DaemonRegistryUnavailableExpirationStrategy(daemon));

        this.strategy = new AnyDaemonExpirationStrategy(strategies.build());
    }

    @Override
    public DaemonExpirationResult checkExpiration() {
        return strategy.checkExpiration();
    }
}

@@ -2018,23 +2018,113 @@
     
-try %7B%0A 
+addLowMemoryDaemonExpirationStrategyWhenSupported(daemon, params.getIdleTimeout(), strategies);%0A%0A
         
    s
@@ -2115,27 +2115,24 @@
 );%0A%0A        
-   
 strategies.a
@@ -2142,138 +2142,766 @@
 new 
-AllDaemonExpirationStrategy(ImmutableList.of(%0A                LowMemoryDaemonExpirationStrategy.belowFreeBytes(1024 * 1024 * 1024)
+DaemonRegistryUnavailableExpirationStrategy(daemon));%0A%0A        this.strategy = new AnyDaemonExpirationStrategy(strategies.build());%0A    %7D%0A%0A    private void addLowMemoryDaemonExpirationStrategyWhenSupported(Daemon daemon, int idleTimeout, ImmutableList.Builder%3CDaemonExpirationStrategy%3E strategies) %7B%0A        try %7B%0A            final LowMemoryDaemonExpirationStrategy lowMemoryDaemonExpirationStrategy = LowMemoryDaemonExpirationStrategy.belowFreeBytes(1024 * 1024 * 1024);%0A            // this is to check that the JVM supports calling MemoryInfo.getFreePhysicalMemory%0A            lowMemoryDaemonExpirationStrategy.checkExpiration();%0A            strategies.add(new AllDaemonExpirationStrategy(ImmutableList.of(%0A                lowMemoryDaemonExpirationStrategy
 ,%0A  
@@ -2958,35 +2958,25 @@
 egy(daemon, 
-params.getI
+i
 dleTimeout()
@@ -2973,18 +2973,16 @@
 eTimeout
-()
  / 8, Ti
@@ -3199,168 +3199,8 @@
    %7D
-%0A%0A        strategies.add(new DaemonRegistryUnavailableExpirationStrategy(daemon));%0A%0A        this.strategy = new AnyDaemonExpirationStrategy(strategies.build());
 %0A   

Check support for LowMemoryDaemonExperiationStrategy before adding it