0

I have this timer function written in java.

    import java.util.Timer;  //Libraries used
    import java.util.TimerTask;

    public static void main(String[] args) {
    Timer clock = new Timer();
    clock.schedule(new TimerTask() {
          @Override
          public void run() {
              System.out.println(Runtime.getRuntime().freeMemory());
              
          }
        }, 1*1*1000, 1*1*1000);// One second intervals
       }

I get this as a result(in bits):

115224312

113179200

113179200

...Same number(113179200)...forever

I want this function to auto refresh and change the number, but it does not do that. And I am unsure why. I have tried other methods(loops, other timers, etc.), but those do not work either.

Community
  • 1
  • 1
Alaa
  • 115
  • 12
  • 3
    If the amount of free memory isn't changing then why would the number change? Are you using memory elsewhere? – lucasvw Apr 21 '17 at 14:09
  • Isn't memory suppose to not be constant so even if I do not do anything in the background the OS will always be changing memory usage. Which should in turn change the amount of free memory? Correct me if a am wrong please? – Alaa Apr 21 '17 at 14:10
  • 4
    [`Runtime#freeMemory`](https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#freeMemory()) returns the amount of free memory in the *Java Virtual Machine*, not the OS – UnholySheep Apr 21 '17 at 14:12
  • 1
    @Atlas see http://stackoverflow.com/questions/3571203/what-are-runtime-getruntime-totalmemory-and-freememory for more information on memory. the freememory method is only the memory within the java environment – lucasvw Apr 21 '17 at 14:12
  • @UnholySheep So if I were to make a GUI application and perform an action will the number change because an event occurred? And lets just say this event required more memory. – Alaa Apr 21 '17 at 14:16
  • 3
    If you use (or free) memory within your Java application then the number will change. Though you could just try it out instead of asking strangers on the internet. – UnholySheep Apr 21 '17 at 14:20

1 Answers1

2

I updated your code with some memory use after you start the timer check:

public static void main(String[] args) {
    Timer clock = new Timer();
    clock.schedule(new TimerTask() {
          @Override
          public void run() {
              System.out.print("Total: " + Runtime.getRuntime().totalMemory());
              System.out.println(" Free: " + Runtime.getRuntime().freeMemory());

          }
        }, 1*1*1000, 1*1*1000);// One second intervals

    // now use memory
    ArrayList<Object> arrays = new ArrayList<>();
    for (int i = 0; i < 1_000_000; ++i) {
        ArrayList<String> strings = new ArrayList<>();
        for (int j = 0; j < 1_000_000; ++j) {
            strings.add("" + j);
        }
        arrays.add(strings);
    }
    System.out.println("Loop complete");
}

And here's some of the output:

Total: 1203240960 Free: 752378848
Total: 1785200640 Free: 1107948136
Total: 3265265664 Free: 1522323408
Total: 3642228736 Free: 1180813496
Total: 5323096064 Free: 1982845016
Total: 5456265216 Free: 1627489984
Total: 7387742208 Free: 2923462928
Total: 7571767296 Free: 2455798264
Total: 7579631616 Free: 1657495280

As you can see, the amount of free memory (as well as total memory) is changing.

lucasvw
  • 1,345
  • 17
  • 36
  • 1
    So as you fill elements in an array you begin to use memory which in turn changes the value of free memory and total memory. Lucas I was mistakenly assuming that the freeMemory() function returns information about the OS while in reality it was returning information about the JVM. That was useful. – Alaa Apr 21 '17 at 14:36