Last active
September 30, 2021 17:46
-
-
Save Mizux/86efa45dd061e8e9a8599a4776a5121d to your computer and use it in GitHub Desktop.
OR-Tools Java MemoryLeakSolver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2010-2018 Google LLC | |
// 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 com.google.ortools; | |
import com.google.ortools.Loader; | |
import com.google.ortools.sat.CpSolverStatus; | |
import com.google.ortools.sat.IntervalVar; | |
import com.google.ortools.sat.CpModel; | |
import com.google.ortools.sat.CpSolver; | |
import com.google.ortools.sat.IntVar; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.stream.IntStream; | |
import java.util.Random; | |
import java.util.logging.Logger; | |
/** Memory Leak Solver Samples */ | |
public class MemoryLeakSolver { | |
private static final Logger logger = Logger.getLogger(MemoryLeakSolver.class.getName()); | |
public static final int NUM_OBSERVATIONS = 6000; | |
public static final int NUM_NIGHTS = 180; | |
public static final int SLOTS_PER_NIGHT = 10; | |
public static final int SLOTS_PER_DAY = 24; | |
public static final int TOTAL_SLOTS = NUM_NIGHTS * SLOTS_PER_DAY; | |
public static long getMaxMemory() { | |
return Runtime.getRuntime().maxMemory(); | |
} | |
public static long getUsedMemory() { | |
return getMaxMemory() - getFreeMemory(); | |
} | |
public static long getTotalMemory() { | |
return Runtime.getRuntime().totalMemory(); | |
} | |
public static long getFreeMemory() { | |
return Runtime.getRuntime().freeMemory(); | |
} | |
public static boolean schedule(List<Integer> obs) { | |
// create constraint model | |
CpModel model = new CpModel(); | |
List<IntervalVar> intervals = new LinkedList<IntervalVar>(); | |
for (int i = 0; i < obs.size(); i++) { | |
// constraint: observation intervals with their duration | |
IntVar from = model.newIntVar(0, TOTAL_SLOTS - 1, "OB from " + i); | |
IntVar to = model.newIntVar(0, TOTAL_SLOTS - 1, "OB to " + i); | |
intervals.add(model.newIntervalVar(from, obs.get(i), to, "OB Interval " + i)); | |
}; | |
// constraint: exclude daytime slots 10 ... 23 | |
IntStream.range(0, NUM_NIGHTS).forEach(night -> { | |
intervals.add(model.newFixedInterval(night * SLOTS_PER_DAY + SLOTS_PER_NIGHT, SLOTS_PER_DAY - SLOTS_PER_NIGHT, | |
"Reserved day daytime " + night)); | |
}); | |
// constraint: no slot overlap | |
model.addNoOverlap(intervals.toArray(new IntervalVar[0])); | |
CpSolverStatus status = new CpSolver().solve(model); | |
return status == CpSolverStatus.OPTIMAL || status == CpSolverStatus.FEASIBLE; | |
} | |
public static void main(String[] args) throws Exception { | |
Loader.loadNativeLibraries(); | |
// prepare observations with variable duration 1 .. 10 | |
Random rand = new Random(); | |
Integer[] observations = new Integer[NUM_OBSERVATIONS]; | |
IntStream.range(0, observations.length).forEach(i -> { | |
observations[i] = 1 + rand.nextInt(3); | |
}); | |
LinkedList<Integer> scheduledObservations = new LinkedList<Integer>(); | |
System.out.println("Total observations to schedule: " + observations.length); | |
IntStream.range(0, observations.length).forEach(i -> { | |
System.out.println("KB: " + getUsedMemory() / 1024); | |
scheduledObservations.add(observations[i]); | |
if (schedule(scheduledObservations)) { | |
System.out.println("Scheduled observation " + (i + 1)); | |
} else { | |
scheduledObservations.removeLast(); | |
System.out.println("Skipping observation " + (i + 1)); | |
} | |
}); | |
System.out.println("Done. Scheduled " + scheduledObservations.size() + " observations"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project | |
xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.google.ortools</groupId> | |
<artifactId>MemoryLeakSolver</artifactId> | |
<version>1.0.0</version> | |
<packaging>jar</packaging> | |
<name>${project.groupId}:${project.artifactId}</name> | |
<description>Google OR-Tools Java project.</description> | |
<url>https://github.com/google/or-tools</url> | |
<licenses> | |
<license> | |
<name>The Apache License, Version 2.0</name> | |
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> | |
</license> | |
</licenses> | |
<developers> | |
<developer> | |
<name>Corentin "Mizux" Le Molgat</name> | |
<email>[email protected]</email> | |
<organization>Google LLC</organization> | |
<organizationUrl>http://www.google.com</organizationUrl> | |
</developer> | |
<developer> | |
<name>Laurent Perron</name> | |
<email>[email protected]</email> | |
<organization>Google LLC</organization> | |
<organizationUrl>http://www.google.com</organizationUrl> | |
</developer> | |
</developers> | |
<scm> | |
<connection>scm:git:git://github.com/google/or-tools.git</connection> | |
<developerConnection>scm:git:ssh://github.com:google/or-tools.git</developerConnection> | |
<url>http://github.com/google/or-tools/tree/master</url> | |
<tag>HEAD</tag> | |
</scm> | |
<issueManagement> | |
<system>GitHub Issues</system> | |
<url>http://github.com/google/or-tools/issues</url> | |
</issueManagement> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<exec.mainClass>com.google.ortools.MemoryLeakSolver</exec.mainClass> | |
<maven.compiler.source>1.8</maven.compiler.source> | |
<maven.compiler.target>1.8</maven.compiler.target> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>com.google.ortools</groupId> | |
<artifactId>ortools-java</artifactId> | |
<version>[8.0,)</version> | |
<type>jar</type> | |
<scope>compile</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-source-plugin</artifactId> | |
<version>3.2.0</version> | |
<executions> | |
<execution> | |
<id>attach-sources</id> | |
<goals> | |
<goal>jar-no-fork</goal> | |
</goals> | |
</execution> | |
</executions> | |
</plugin> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-javadoc-plugin</artifactId> | |
<version>3.2.0</version> | |
<executions> | |
<execution> | |
<id>attach-javadocs</id> | |
<goals> | |
<goal>jar</goal> | |
</goals> | |
</execution> | |
</executions> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment