Ever-increasing Handles
Hi there. I'm pretty new to Java (hence this forum) and have created a simple program to monitor a Lotus Notes database and a file directory.
If a new document is found within the Notes database, an agent is called within that database, likewise if a file is found in the directory on the machine's c drive.
This monitor runs 24/7 and I've had issues whereby the memory usuage gets bigger and bigger - I 'think' I have that under control now, but the other thing that's worrying me is the handle count as displayed by the Task Manager (XP).
After running for a few hours the count is into the tens of thousands - that can't be healthy, can it?
1. Is it a problem, because it looks like one?
2. How can I keep them static or thereabouts?
Any guidance would be much appreciated. Thanks.
Kind of hard to help without some code to look at.
jbisha at 2007-7-29 16:08:03 >

That may help....!! :o)
public class CARS_Allocation_Monitor extends Frame {
public static final int kHeight = 340;
public static final int kWidth = 450;
TextArea fTextArea;
ScrollPane fTextAreaPane;
Button fConnectButton;
TextField inputField;
TextField outputField;
public CARS_Allocation_Monitor() {
// set up the overall appearance
GridBagLayout theGridBag = new GridBagLayout();
setLayout(theGridBag);
GridBagConstraints theConstraints = new GridBagConstraints();
theConstraints.fill = GridBagConstraints.BOTH;
setBackground(Color.lightGray);
// place a view of the list on the right
theConstraints.gridwidth = GridBagConstraints.REMAINDER;
theConstraints.weightx = 1.0;
Label inputTitle = new Label("Lenovo/InfoPrint - Submitted for Processing Views",Label.CENTER);
theGridBag.setConstraints(inputTitle, theConstraints);
add(inputTitle);
inputField = new TextField("");
theGridBag.setConstraints(inputField, theConstraints);
inputField.setEditable(false);
add(inputField);
Label outputTitle = new Label("C:\\Robots\\Output Folder",Label.CENTER);
theGridBag.setConstraints(outputTitle, theConstraints);
add(outputTitle);
outputField = new TextField("");
theGridBag.setConstraints(outputField,theConstraints);
outputField.setEditable(false);
add(outputField);
fConnectButton = new Button("Clear Window");
theConstraints.gridwidth = GridBagConstraints.REMAINDER;
theGridBag.setConstraints(fConnectButton, theConstraints);
add(fConnectButton);
// make an empty text area with 4 rows and 30 columns
theConstraints.gridwidth = GridBagConstraints.RELATIVE;
theConstraints.weightx = 1.0;
fTextArea = new TextArea("", 10, 50);
fTextArea.setEditable(false); // read only
theGridBag.setConstraints(fTextArea, theConstraints);
add(fTextArea);
fConnectButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Clear();
}
});
}
public void runCheck() {
try {
Calendar rightNow = Calendar.getInstance();
rightNow.getTime();
while (null==null) {
NotesThread.sinitThread();
Session s = NotesFactory.createSession();
fTextArea.append("Checking Databases...\n");
// Create array of database paths
String [] notesDatabases = { "lenovoca.nsf","infoprca.nsf"};
// Check the views of all databases and process accordingly
for (int dLoop = 0 ; dLoop < notesDatabases.length ; dLoop++ ) {
Database db = s.getDatabase("gblp077d",notesDatabases[dLoop]);
if (db==null) {
fTextArea.append("Unable to open CARS Allocations Database ("+notesDatabases[dLoop]+"\n");
}
else {
fTextArea.append("Opened " + db.getTitle()+ " ("+notesDatabases[dLoop]+")\n");
// First Check output folder
int cOu = checkOutput(db);
// Then check for new requests
int cDb = checkDatabase(db);
db = null;
}
}
fTextArea.append("Sleeping for 10 seconds\n");
s = null;
NotesThread.stermThread();
System.gc();
Thread.sleep(10000);
}
}
catch(Exception e) {
e.printStackTrace();
}
}
public int checkOutput(Database db) {
// Function to check output folder on machine
try {
File path = new File("C:\\robots\\output\\"); // path of output folder
String[] list; // initiate array
String prefix; // File prefix, ie: LEN or IPS
boolean inputFilesFound = false; // If Yes, then the relevant agent is called
list = path.list(); // build array based on files in directory
if (list.length == 0) {
// If no files
outputField.setText(getTime()+" - No files to process");
}
else {
// If files exist, loop through and check if any of the files match the database in question
// If so, call executeOutputAgent
for(int i = 0; i < list.length; i++) {
// Check which filename to look for according to database
if ( db.getTitle().contains("Lenovo")) {
prefix = "LEN";
}
else {
prefix = "IPS";
}
// Check if file is an allocation file then call output agent
if ( list.contains(prefix+"_ALLOCATION")) {
fTextArea.append(list+"\n");
inputFilesFound = true;
}
else {
outputField.setText(getTime()+" - No files to process");
}
}
// If files were found, call agent
if ( inputFilesFound==true ){
int rc = executeOutputAgent(db);
}
}
} catch (Exception e) {
//System.out.println("File error :" +e);
e.printStackTrace();
}
return 0;
}
public int checkDatabase(Database db) {
// Function to check Submitted fot Processing view of database
try {
View view = db.getView("vwSub"); // Get the submitted for processing view
view.refresh(); // Refresh it
ViewEntryCollection vec = view.getAllEntries(); // Get all entries in the view
// If the entry count is 0, output 'no files' message
if (vec.getCount() == 0) {
inputField.setText(getTime()+" - No Requests to process in "+db.getTitle());
}
else {
// If entries are found, call the agent
ViewEntry ve = vec.getFirstEntry();
fTextArea.append(ve.getColumnValues().elementAt(0)+"\n");
int rc = executeInputAgent(db);
}
view = null;
} catch (NotesException ne) {
ne.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
public String getTime() {
//TimeZone.setDefault(TimeZone.getTimeZone("Europe/London")) ;
String[] ids = TimeZone.getAvailableIDs(0);
SimpleTimeZone bst = new SimpleTimeZone(0, ids[0]);
// set day light savings.
bst.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
bst.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
// Format the current time.
SimpleDateFormat formatter = new SimpleDateFormat ("hh:mm:ss");
Calendar calendar = new GregorianCalendar(bst,Locale.UK);
Date trialTime = new Date();
calendar.setTime(trialTime);
String dateString = formatter.format(calendar.getTime());
return dateString;
}
public int executeOutputAgent(Database db) {
try {
// Call ALLOUT agent from relevant database
Agent agent = null;
agent = db.getAgent("ALLOOUT");
outputField.setText("Output found for "+db.getTitle());
agent.run(); // Run the agent
agent = null;
} catch (NotesException ne) {
ne.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
public int executeInputAgent(Database db) {
try {
// Call ALLOIN agent from relevant database
Agent agent = null;
agent = db.getAgent("ALLOIN");
inputField.setText("New request(s) found for "+db.getTitle());
agent.run();
agent = null;
} catch (NotesException ne) {
ne.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
public void Clear() {
fTextArea.setText("");
}
public static void main(String args[]) {
// have a main routine so this class can be run as an application
CARS_Allocation_Monitor theFrame = new CARS_Allocation_Monitor();
theFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
theFrame.setTitle("CARS Allocation Monitor");
theFrame.setSize(kWidth, kHeight);
theFrame.setVisible(true);
theFrame.runCheck();
}
}
Posting that code using code tags would help even more (the code button above the window you paste into).
Also, I am assuming that you are not closing any open streams.
Sorry! As for not closing open streams...I don't think I am.
public class CARS_Allocation_Monitor extends Frame {
public static final int kHeight = 340;
public static final int kWidth = 450;
TextArea fTextArea;
ScrollPane fTextAreaPane;
Button fConnectButton;
TextField inputField;
TextField outputField;
public CARS_Allocation_Monitor() {
// set up the overall appearance
GridBagLayout theGridBag = new GridBagLayout();
setLayout(theGridBag);
GridBagConstraints theConstraints = new GridBagConstraints();
theConstraints.fill = GridBagConstraints.BOTH;
setBackground(Color.lightGray);
// place a view of the list on the right
theConstraints.gridwidth = GridBagConstraints.REMAINDER;
theConstraints.weightx = 1.0;
Label inputTitle = new Label("Lenovo/InfoPrint - Submitted for Processing Views",Label.CENTER);
theGridBag.setConstraints(inputTitle, theConstraints);
add(inputTitle);
inputField = new TextField("");
theGridBag.setConstraints(inputField, theConstraints);
inputField.setEditable(false);
add(inputField);
Label outputTitle = new Label("C:\\Robots\\Output Folder",Label.CENTER);
theGridBag.setConstraints(outputTitle, theConstraints);
add(outputTitle);
outputField = new TextField("");
theGridBag.setConstraints(outputField,theConstraints);
outputField.setEditable(false);
add(outputField);
fConnectButton = new Button("Clear Window");
theConstraints.gridwidth = GridBagConstraints.REMAINDER;
theGridBag.setConstraints(fConnectButton, theConstraints);
add(fConnectButton);
// make an empty text area with 4 rows and 30 columns
theConstraints.gridwidth = GridBagConstraints.RELATIVE;
theConstraints.weightx = 1.0;
fTextArea = new TextArea("", 10, 50);
fTextArea.setEditable(false); // read only
theGridBag.setConstraints(fTextArea, theConstraints);
add(fTextArea);
fConnectButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Clear();
}
});
}
public void runCheck() {
try {
Calendar rightNow = Calendar.getInstance();
rightNow.getTime();
while (null==null) {
NotesThread.sinitThread();
Session s = NotesFactory.createSession();
fTextArea.append("Checking Databases...\n");
// Create array of database paths
String [] notesDatabases = { "lenovoca.nsf","infoprca.nsf"};
// Check the views of all databases and process accordingly
for (int dLoop = 0 ; dLoop < notesDatabases.length ; dLoop++ ) {
Database db = s.getDatabase("gblp077d",notesDatabases[dLoop]);
if (db==null) {
fTextArea.append("Unable to open CARS Allocations Database ("+notesDatabases[dLoop]+"\n");
}
else {
fTextArea.append("Opened " + db.getTitle()+ " ("+notesDatabases[dLoop]+")\n");
// First Check output folder
int cOu = checkOutput(db);
// Then check for new requests
int cDb = checkDatabase(db);
db = null;
}
}
fTextArea.append("Sleeping for 10 seconds\n");
s = null;
NotesThread.stermThread();
System.gc();
Thread.sleep(10000);
}
}
catch(Exception e) {
e.printStackTrace();
}
}
public int checkOutput(Database db) {
// Function to check output folder on machine
try {
File path = new File("C:\\robots\\output\\"); // path of output folder
String[] list; // initiate array
String prefix; // File prefix, ie: LEN or IPS
boolean inputFilesFound = false; // If Yes, then the relevant agent is called
list = path.list(); // build array based on files in directory
if (list.length == 0) {
// If no files
outputField.setText(getTime()+" - No files to process");
}
else {
// If files exist, loop through and check if any of the files match the database in question
// If so, call executeOutputAgent
for(int i = 0; i < list.length; i++) {
// Check which filename to look for according to database
if ( db.getTitle().contains("Lenovo")) {
prefix = "LEN";
}
else {
prefix = "IPS";}
// Check if file is an allocation file then call output agent
if ( list[i].contains(prefix+"_ALLOCATION")) {
fTextArea.append(list[i]+"\n");
inputFilesFound = true;
}
else {
outputField.setText(getTime()+" - No files to process");
}
}
// If files were found, call agent
if ( inputFilesFound==true ){
int rc = executeOutputAgent(db);
}
}
} catch (Exception e) {
//System.out.println("File error :" +e);
e.printStackTrace();
}
return 0;
}
public int checkDatabase(Database db) {
// Function to check Submitted fot Processing view of database
try {
View view = db.getView("vwSub"); // Get the submitted for processing view
view.refresh(); // Refresh it
ViewEntryCollection vec = view.getAllEntries(); // Get all entries in the view
// If the entry count is 0, output 'no files' message
if (vec.getCount() == 0) {
inputField.setText(getTime()+" - No Requests to process in "+db.getTitle());
}
else {
// If entries are found, call the agent
ViewEntry ve = vec.getFirstEntry();
fTextArea.append(ve.getColumnValues().elementAt(0)+"\n");
int rc = executeInputAgent(db);
}
view = null;
} catch (NotesException ne) {
ne.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
public String getTime() {
//TimeZone.setDefault(TimeZone.getTimeZone("Europe/London")) ;
String[] ids = TimeZone.getAvailableIDs(0);
SimpleTimeZone bst = new SimpleTimeZone(0, ids[0]);
// set day light savings.
bst.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
bst.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
// Format the current time.
SimpleDateFormat formatter = new SimpleDateFormat ("hh:mm:ss");
Calendar calendar = new GregorianCalendar(bst,Locale.UK);
Date trialTime = new Date();
calendar.setTime(trialTime);
String dateString = formatter.format(calendar.getTime());
return dateString;
}
public int executeOutputAgent(Database db) {
try {
// Call ALLOUT agent from relevant database
Agent agent = null;
agent = db.getAgent("ALLOOUT");
outputField.setText("Output found for "+db.getTitle());
agent.run(); // Run the agent
agent = null;
} catch (NotesException ne) {
ne.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
public int executeInputAgent(Database db) {
try {
// Call ALLOIN agent from relevant database
Agent agent = null;
agent = db.getAgent("ALLOIN");
inputField.setText("New request(s) found for "+db.getTitle());
agent.run();
agent = null;
} catch (NotesException ne) {
ne.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
public void Clear() {
fTextArea.setText("");
}
public static void main(String args[]) {
// have a main routine so this class can be run as an application
CARS_Allocation_Monitor theFrame = new CARS_Allocation_Monitor();
theFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
theFrame.setTitle("CARS Allocation Monitor");
theFrame.setSize(kWidth, kHeight);
theFrame.setVisible(true);
theFrame.runCheck();
}
}
