Monitoring Java Program Memory usage

Source: Internet
Author: User

=========================================

package Myprojects.jmemorydemo;


import Javax.swing.UIManager;


import java.awt.*;


public class Jmemorydemo {


Private Boolean packframe = false;


public Jmemorydemo () {


MainFrame frame = new MainFrame ();


if (packframe) {


Frame.pack ();


}


else {


frame.validate ();


}


Dimension screensize = Toolkit.getdefaulttoolkit (). Getscreensize ();


Dimension framesize = Frame.getsize ();


if (Framesize.height > Screensize.height) {


framesize.height = screensize.height;


}


if (Framesize.width > Screensize.width) {


framesize.width = screensize.width;


}


Frame.setlocation ((screensize.width-framesize.width)/2, (screensize.height-framesize.height)


/2);


frame.setvisible (TRUE);


}


public static void Main (string[] args)


{


new Jmemorydemo ();


}


}


===================================================


package Myprojects.jmemorydemo;


import javax.swing.*;


public class Progressthread extends Thread {


JProgressBar Pbar;


JLabel label;


public Progressthread (jprogressbar Pbar,jlabel label) {


This.pbar=pbar;


This.label=label;


}


public void Run () {


int min=0;


int max=100;


int free=0;


int totle=0;


int status=0;


pbar.setminimum (min);


Pbar.setmaximum (max);


pbar.setvalue (status);


while (true) {


try{


totle= (int) (Runtime.getruntime (). TotalMemory ()/1024);


free= (int) (Runtime.getruntime (). Freememory ()/1024);


}catch (Exception e) {


E.printstacktrace ();


}


Label.settext ("Free Memory:" + (int) (Runtime.getruntime (). Freememory ()/1024) + "K" + "Totle Memory, Allo


cated: "


+ (int) (Runtime.getruntime (). TotalMemory ()/1024) + "K");


status= (int) (free*100/totle);


pbar.setvalue (status);


pbar.setstring ("Free momery" + status+ "%");


try{


this.sleep (1000);


}catch (interruptedexception err) {}


}


}


}


===============================================


package Myprojects.jmemorydemo;


Import java.awt.*;


import java.awt.event.*;


import javax.swing.*;


public class MainFrame extends JFrame {


private JPanel ContentPane;


private BorderLayout borderLayout1 = new BorderLayout ();


private JProgressBar jProgressBar1 = new JProgressBar ();


private JLabel JLabel1 = new JLabel ();


private JLabel JLabel2 = new JLabel ();


public MainFrame () {


enableevents (Awtevent.window_event_mask);


try {


Jbinit ();


}


catch (Exception e) {


E.printstacktrace ();


}


}


private void Jbinit () throws Exception {


(MainFrame.class.getResource ("[Your Icon]"));


ContentPane = (JPanel) this.getcontentpane ();


contentpane.setlayout (BORDERLAYOUT1);


This.setsize (New Dimension (304, 215));


this.settitle ("Jmemorydemo");


Jlabel1.setfont (New Java.awt.Font ("Dialog", 0, 14));


jlabel1.sethorizontalalignment (Swingconstants.center);


jlabel1.settext ("Memory Monitor");


jprogressbar1.setorientation (jprogressbar.vertical);


Jprogressbar1.setfont (New Java.awt.Font ("Dialog", 0, 14));


Jprogressbar1.settooltiptext ("");


jprogressbar1.setstringpainted (TRUE);


Jlabel2.setfont (New Java.awt.Font ("Dialog", 0, 14));


Jlabel2.settext ("");


Contentpane.add (jProgressBar1, Borderlayout.center);


Contentpane.add (JLabel1, Borderlayout.north);


Contentpane.add (JLabel2, Borderlayout.south);


progressthread pThread = new Progressthread (THIS.JPROGRESSBAR1,THIS.JLABEL2);


Pthread.start ();


}


protected void processWindowEvent (WindowEvent e) {


super.processwindowevent (e);


if (e.getid () = = windowevent.window_closing) {


system.exit (0);


}


}


}

======================================================================

The prompts are as follows:

======================================================================

The key to the problem:

totle= (int) (Runtime.getruntime (). TotalMemory ()/1024);

free= (int) (Runtime.getruntime (). Freememory ()/1024);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.