Passing by value and Garbage Collection

cvcarson
cvcarson used Ask the Experts™
on
I'm using J++ for a client program and I'm using the JVM from Sun on the Server program and both programs just chew up memory.

I think it's because I'm passing certain objects in an out of other object methods and so the objects never really have there references released.

My questions are:
1) How do I pass an object by Value to a method or function (that way I can just set it to null then call System.gc() in the finally block)

2) Is there any other way reclaim memory in Java. Or is there something else that I should be doing.

The client programs runs at about 20MB that's where the program starts and if it only runs from about 8 hours it's fine it'll stay there. If it runs any longer than that the memory goes up.

The server program starts at 19MB but it climbs up to 78MB + at times.

Would appreciate any assistance that you can give me.

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Here is a piece of the code:

/*
      Author: Alfonzo R Carson Sr.
      ClassName: HTML
      Date: December 6, 2002
      Purpose: The only class that writes the html to the filesystem

*/

package resources;
import java.util.*;
import com.ms.wfc.io.*;
import java.io.IOException;
import com.ms.wfc.ui.*;



public class HTML
{
      public static boolean isWorking = false;
      public Config config;
      
      public HTML(Config config){
            this.config = config;
            boolean status = this.config.load();
      }
      
      public boolean doHTML(Vector vhotlist, HTMLControl htmlMM){
            TextWriter outText;
            this.isWorking = true;
            try{
                            
                  File htmlFile = new File(config.appPath + "\\data\\mm.html", FileMode.CREATE, FileAccess.WRITE);
                  outText = new TextWriter(htmlFile);
                  outText.setAutoFlush(true);
                              
                  outText.writeLine("<HTML>");
            
                  outText.writeLine("<HEAD>");
                  outText.writeLine("<LINK href=\"style.css\" rel=\"stylesheet\" type=\"text/css\">");
                  outText.writeLine("</HEAD>");
                  outText.writeLine("<BODY>");
            
                  outText.writeLine("<table width=100% border=0 cellpadding=0 cellspacing=0>");
                  
                  for(int i = 0; i < vhotlist.size(); i++){
                                    
                        //stsInfo.setText(" Processing record " + i );
                        HotEntry theEntry = (HotEntry) vhotlist.elementAt(i);
                                    
                        for(int z = 0 ; z < config.vAuthors.size(); z++){
                  
                              Authors theAuthor = (Authors)config.vAuthors.elementAt(z);
                              if(theAuthor.AuthorName.equalsIgnoreCase(theEntry.Author) && theAuthor.isActive == 1){
                                                
                                    outText.writeLine("<tr>");
                                    outText.writeLine("<td width=8 class=normal>&nbsp;</td>");
                                    outText.writeLine("<td valign=top class=normal><B>" + doAuthor(theEntry.Author) + "</b></td>");
                                    outText.writeLine("<td width=10 class=normal> &nbsp; </td>");
                                    outText.writeLine("<td class=normal><font class=datetime>" + theEntry.DateID + ",&nbsp;&nbsp;" + theEntry.TimeID + "</font><br>" + theEntry.EntryText + "</td>");
                                    outText.writeLine("<td width=10 class=normal>&nbsp;</td>");
                                    outText.writeLine("</tr>");
                  
                                    outText.writeLine("<tr>");
                                    outText.writeLine("<td width=8 class=normal>&nbsp;</td>");
                                    outText.writeLine("<td valign=top class=normal></b></td>");
                                    outText.writeLine("<td width=10 class=normal>&nbsp;</td>");
                                    outText.writeLine("<td class=normal></td>");
                                    outText.writeLine("<td width=10 class=normal>&nbsp;</td>");
                                    outText.writeLine("</tr>");
                                                
                                                
                              }
                  
                        }
                        
                        theEntry = null;
                                    
                  }      
                                    
                        
                  outText.writeLine("</table>");
                  outText.writeLine("</BODY>");
                  outText.writeLine("</HTML>");
            
                  htmlFile.close();
                  outText.close();
                  htmlMM.setURL(config.appPath + "\\data\\mm.html");
                  htmlMM.show();
                        
                  
                this.isWorking = false;    
        }catch(SecurityException e){
                  
                this.isWorking = false;  
        }catch(Throwable e) {
           
                this.isWorking = false;                            
            }finally{
                  
                  vhotlist.removeAllElements();
                  vhotlist = null;
                  this.isWorking = false;
                  System.gc();
            }
            
            return this.isWorking;
            
      }
      
      private String doAuthor(String Author){
            
            String authorTag = "";
            int knownAuthor = 0;
            
            if(Author.compareTo("Jeff Bailey") == 0){
                  
                  knownAuthor = 1;
                        
            }
            
            if(Author.compareTo("Jim Brown") == 0){
                  
                        knownAuthor = 2;
            }
            
            if(Author.compareTo("Jonathan Levinson") == 0){
                  
                  knownAuthor = 3;
                        
            }
            
            if(Author.compareTo("John Seckinger") == 0){
                  
                  knownAuthor = 4;
                        
            }
            
            if(Author.compareTo("Kent Barton") == 0){
                  
                        knownAuthor = 5;
                        
            }
            
            if(Author.compareTo("Linda Piazza") == 0){
                  
                        knownAuthor = 6;
                        
            }
            
            if(Author.compareTo("Mark Phillips") == 0){
                  
                  knownAuthor = 7;
                        
            }
            
            if(Author.compareTo("Mike Parnos") == 0){
                  
                  knownAuthor = 8;
                        
            }
            
            if(Author.compareTo("Steven Price") == 0){
                  
                        knownAuthor = 9;
                        
            }
            
            if(Author.compareTo("OI Technical Staff") == 0){
                  
                        knownAuthor = 10;
                        
            }
            
            switch(knownAuthor){
            
            case 1:
                  authorTag = "<b><font color=blue>Jeff Bailey </font></b>";
                  break;
            case 2:
                  authorTag = "<b><font color=black>Jim Brown</font></b>";
                  break;
            case 3:
                  authorTag = "<font color=#990000>Jonathan Levinson</font>";
                  break;
            case 4:
                  authorTag = "<font color=green>John Seckinger</font>";
                  break;
            case 5:
                  authorTag = "<b><font color=\"purple\">Kent Barton</font></b>";
                  break;
            case 6:
                  authorTag = "<b><font color=black>Linda Piazza</font></b>";                  
                  break;
            case 7:
                  authorTag = "<b><font color=#996633>Mark Phillips</font></b>";                  
                  break;
            case 8:
                  authorTag = "<b><font color=\"purple\">Mike Parnos</font></b>";
                  break;
            case 9:
                  authorTag = "<font color=#3366CC><b>Steven Price</b></font>";
                  break;
            case 10:
                  authorTag = "<b><font color=\"red\">OI Technical Staff</font></b>";
                  break;
            default:
                  authorTag = "<b><font color=black>" + Author + "</font></b>";

                  
            }
            
            return authorTag;
      }
      
      
}
Commented:
While I don't see anything in your code that will explicitly make memory usage spiral out of control, I am concerned about the HotEntry class and the Vector that holds them. Is this a large class? How many instances are created? Is the vector added to over time? Are you holding the whole documents in memory when maybe you should be holding references to their path on the server?

The first step in solving your problem is to determine where the memory is being allocated. My guess is your holding Objects in Collections that you really don't need.

#1: Pass by value creates more Objects, not less. In Java, object variables are references, however method arguments are always passed by value. Regardless, I don't think this is the cause of your problem.

#2: Explicitly setting objects to null and calling System.gc() is about all there is. You can use incremental garbage collection with a command line switch when you start the VM: -Xincgc. However, I don't think that's your answer either.

Author

Commented:
Here is the HotEntry Class:

package resources;
public class HotEntry
{
      public String Author;
      public String DateID;
      public String TimeID;
      public String EntryText;
      
      public HotEntry(String Author, String DateID, String TimeID, String EntryText){
            this.Author = Author;
            this.DateID = DateID;
            this.TimeID = TimeID;
            this.EntryText = EntryText;
      }
}


and here is the class that adds the hotentrys and adds them to the vector:

/*
      Author: Alfonzo R Carson Sr.
      ClassName: Query
      Date: December 6, 2002
      Purpose: Checks the server for updates to the database. Gets the MM Entries from the client.

*/

package resources;
import java.io.*;
import java.net.*;
import java.util.*;

public class Query
{
      public static String UpdateID = "0";
      
      public Query(){
      }
            
      public boolean doQuery(Connection conn){
            java.io.BufferedReader dis;
            java.io.BufferedWriter dos;
            Socket socket;
            boolean newFlag = false;
            String buffer;
            
            
            try{
                  
                  socket = new Socket("64.78.234.49",5051);                  
                  dis = new java.io.BufferedReader(new java.io.InputStreamReader(socket.getInputStream()));                  
                  dos = new java.io.BufferedWriter(new java.io.OutputStreamWriter(socket.getOutputStream()));
                  
                  dos.write("983\n");
                  dos.flush();
                  buffer = dis.readLine();
                  
                  dos.write(conn.username + "\n");
                  dos.flush();
                  
                  buffer = dis.readLine();
                  
                  dis.close();
                  dos.close();
                  socket.close();
                  
                  if(buffer.equalsIgnoreCase("DISCONNECTED")){
                        conn.isConnected = false;
                        newFlag = false;
                  }else{
                        
                        if(!buffer.equalsIgnoreCase(this.UpdateID)){
                              this.UpdateID = buffer;
                              conn.isConnected = true;
                              newFlag = true;
                        }
                  }
                  
            }catch(UnknownHostException ex) {
                        conn.isConnected = true;
                        newFlag = false;
            }catch(IOException ex) {
                        conn.isConnected = true;
                        newFlag = false;
            }finally{
                  dos = null;
                  dis = null;
                  socket = null;
                  
                  System.gc();
            }
            
                  
            return newFlag;
      }
      
      public Vector getHotlist(){
            java.io.BufferedReader dis;
            java.io.BufferedWriter dos;
            Socket socket;
            Vector vhotlist = new Vector();
            
                  try{
                  
                  
                        socket = new Socket("64.78.234.49",5051);                  
                        dis = new java.io.BufferedReader(new java.io.InputStreamReader(socket.getInputStream()));                  
                        dos = new java.io.BufferedWriter(new java.io.OutputStreamWriter(socket.getOutputStream()));
                  
                        dos.write("984\n");
                        dos.flush();
                        
                        
                        String author = "";
                        String dateid = "";
                        String timeid = "";
                        String entrytext = "";
                        String strtemp = "";
                        
                        while(true){
                              
                              author = "";
                              dateid = "";
                              timeid = "";
                              entrytext = "";
                                          
                              
                              String svrMsg = dis.readLine();
                              
                              if (svrMsg.compareTo("NewRecord") == 0){
                                    
                                    
                                    author = dis.readLine();
                                    dateid = dis.readLine();
                                    timeid = dis.readLine();
                                    entrytext = dis.readLine();
                                    
                                    HotEntry htentry = new HotEntry(author, dateid,timeid, entrytext);
                                    vhotlist.addElement(htentry);
                                    
                              }
                              
                              if(svrMsg.compareTo("EOF")== 0){
                                    break;
                                    
                              }
                        }
                        
                  
                  
                  }catch(UnknownHostException ex) {
                        
                  }catch(IOException ex) {
                                
                  }finally{
                        dos = null;
                        dis = null;
                        socket = null;
                  
                        System.gc();
                  }
            
            
            return vhotlist;
      }
      
      
      
}
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Commented:
Give a quick overview of what your app does, Im having trouble following it.

Is there a huge amount of data being sent? Are the streams being flushed and closed properly? Does all of the information come from a single client or is information constantly being added by different clients? Is there a huge amount of data being sent? What happens to the old data when new data arrives?

Have you tried using a profiler to determine what type of objects are using the most memory?

I doubt Ill be able see the problem myself, these memory issues can be difficult without being able to run it.
Mick BarryJava Developer
Top Expert 2010

Commented:
I doubt System.gc() will help you, and it's not guaranteed to do anything anyway. I'd say the problem is with your code eating up memory, not the garbage collector not running often enough.

Do some profiling.

Commented:
Just curious...

How did you solve it? Profiling?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial