1

I am essentially trying to create a visual representation of the memory being used in my program. I have created a LineChart and set the Y-axis to be Memory used, and the X-axis to be time. My question is, what is the best way to set up a timer, so that incoming data about memory usage can be paired with the current time.

By this I mean, I want to start a timer when the window displays, and continue to count up (possibly with millisecond precision), and so I can say that after the program has been running for this long, this is the amount of memory used.

What would be the best resources to use for this task?

G Boggs
  • 381
  • 1
  • 6
  • 19

2 Answers2

0

The best bet would probably just to use System.currentTimeMillis(); and set it to a variable when you start the count, then call it again and compare the saved value with the new timer to get your time.

so..

Long startTime = System.currentTimeMillis();
//Do whatever stuff
long timeElapsed = System.currentTimeMillis() - startTime;

One thing to keep in mind with this though, is currentTimeMillis() is platform dependent on how granular it is. On unix-based you get 1 ms. of a granularity minimum, I think on windows it's 50. So if you need something more accurate than 50 ms. time steps, you might need a different method.

WillBD
  • 1,919
  • 1
  • 18
  • 26
0

You must use a StopWatch to measure the time. Please go through the following links

https://stackoverflow.com/a/8255766/1759128

There are many alternatives in different answers of the question. You can use any of them !

Community
  • 1
  • 1
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176