1package org.ahmadsoft.stream.loader;
2
3import java.sql.Connection;
4import java.sql.DriverManager;
5import java.sql.ResultSet;
6import java.sql.Statement;
7
8public class CLTester {
9
10	/**
11	 * @param args
12	 */
13	public static void main(String[] args) throws Exception {
14		runGC();
15		long l1 = usedMemory();
16		System.out.println("mem=" + (l1));
17
18		Class.forName("com.mysql.jdbc.Driver");
19		Connection connection = DriverManager
20				.getConnection("jdbc:mysql://localhost/mysql?user=root&password=foo&defaultFetchSize=10&useCursorFetch=true");
21		Statement stmt = connection.createStatement();
22		stmt.setFetchDirection(ResultSet.FETCH_FORWARD);
23		runGC();
24		long l2 = usedMemory();
25		System.out.println("mem=" + (l2));
26		ResultSet rs = stmt.executeQuery("SELECT * FROM PRESIDENT");
27		runGC();
28		long l3 = usedMemory();
29		System.out.println("mem=" + (l3));
30		while (rs.next()) {
31			//runGC();
32			long l4 = usedMemory();
33			System.out.println("  mem=" + (l4));
34		}
35		connection.close();
36	}
37    private static void runGC () throws Exception
38    {
39        // It helps to call Runtime.gc()
40        // using several method calls:
41        for (int r = 0; r < 4; ++ r) _runGC ();
42    }
43    private static void _runGC () throws Exception
44    {
45        long usedMem1 = usedMemory (), usedMem2 = Long.MAX_VALUE;
46        for (int i = 0; (usedMem1 < usedMem2) && (i < 500); ++ i)
47        {
48            s_runtime.runFinalization ();
49            s_runtime.gc ();
50            Thread.currentThread ().yield ();
51
52            usedMem2 = usedMem1;
53            usedMem1 = usedMemory ();
54        }
55    }
56    private static long usedMemory ()
57    {
58        return s_runtime.totalMemory () - s_runtime.freeMemory ();
59    }
60
61    private static final Runtime s_runtime = Runtime.getRuntime ();
62}
63