1 /* TestUtils
2 *
3 * $Id: TestUtils.java 6434 2009-08-04 06:50:43Z gojomo $
4 *
5 * Created on Dec 28, 2004
6 *
7 * Copyright (C) 2004 Internet Archive.
8 *
9 * This file is part of the Heritrix web crawler (crawler.archive.org).
10 *
11 * Heritrix is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * any later version.
15 *
16 * Heritrix is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser Public License
22 * along with Heritrix; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25 package org.archive.util;
26
27 import java.lang.ref.SoftReference;
28 import java.util.LinkedList;
29 import java.util.logging.Logger;
30
31 /***
32 * Utility methods useful in testing situations.
33 *
34 * @author gojomo
35 */
36 public class TestUtils {
37 private static final Logger logger =
38 Logger.getLogger(TestUtils.class.getName());
39
40 /***
41 * Temporarily exhaust memory, forcing weak/soft references to
42 * be broken.
43 */
44 public static void forceScarceMemory() {
45 // force soft references to be broken
46 LinkedList<SoftReference<byte[]>> hog = new LinkedList<SoftReference<byte[]>>();
47 long blocks = Runtime.getRuntime().maxMemory() / 1000000;
48 logger.info("forcing scarce memory via "+blocks+" 1MB blocks");
49 for(long l = 0; l <= blocks; l++) {
50 try {
51 hog.add(new SoftReference<byte[]>(new byte[1000000]));
52 } catch (OutOfMemoryError e) {
53 hog = null;
54 logger.info("OOME triggered");
55 break;
56 }
57 }
58 }
59 }