001 /* 002 * Copyright (C) 2008-2010 by Holger Arndt 003 * 004 * This file is part of the Universal Java Matrix Package (UJMP). 005 * See the NOTICE file distributed with this work for additional 006 * information regarding copyright ownership and licensing. 007 * 008 * UJMP is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation; either version 2 011 * of the License, or (at your option) any later version. 012 * 013 * UJMP is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public 019 * License along with UJMP; if not, write to the 020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 021 * Boston, MA 02110-1301 USA 022 */ 023 024 package org.ujmp.core.util; 025 026 public abstract class GCUtil { 027 028 private static final Runtime runtime = Runtime.getRuntime(); 029 030 private static final long maxMemory = runtime.maxMemory(); 031 032 public static void gc() { 033 try { 034 long tmpIsFree = runtime.freeMemory(); 035 long tmpWasFree; 036 037 do { 038 tmpWasFree = tmpIsFree; 039 runtime.runFinalization(); 040 runtime.gc(); 041 Thread.sleep(10); 042 tmpIsFree = runtime.freeMemory(); 043 } while (tmpIsFree > tmpWasFree); 044 045 runtime.runFinalization(); 046 } catch (Exception e) { 047 e.printStackTrace(); 048 } 049 } 050 051 public static void purgeMemory() { 052 try { 053 long tmpIsFree = runtime.freeMemory(); 054 long tmpWasFree; 055 byte[] a1 = null; 056 byte[] a2 = null; 057 byte[] a3 = null; 058 byte[] a4 = null; 059 byte[] a5 = null; 060 byte[] a6 = null; 061 byte[] a7 = null; 062 byte[] a8 = null; 063 byte[] a9 = null; 064 try { 065 a1 = new byte[(int) (maxMemory * 0.1)]; 066 a2 = new byte[(int) (maxMemory * 0.1)]; 067 a3 = new byte[(int) (maxMemory * 0.1)]; 068 a4 = new byte[(int) (maxMemory * 0.1)]; 069 a5 = new byte[(int) (maxMemory * 0.1)]; 070 a6 = new byte[(int) (maxMemory * 0.1)]; 071 a7 = new byte[(int) (maxMemory * 0.1)]; 072 a8 = new byte[(int) (maxMemory * 0.1)]; 073 a9 = new byte[(int) (maxMemory * 0.1)]; 074 } catch (Throwable t1) { 075 } 076 runtime.runFinalization(); 077 runtime.gc(); 078 Thread.sleep(10); 079 a1 = a1 == null ? null : null; 080 a2 = a2 == null ? null : null; 081 a3 = a3 == null ? null : null; 082 a4 = a4 == null ? null : null; 083 a5 = a5 == null ? null : null; 084 a6 = a6 == null ? null : null; 085 a7 = a7 == null ? null : null; 086 a8 = a8 == null ? null : null; 087 a9 = a9 == null ? null : null; 088 do { 089 tmpWasFree = tmpIsFree; 090 runtime.runFinalization(); 091 runtime.gc(); 092 Thread.sleep(10); 093 tmpIsFree = runtime.freeMemory(); 094 } while (tmpIsFree > tmpWasFree); 095 runtime.runFinalization(); 096 } catch (Exception e) { 097 e.printStackTrace(); 098 } 099 } 100 }