[SNMP4J] Memory leak

Frank Fock fock at agentpp.com
Mon Jan 9 20:13:04 CET 2006


Hi Rejean,

You have to call Snmp.close() to free your resources.
It is probably more efficient to use a single Snmp instance for
several servlet instances in order to avoid thread and port
creation (binding).

Best regards,
Frank

Rejean Chiragh wrote:

>Hi to all,
>
>I am using snmp4j and am having problems, there seems to be a memory leak
>tomcat crashed after a fiew refresh of the jsp page, everytime I call the
>page, It consumes 3 meg more of RAM in the tomcat process.
>
>Here is my code:
>
>
>package cop;
>
>import java.io.IOException;
>import java.util.Vector;
>
>import javax.servlet.jsp.JspWriter;
>
>import org.apache.log4j.Level;
>import org.apache.log4j.Logger;
>import org.snmp4j.CommunityTarget;
>import org.snmp4j.PDU;
>import org.snmp4j.Snmp;
>import org.snmp4j.TransportMapping;
>import org.snmp4j.event.ResponseEvent ;
>import org.snmp4j.mp.SnmpConstants;
>import org.snmp4j.smi.Address;
>import org.snmp4j.smi.GenericAddress;
>import org.snmp4j.smi.OID;
>import org.snmp4j.smi.OctetString;
>import org.snmp4j.smi.VariableBinding ;
>import org.snmp4j.transport.DefaultUdpTransportMapping;
>
>
>public class Get
>{
> public Get()
>    {
>        Logger.getRootLogger().setLevel(Level.OFF);
>    }
>    public void go(String serveur, final JspWriter out) throws Exception
>    {
>     OctetString community = new OctetString("public");
>     Address targetAddress = GenericAddress.parse("udp:"+serveur+"/161");
>     TransportMapping transport = new DefaultUdpTransportMapping();
>        transport.listen();
>        // setting up target
>        CommunityTarget target = new CommunityTarget();
>        target.setCommunity(community);
>        target.setVersion(SnmpConstants.version2c);
>        target.setAddress(targetAddress);
>        target.setRetries (1);
>        target.setTimeout(1500);
>        // creating PDU
>        PDU requestPDU = new PDU();
>        PDU response;
>
>     //SNMPv2-MIB sysDescr 1.3.6.1.2.1.1.1.0
>        requestPDU.add(new VariableBinding(new OID(new int[]
>{1,3,6,1,2,1,1,5})));
>        requestPDU.add(new VariableBinding(new OID(new int[]
>{1,3,6,1,2,1,1,1})));
>        requestPDU.add(new VariableBinding(new OID(new int[]
>{1,3,6,1,4,1,232,11,2,2,1})));
>        requestPDU.add(new VariableBinding(new OID(new int[]
>{1,3,6,1,4,1,232,2,2,2,1})));
>        requestPDU.add(new VariableBinding(new OID(new int[]
>{1,3,6,1,4,1,232,2,2,4,2})));
>        requestPDU.add(new VariableBinding(new OID(new int[]
>{1,3,6,1,2,1,4,20,1,1})));
>        requestPDU.add(new VariableBinding(new OID(new int[]
>{1,3,6,1,2,1,1,3})));
>        //requestPDU.add(new VariableBinding(new OID(mibObject)));
>        requestPDU.setType(PDU.GETNEXT);
>        Snmp snmp = new Snmp(transport);
>        // sending request
>        ResponseEvent event = snmp.send(requestPDU, target);
>        response = event.getResponse();
>        if (response == null){
>         System.out.println("Erreur lors de la requete SNMP pour le serveur
>"+serveur+".");
>         return;
>        }
>        printVarBinds(response.getVariableBindings(), out);
>        ((Snmp)event.getSource()).cancel(event.getRequest(),null);
>        System.gc();
>    }
>
>    private void printVarBinds(Vector varbinds, JspWriter out)
>    {
>     try {
>   out.println("<tr>");
>  } catch (IOException e1) {
>   // TODO Auto-generated catch block
>   e1.printStackTrace();
>  }
>
>        for(int i=0; i < varbinds.size (); i++)
>        {
>         try {
>    //out.println("\tvarbind[" + i + "] = " + varbinds.get(i));
>          String OID = varbinds.get(i).toString().split("=")[0];
>          String resultat = varbinds.get(i).toString().split("=")[1];
>
>       out.println("<td bgcolor=EDEFF1 title='"+resultat+"'>"+(
>resultat.length()>30 ? resultat.substring(0,30): resultat)+"</td>");
>       //out.println("<tr><td>" + varbinds.get(i)+"</td></tr>");
>
>   } catch (IOException e) {
>    // TODO Auto-generated catch block
>    e.printStackTrace ();
>   }
>  }
>        try {
>   out.println("</tr>");
>  } catch (IOException e) {
>   // TODO Auto-generated catch block
>   e.printStackTrace();
>  }
> }
>}
>
>
>
>
>
>Here is my jsp page :
>
><%@ page import="cop.*" %>
><%@ page import="java.net.*"%>
>
><jsp:useBean id="Get" scope="page" class="cop.Get" />
><html>
><title>Liste des serveurs POP</title>
><head>
><style type="text/css"><!--
>FONT {color:white;
>      background-color:003366}
>--></style>
><link href="./css/sortabletable.css" rel="stylesheet" type="text/css" />
><script language="javascript" src="./js/sortabletable.js"></script>
></head>
><body>
><table>
><tr><td colspan=6 bgcolor=003366><font color=FFFFFF size=5>Liste des
>serveurs COP</font></td></tr>
><tr>
><td>
><table border=1 id=serveurCOP class="sort-table">
><thead>
><tr bgcolor=003366><td bgcolor=67A5D8 width='140'>Nom du serveur</td><td
>bgcolor=67A5D8 width='180'>Description</td><td bgcolor=67A5D8
>width='160'>OS</td><td bgcolor=67A5D8 width='80'>No série</td><td
>bgcolor=67A5D8 width='170'>Modèle</td><td bgcolor=67A5D8 width='80'>Adresse
>IP</td><td bgcolor=67A5D8 width='120'>Uptime</td></tr>
></thead>
><tbody>
><%
>        Get snmpget = new Get();
>  String[] serveurCOP =
>{"copctx0","copctx1","copctx2","copctx3","copctx4","copctx5","copsmp0","copsmp1","copcom0","copcom1","copcom2","copcom3","copmod","copfic0","copfic1","copdom0","copdom1","copemp0","copemp1","copemp2","copemp3","coppds","copweb0","copweb1","cop0poc","cop1poc","copscm0","copora0","copora1","copora2","copora3","copbck0","copbck1"};
>
> /*
>  String copctx0 = "10.1.103.90";
>  int[] name = {1,3,6,1,2,1,1,5,0};
>  int[] desc = {1,3,6,1,2,1,1,1,0};
>  int[] uptime = {1,3,6,1,2,1,1,3,0};
>  int[] serialNumber = {1,3,6,1,4,1,232,2,2,2,1,0};
>  int[] model = {1,3,6,1,4,1,232,2,2,4,2,0};
>  int[] adresseIp = {1,3,6,1,2,1,4,20,1,1,0};
>  */
>  for (int i=0;i<serveurCOP.length;i++)
>  {
>   snmpget.go(serveurCOP[i],out);
>  }
>%>
></tbody>
></table>
></td>
></tr>
><tr><td colspan=6 bgcolor=003366><font color=FFFFFF size=5>Liste des
>serveurs PQDH</font></td></tr>
><tr>
><td>
><table border=1 id=serveurPQDH class="sort-table">
><thead>
><tr bgcolor=003366><td bgcolor=67A5D8 width='140'>Nom du serveur</td><td
>bgcolor=67A5D8 width='180'>Description</td><td bgcolor=67A5D8
>width='160'>OS</td><td bgcolor=67A5D8 width='80'>No série</td><td
>bgcolor=67A5D8 width='170'>Modèle</td><td bgcolor=67A5D8 width='80'>Adresse
>IP</td><td bgcolor=67A5D8 width='120'>Uptime</td></tr>
></thead>
><tbody>
><%
>  String[] serveurPQDH =
>{"spdaspan","spdaspandex","serveurpdaprod","serveurpdaprepr","serveurpdadev"};
> /*
>  String copctx0 = " 10.1.103.90";
>  int[] name = {1,3,6,1,2,1,1,5,0};
>  int[] desc = {1,3,6,1,2,1,1,1,0};
>  int[] uptime = {1,3,6,1,2,1,1,3,0};
>  int[] serialNumber = {1,3,6,1,4,1,232,2,2,2,1,0};
>  int[] model = {1,3,6,1,4,1,232,2,2,4,2,0};
>  int[] adresseIp = {1,3,6,1,2,1,4,20,1,1,0};
>  */
>  for (int i=0;i<serveurPQDH.length;i++)
>  {
>   snmpget.go(serveurPQDH[i],out);
>  }
>%>
></tbody>
></table>
></td>
></tr>
><tr><td colspan=6 bgcolor=003366><font color=FFFFFF size=5>Liste des
>serveurs SPA</font></td></tr>
><tr>
><td>
><table border=1 id=serveurSPA class="sort-table">
><thead>
><tr bgcolor=003366><td bgcolor=67A5D8 width='140'>Nom du serveur</td><td
>bgcolor=67A5D8 width='180'>Description</td><td bgcolor=67A5D8
>width='160'>OS</td><td bgcolor=67A5D8 width='80'>No série</td><td
>bgcolor=67A5D8 width='170'>Modèle</td><td bgcolor=67A5D8 width='80'>Adresse
>IP</td><td bgcolor=67A5D8 width='120'>Uptime</td></tr>
></thead>
><tbody>
><%
>  String[] serveurSPA = {"spamod0","spamod1","spamodd"};
> /*
>  String copctx0 = "10.1.103.90";
>  int[] name = {1,3,6,1,2,1,1,5,0};
>  int[] desc = {1,3,6,1,2,1,1,1,0};
>  int[] uptime = {1,3,6,1,2,1,1,3,0};
>  int[] serialNumber = {1,3,6,1,4,1,232,2,2,2,1,0};
>  int[] model = {1,3,6,1,4,1,232,2,2,4,2,0};
>  int[] adresseIp = {1,3,6,1,2,1,4,20,1,1,0};
>  */
>  for (int i=0;i<serveurSPA.length;i++)
>  {
>   snmpget.go(serveurSPA[i],out);
>  }
>%>
></tbody>
></table>
></td>
></tr>
></table>
>Information en provenance du serveur web : <%=InetAddress.getLocalHost
>().getHostName()%>
><script language="javascript">
>var serveurCOP = new SortableTable(document.getElementById ("serveurCOP"),
>["CaseInsensitiveString","CaseInsensitiveString","CaseInsensitiveString","CaseInsensitiveString","CaseInsensitiveString","CaseInsensitiveString"]);
>
>var serveurPQDH = new SortableTable(document.getElementById("serveurPQDH"),
>["CaseInsensitiveString","CaseInsensitiveString","CaseInsensitiveString","CaseInsensitiveString","CaseInsensitiveString","CaseInsensitiveString"]);
>
>var serveurSPA = new SortableTable(document.getElementById("serveurSPA"),
>["CaseInsensitiveString","CaseInsensitiveString","CaseInsensitiveString","CaseInsensitiveString","CaseInsensitiveString","CaseInsensitiveString"]);
>
></script>
>
></body>
>
></html>
>
>
>Thanks!
>_______________________________________________
>SNMP4J mailing list
>SNMP4J at agentpp.org
>http://lists.agentpp.org/mailman/listinfo/snmp4j
>
>
>  
>


-- 
AGENT++
http://www.agentpp.com
http://www.mibexplorer.com
http://www.mibdesigner.com





More information about the SNMP4J mailing list