Hi,<div> I wrote this sample code and the resident memory of the process is increasing gradually over time. What could be the reason ? I don't see any obvious leaks in my program. Could it be that the API is not freeing/deleting memory ?<br>
<br></div><div>Regards,</div><div>Navin</div><div><br></div><div><br></div><div><br></div><div><div>package ovcollector;</div><div><br></div><div><br></div><div><br></div><div>import java.util.*;</div><div>import java.io.*;</div>
<div>import java.lang.management.ManagementFactory;</div><div><br></div><div>import org.apache.http.client.*;</div><div>import org.apache.http.params.*;</div><div>import org.ovirt.engine.sdk.Api;</div><div>import org.apache.http.pool.*;</div>
<div>import org.ovirt.engine.sdk.decorators.Host;</div><div>import org.ovirt.engine.sdk.decorators.HostStatistic;</div><div>import org.ovirt.engine.sdk.decorators.StorageDomain;</div><div>import org.ovirt.engine.sdk.decorators.VM;</div>
<div>import org.ovirt.engine.sdk.decorators.VMDisk;</div><div>import org.ovirt.engine.sdk.decorators.VMDiskStatistic;</div><div>import org.ovirt.engine.sdk.decorators.VMStatistic;</div><div>import org.ovirt.engine.sdk.decorators.VMs;</div>
<div>import org.ovirt.engine.sdk.entities.CPU;</div><div>import org.ovirt.engine.sdk.entities.Value;</div><div>import org.ovirt.engine.sdk.exceptions.ServerException;</div><div>import org.ovirt.engine.sdk.exceptions.UnsecuredConnectionAttemptError;</div>
<div>import org.ovirt.engine.*;</div><div><br></div><div>import javax.swing.JFrame;</div><div>import javax.swing.JPanel;</div><div>import javax.swing.JScrollPane;</div><div>import javax.swing.JTable;</div><div>import java.awt.Dimension;</div>
<div>import java.awt.GridLayout;</div><div>import java.awt.event.MouseAdapter;</div><div>import java.awt.event.MouseEvent;</div><div><br></div><div><br></div><div>public class collectHosts extends JPanel {</div><div><br></div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>/**</div><div><span class="Apple-tab-span" style="white-space:pre">                </span> * @param args</div><div><span class="Apple-tab-span" style="white-space:pre">                </span> */</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span></div><div><span class="Apple-tab-span" style="white-space:pre">                </span></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>public static String[] columnNames = {"Host Name",</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span> "Host uuid",</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> "Cpu Cores", "Cpu Sockets",</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span> "Memory.total",</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> "Memory.used",</div><div>
<span class="Apple-tab-span" style="white-space:pre">        </span> "Memory.free",</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> "Memory.shared","Memory.buffers","Memory.cached","swap.total","swap.free","swap.used","swap.cached","ksm.cpu.current",</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span> "cpu.current.user","cpu.current.system","cpu.current.idle","cpu.load.avg.5m"</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> };</div>
<div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>public static Object[][] data = </div><div><span class="Apple-tab-span" style="white-space:pre">        </span>{</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>{"blankvmname", "blankuuid", new Double(0),new Double(0),new Double(0),new Double(0),new Double(0),new Double(0),new Double(0), new Double(0),</div>
<div><span class="Apple-tab-span" style="white-space:pre">                        </span>new Double(0),new Double(0),new Double(0),new Double(0),new Double(0),new Double(0),new Double(0),new Double(0),new Double(0)},</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>{"blankvmname", "blankuuid", new Double(0),new Double(0),new Double(0),new Double(0),new Double(0),new Double(0),new Double(0), new Double(0),</div>
<div><span class="Apple-tab-span" style="white-space:pre">                                </span>new Double(0),new Double(0),new Double(0),new Double(0),new Double(0),new Double(0),new Double(0),new Double(0),new Double(0)}<span class="Apple-tab-span" style="white-space:pre">                        </span></div>
<div><span class="Apple-tab-span" style="white-space:pre">                                                </span></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>};</div><div><span class="Apple-tab-span" style="white-space:pre">                </span></div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span></div><div><span class="Apple-tab-span" style="white-space:pre">                </span></div><div><span class="Apple-tab-span" style="white-space:pre">                </span></div><div>
<span class="Apple-tab-span" style="white-space:pre">        </span>public collectHosts() {</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> super(new GridLayout(1,0));</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> </div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span> </div><div><span class="Apple-tab-span" style="white-space:pre">        </span> </div><div><span class="Apple-tab-span" style="white-space:pre">        </span> final JTable table = new JTable(data, columnNames);</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span> table.setPreferredScrollableViewportSize(new Dimension(500, 70));</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> table.setFillsViewportHeight(true);</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span> </div><div><span class="Apple-tab-span" style="white-space:pre">        </span> </div><div><span class="Apple-tab-span" style="white-space:pre">        </span> //Create the scroll pane and add the table to it.</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span> JScrollPane scrollPane = new JScrollPane(table);</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> </div><div><span class="Apple-tab-span" style="white-space:pre">        </span> //Add the scroll pane to this panel.</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span> add(scrollPane);</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> }</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> </div>
<div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                        </span></div><div><span class="Apple-tab-span" style="white-space:pre">                </span> /**</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> * Create the GUI and show it. For thread safety,</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span> * this method should be invoked from the</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> * event-dispatching thread.</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span> */</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> private static void createAndShowGUI() {</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> //Create and set up the window.</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span> JFrame frame = new JFrame("Hosts");</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span> </div><div><span class="Apple-tab-span" style="white-space:pre">        </span> //Create and set up the content pane.</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> collectHosts newContentPane = new collectHosts();</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span> newContentPane.setOpaque(true); //content panes must be opaque</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> frame.setContentPane(newContentPane);</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span> </div><div><span class="Apple-tab-span" style="white-space:pre">        </span> //Display the window.</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> frame.pack();</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span> frame.setVisible(true);</div><div><span class="Apple-tab-span" style="white-space:pre">        </span> }</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span></div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>private static final String URL = "<a href="http://somehost:80/api">http://somehost:80/api</a>";</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>public static void main(String[] args) throws ClientProtocolException,ServerException, UnsecuredConnectionAttemptError,IOException {</div>
<div><span class="Apple-tab-span" style="white-space:pre">                        </span>// TODO Auto-generated method stub</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>System.out.println("Hosts");</div><div>
<br></div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>while (true)</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>{</div><div><span class="Apple-tab-span" style="white-space:pre">                                </span>Api api = new Api(URL,"SOMEUSERNAME","SOMEPASSWORD");</div>
<div><span class="Apple-tab-span" style="white-space:pre">                                </span>System.out.println(ManagementFactory.getRuntimeMXBean().getName()) ;</div><div><span class="Apple-tab-span" style="white-space:pre">                                </span></div><div>
<span class="Apple-tab-span" style="white-space:pre">                                </span>List<Host> hlist = api.getHosts().list();</div><div><span class="Apple-tab-span" style="white-space:pre">                                </span>Integer i=0,j=0;</div><div><span class="Apple-tab-span" style="white-space:pre">                                </span>for(Host obj: hlist)</div>
<div><span class="Apple-tab-span" style="white-space:pre">                                </span>{</div><div><span class="Apple-tab-span" style="white-space:pre">                                        </span>j=0;</div><div><span class="Apple-tab-span" style="white-space:pre">                                        </span>System.out.println(obj.getName()+ ":" + obj.getId() +":"+ obj.getMemory()+":" + obj.getCpu().getTopology().getCores() +":"+</div>
<div><span class="Apple-tab-span" style="white-space:pre">                                                                        </span>obj.getCpu().getTopology().getSockets());</div><div><span class="Apple-tab-span" style="white-space:pre">                                </span></div><div><span class="Apple-tab-span" style="white-space:pre">                                        </span>data[i][j++]=obj.getName();</div>
<div><span class="Apple-tab-span" style="white-space:pre">                                        </span>data[i][j++]=obj.getId();</div><div><span class="Apple-tab-span" style="white-space:pre">                                        </span>data[i][j++]=obj.getCpu().getTopology().getCores();</div>
<div><span class="Apple-tab-span" style="white-space:pre">                                        </span>data[i][j++]=obj.getCpu().getTopology().getSockets();</div><div><span class="Apple-tab-span" style="white-space:pre">                                </span></div><div><span class="Apple-tab-span" style="white-space:pre">                                        </span>List<HostStatistic> lhs=obj.getStatistics().list();</div>
<div><span class="Apple-tab-span" style="white-space:pre">                                        </span>for(HostStatistic obj1: lhs)</div><div><span class="Apple-tab-span" style="white-space:pre">                                        </span>{</div><div><span class="Apple-tab-span" style="white-space:pre">                                                </span>System.out.println(obj1.getName());</div>
<div><span class="Apple-tab-span" style="white-space:pre">                                                </span>List<Value> vll=obj1.getValues().getValues();</div><div><span class="Apple-tab-span" style="white-space:pre">                                                </span>for(Value vl:vll)</div>
<div><span class="Apple-tab-span" style="white-space:pre">                                                </span>{</div><div><span class="Apple-tab-span" style="white-space:pre">                                                        </span>System.out.println(vl.getDatum());</div><div><span class="Apple-tab-span" style="white-space:pre">                                                        </span>data[i][j++]=vl.getDatum();</div>
<div><span class="Apple-tab-span" style="white-space:pre">                                                </span>}</div><div><span class="Apple-tab-span" style="white-space:pre">                                        </span>}</div><div><span class="Apple-tab-span" style="white-space:pre">                                        </span>i++;</div>
<div><span class="Apple-tab-span" style="white-space:pre">                                </span>}</div><div><span class="Apple-tab-span" style="white-space:pre">                                </span></div><div><span class="Apple-tab-span" style="white-space:pre">                                </span>try {</div>
<div><span class="Apple-tab-span" style="white-space:pre">                                        </span>Thread.sleep(10000);</div><div><span class="Apple-tab-span" style="white-space:pre">                                </span>} catch (InterruptedException e) {</div><div><span class="Apple-tab-span" style="white-space:pre">                                        </span>// TODO Auto-generated catch block</div>
<div><span class="Apple-tab-span" style="white-space:pre">                                        </span>e.printStackTrace();</div><div><span class="Apple-tab-span" style="white-space:pre">                                </span>}</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>}</div>
<div><span class="Apple-tab-span" style="white-space:pre">                        </span></div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>/*</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>javax.swing.SwingUtilities.invokeLater(new Runnable() {</div>
<div><span class="Apple-tab-span" style="white-space:pre">                                </span>public void run() {</div><div><span class="Apple-tab-span" style="white-space:pre">                </span> createAndShowGUI();</div><div><span class="Apple-tab-span" style="white-space:pre">                </span> }</div>
<div><span class="Apple-tab-span" style="white-space:pre">                        </span>});</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span></div><div><span class="Apple-tab-span" style="white-space:pre">                        </span></div>
<div><span class="Apple-tab-span" style="white-space:pre">                        </span>List<StorageDomain> list_SD=api.getStorageDomains().list();</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>Integer i=0;</div>
<div><span class="Apple-tab-span" style="white-space:pre">                        </span>for(StorageDomain obj: list_SD)</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>{</div><div><span class="Apple-tab-span" style="white-space:pre">                                </span>System.out.println(obj.getName()+ " | " + obj.getUsed() + " | " + obj.getAvailable() + " | " + obj.getType());<span class="Apple-tab-span" style="white-space:pre">                </span></div>
<div><span class="Apple-tab-span" style="white-space:pre">                        </span>}</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>*/</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span></div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>} <span class="Apple-tab-span" style="white-space:pre">        </span></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div><br></div>
</div>