8 Replies Latest reply: Sep 14, 2007 8:59 AM by 807592 RSS

    Java Memory Monitor

    807590
      Hello
      I have a little question.
      I want to create a memori monitor like in NetBeans 5. to see memory status.
      I am not sure that i can monitor SYSTEM memory usage, but it will be fine if i can monitor JVM memory ussage.
      Can anyone help me or give me a good link to a tutorial ?

      Thank you !
        • 1. Re: Java Memory Monitor
          807590
          I have made my own memory monitor bar. I post the code below :
          import java.awt.*;
          import javax.swing.*;
          
          
          /**
           * Memory monitor component.
           *
           * @author     Antoine Lemoine
           * @version     1.0 (Released November 25, 2005)
           */
          public class MemoryMonitorBar extends JPanel
          {
          
               /*--------------------------------------------------------------------------
                         Constructors
               --------------------------------------------------------------------------*/
          
               public MemoryMonitorBar()
               {
                    setLayout(new FlowLayout(FlowLayout.CENTER,5,0));
          
                    this.label = new JLabel();
                    add(label);
                    
                    Thread thread =
                         new Thread()
                         {
                              public void run()
                              {
                                   repaint();
                                   try { Thread.sleep(1000); } catch (Exception e) {}
                              }
                         };
               }
          
               /*--------------------------------------------------------------------------
                         Protected redefined methods
               --------------------------------------------------------------------------*/
          
               protected void paintComponent(Graphics g)
               {
                    super.paintComponent(g);
          
                    long freeMemory = Runtime.getRuntime().freeMemory();
                    long totalMemory = Runtime.getRuntime().totalMemory();
                    long maxMemory = Runtime.getRuntime().maxMemory();
          
                    long memoryUsed = totalMemory-freeMemory;
          
                    label.setText((memoryUsed/(1024*1024))+" of "+(maxMemory/(1024*1024))+" MB used");
          
                    Insets insets = getInsets();
          
                    int width = getWidth()-insets.left-insets.right;
                    int height = getHeight()-insets.top-insets.bottom;
          
                    int x0 = insets.left;
                    int x1 = insets.left+(int)(width*memoryUsed/maxMemory);
                    int x2 = insets.left+(int)(width*totalMemory/maxMemory);
                    int x3 = insets.left+width;
          
                    int y0 = insets.top;
                    int y1 = insets.top+height;
          
                    Graphics2D g2d = (Graphics2D)g;
                    Paint oldPaint = g2d.getPaint();
                    g2d.setPaint(new GradientPaint(0,0,Color.white,0,height,new Color(0x0072B6)));
                    g2d.fillRect(x0,y0,x1,y1);
          
                    g2d.setPaint(oldPaint);
          
                    g2d.setColor(new Color(0x999999));
                    g2d.fillRect(x1,y0,x2,y1);
          
                    g2d.setColor(getBackground());
                    g2d.fillRect(x2,y0,x3,y1);
               }
          
               /*--------------------------------------------------------------------------
                         Private attributes
               --------------------------------------------------------------------------*/
          
               private JLabel label;
          
          }
          • 2. Re: Java Memory Monitor
            800322
            Hello
            I have a little question.
            I want to create a memori monitor like in NetBeans 5.
            to see memory status.
            I am not sure that i can monitor SYSTEM memory usage,
            you can't.
            but it will be fine if i can monitor JVM memory
            ussage.
            Can anyone help me or give me a good link to a
            tutorial ?
            read the Runtime API.
            • 3. Re: Java Memory Monitor
              807590
              Thank you
              I WONT USE YOUR CODE IN MY PROJECT!
              It will be a guide for me so no need to worry !

              Thank you !
              • 4. Re: Java Memory Monitor
                807590
                Oh, you could, I'm not fanatic ;-)
                And no GPL license that contrains you to make an open source program.

                You could use, re-use, un-use and modify it ;-)
                • 5. Re: Java Memory Monitor
                  807590
                  Thank you ;)
                  But one thing i cannot understand
                  100% is my JPanel's width right ?
                  Then a "blue gradient box" appears and a gray one
                  What this means ?
                  for example for 5 MB used i have a preety long blue bar and a sort gray one
                  this means that there is a little more to reach 6 MB ?

                  Thank you
                  • 6. Re: Java Memory Monitor
                    807590
                    The first value with a gradient bar represents the memory used by the JVM
                    The second value represented by a gray bar is the memory allowed to the JVM at the time
                    The third value, the panel's width is the memory that the JVM can allow to herself if it is necessary : the JVM take more memory when the memory used reaches the memory allowed.

                    The JVM allowed memory is set to xMin JVM creation param at the begin of your program and the JVM max memory (size of the panel) is set to xMax JVM creation param
                    • 7. Re: Java Memory Monitor
                      807592
                      I have a question in regards to scramNet? I kind of found this helpful about ScramNet:

                      SCRAMNet (Shared Common Random Access Memory Network) is an interesting and unique approach to shared memory that has found relatively widespread application. It consists of memory/interface cards connected by a 150 megabit per second optical fiber token ring network.

                      If anyone knows scramNet could you please answer this question.

                      Can I write a java program to monitor the scramNet memory?
                      • 8. Re: Java Memory Monitor
                        807592
                        CeciNEstPasUnProgrammeur wrote:
                        Hello
                        I have a little question.
                        I want to create a memori monitor like in NetBeans 5.
                        to see memory status.
                        I am not sure that i can monitor SYSTEM memory usage,
                        you can't.
                        Unless you use the mbean interface to the JVM (JMX). The JMX interfaces exposes the platform mbean which has an 'OperatingSystem' Object. It contains various details on the OS the JVM is running on. Including FreePhysicalMemorySize and some other memory info.

                        >
                        but it will be fine if i can monitor JVM memory
                        ussage.
                        Can anyone help me or give me a good link to a
                        tutorial ?
                        read the Runtime API.