Skip to content

Instantly share code, notes, and snippets.

@micmich
Created June 3, 2019 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micmich/ef8597914743ecef3402269e82ded830 to your computer and use it in GitHub Desktop.
Save micmich/ef8597914743ecef3402269e82ded830 to your computer and use it in GitHub Desktop.
package pgamemoryleak;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import oracle.jdbc.pool.OracleDataSource;
public class Main {
public static void main(String[] args) throws IOException, SQLException, InterruptedException {
final OracleDataSource oracleDataSource = readConfig();
try (Connection connection = oracleDataSource.getConnection()) {
while (true) {
final ResultSet resultSetMyPGA;
try (Statement statement = connection.createStatement();
ResultSet resultSetTopSQL = statement.executeQuery(QUERY_TOP_SQL)) {
// ignore the result - calling the query is enough to cause memory leak
}
try (Statement statement = connection.createStatement()) {
resultSetMyPGA = statement.executeQuery(QUERY_MY_PGA);
resultSetMyPGA.next();
final int pgaUsage = resultSetMyPGA.getInt(2);
System.out.println("PGA usage: " + pgaUsage);
}
Thread.sleep(1000);
}
}
}
private static final String QUERY_TOP_SQL =
"select a1.sql_id,"
+ " a1.elapsed_time,"
+ " a1.cpu_time,"
+ " a1.sql_fulltext sql_fulltext,"
+ " a1.disk_reads,"
+ " a1.direct_writes,"
+ " a1.executions,"
+ " to_timestamp(a1.first_load_time, 'yyyy-mm-dd/hh24:mi:ss') first_load_time,"
+ " a1.last_load_time,"
+ " a1.parse_calls,"
+ " a1.buffer_gets,"
+ " a1.rows_processed,"
+ " a1.user_io_wait_time,"
+ " a1.cluster_wait_time,"
+ " a1.concurrency_wait_time,"
+ " a1.application_wait_time,"
+ " a1.parsing_schema_name"
+ " from v$sql a1";
private static final String QUERY_MY_PGA = "select machine, round(pga_used_mem / (1024 * 1024),2) pga_used_mb"
+ " from v$session"
+ " join v$process on v$session.paddr = v$process.addr"
+ " where sid = ("
+ " select sid from v$mystat where rownum=1"
+ " )"
+ " order by machine desc";
private static OracleDataSource readConfig() throws IOException, SQLException {
final Path configFile = Paths.get("config", "config.properties");
try (BufferedReader reader = Files.newBufferedReader(configFile)) {
Properties config = new Properties();
config.load(reader);
String host = config.getProperty("host");
String port = config.getProperty("port");
String service = config.getProperty("service");
String login = config.getProperty("login");
String password = config.getProperty("password");
System.out.println("Config read:"
+ " host:" + host
+ " port:" + port
+ " service:" + service
+ " login:" + login
+ " password:" + password);
final OracleDataSource oracleDataSource = new OracleDataSource();
oracleDataSource.setDriverType("thin");
oracleDataSource.setServerName(config.getProperty("host"));
oracleDataSource.setPortNumber(Integer.parseInt(config.getProperty("port")));
oracleDataSource.setServiceName(config.getProperty("service"));
oracleDataSource.setUser(config.getProperty("login"));
oracleDataSource.setPassword(config.getProperty("password"));
return oracleDataSource;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment