import org.junit.Test; 

import java.util.Timer; 
import java.util.TimerTask; 
import java.util.concurrent.Executors; 
import java.util.concurrent.ScheduledExecutorService; 
import java.util.concurrent.ScheduledFuture; 
import java.util.concurrent.TimeUnit; 

public class JI9055915 {

	@Test 
	public void testTimer() { 
		Timer timer = new Timer(); 

		long startMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); 
		long maxMemory = startMemory; 
		for(int i = 0; i < 1000000; i++) { 
			TimerTask task = new TimerTask() { 
				@Override 
				public void run() { 
					// NoOp 
				} 
			}; 
			timer.schedule(task, TimeUnit.MILLISECONDS.convert(10, TimeUnit.MINUTES)); 
			task.cancel(); 

			long currentMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); 
			if(currentMemory > maxMemory) { 
				maxMemory = currentMemory; 
			} 
		} 

		timer.purge(); 

		long currentMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); 
		System.out.println("initial memory: " + startMemory); 
		System.out.println("maximum memory: " + maxMemory); 
		System.out.println("current memory: " + currentMemory); 
	} 

	@Test 
	public void testService() { 
		ScheduledExecutorService timer = Executors.newScheduledThreadPool(1); 

		long startMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); 
		long maxMemory = startMemory; 
		for(int i = 0; i < 1000000; i++) { 
			ScheduledFuture<?> task = timer.schedule(() -> { 
				// NoOp 
			}, 10, TimeUnit.MINUTES); 
			task.cancel(false); 

			long currentMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); 
			if(currentMemory > maxMemory) { 
				maxMemory = currentMemory; 
			} 
		} 

		long currentMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); 
		System.out.println("initial memory: " + startMemory); 
		System.out.println("maximum memory: " + maxMemory); 
		System.out.println("current memory: " + currentMemory); 
	} 

}
