/*
 * 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.health.DaemonHealthServices;
import org.gradle.launcher.daemon.server.health.DaemonStatus;
import org.gradle.launcher.daemon.server.health.MemoryInfo;

import java.util.concurrent.TimeUnit;

public class DaemonExpirationStrategies {
    private static Logger logger = Logging.getLogger(DaemonExpirationStrategies.class);

    public static DaemonExpirationStrategy getDefaultStrategy(Daemon daemon, DaemonServices daemonServices, final DaemonServerConfiguration params) {
        ImmutableList.Builder<DaemonExpirationStrategy> strategies = ImmutableList.<DaemonExpirationStrategy>builder();
        strategies.add(getHealthStrategy(daemonServices.get(DaemonHealthServices.class).getDaemonStatus()));
        strategies.add(new DaemonIdleTimeoutExpirationStrategy(daemon, params.getIdleTimeout(), TimeUnit.MILLISECONDS));
        try {
            strategies.add(new AllDaemonExpirationStrategy(ImmutableList.of(
                new DaemonIdleTimeoutExpirationStrategy(daemon, params.getIdleTimeout() / 8, TimeUnit.MILLISECONDS),
                LowMemoryDaemonExpirationStrategy.belowFreeBytes(new MemoryInfo().getMaxMemory())
            )));
        } 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));
        return new AnyDaemonExpirationStrategy(strategies.build());
    }

    public static DaemonExpirationStrategy getHealthStrategy(DaemonStatus status) {
        return new AnyDaemonExpirationStrategy(ImmutableList.of(
            new GcThrashingDaemonExpirationStrategy(status),
            new LowTenuredSpaceDaemonExpirationStrategy(status),
            new LowPermGenDaemonExpirationStrategy(status)
        ));
    }
}

@@ -989,68 +989,8 @@
 tus;
-%0Aimport org.gradle.launcher.daemon.server.health.MemoryInfo;
 %0A%0Aim
@@ -1934,39 +1934,26 @@
 tes(
-new MemoryInfo().getMaxMemory()
+1024 * 1024 * 1024
 )%0A  

Fix the low memory threshold at 1 GB. Max memory available to JVM can sometimes be very large or even MAX_VALUE.