public static void compactMemory ( ) { try { final byte [ ] [ ] unused = new byte [ 128 ] [ ] ; for ( int i = unused . length ; i -- != 0 ; ) { unused [ i ] = new byte [ 2000000000 ] ; } } catch ( OutOfMemoryError ignore ) { } System . gc ( ) ; }
Compacts memory as much as possible by allocating huge memory block and then forcing garbage collection .