0

I am performing analysis of different sort algorithms. Currently I am analysing the Insertion Sort and Quick Sort. And as part of the analysis, I need to measure the memory consumption.

I am using Visual VM for profiling. However when I execute the Insertion Sort for a random data set of, let's say 70,000, I get different range of Heap Memory usage. For example, in the first run the heap memory consumption was 75 kbytes and then in the next round it drops to 35 kbytes. And if I execute it few more times then this value fluctuates randomly.

Is this normal or am I missing something here ? I have plot a graph of data size versus the memory consumption and with this fluctuation I won't be able to draw a chart.

java version "1.8.0_65"

RDM
  • 1,007
  • 2
  • 18
  • 40
  • 1
    Maybe the Garbage collector do a pass in some of your test and not in some other? – litelite Feb 01 '17 at 21:22
  • Is there a better to get the memory consumption data ? – RDM Feb 01 '17 at 21:25
  • 75kb ? Do not hesitate about this. Anyway, insertion sort should have no memory impact at all. – PeterMmm Feb 01 '17 at 21:33
  • You could try use jconsole, it can be found at $JDK/bin folder – Jesus Espinoza Feb 01 '17 at 21:34
  • I have tried Jconsole as well. Its the same thing. Value fluctuates. – RDM Feb 01 '17 at 21:35
  • 1
    Analyze the algorithm to determine it's memory requirements. One algorithm might create 1000 objects, but only keep one at a time, while another algorithm might create 200 objects, but keep all of the them until the end. Which consumes more memory? #1 generates more garbage and requires more memory before GC is needed, but can run on very little memory. #2 requires more memory to run, but generates less garbage. Monitoring the app will not show the difference, unless you continually trigger GC. So, you need to examine the code instead. – Andreas Feb 01 '17 at 21:36

2 Answers2

0

This is Java's garbage collector at work, it kicks in at its own pace and does its job. Perhaps, it would be best for you to measure the amount of memory used after explicitly calling System.gc(), so that you're not taking notes of the garbage.

EDIT: System.gc() should be called after you perform your tests, to explicitly request that garbage collector kicks in. While it is true that System.gc() is treated only as a request and it is not mathematically 100% sure that JVM will respect your request, it is most probably safe for your analysis, especially if you perform several runs of it.

With regards to measuring memory usage, it is quite tricky, especially for low values. Please see this answer which contains some nice details:

Community
  • 1
  • 1
JChrist
  • 1,569
  • 19
  • 22
  • Do I need to call System.gc() before I call the sort method ? or is there a better way to get the data for memory consumption ? – RDM Feb 01 '17 at 21:24
  • Calling `System.gc()` do not garanted at all, that any GC is done. Read the API doc. – PeterMmm Feb 01 '17 at 21:28
  • see [When does System.gc() do anything](http://stackoverflow.com/questions/66540/when-does-system-gc-do-anything) – Jesus Espinoza Feb 01 '17 at 21:30
  • Thats true. Calling System.gc doesn't guarantee that GC will happen. So how do I get an accurate data for memory consumption ? – RDM Feb 01 '17 at 21:36
0

You may find JMH useful for running benchmarks while isolating side effects from the JVM.

Read through the code samples to understand how to use it.

chaim
  • 294
  • 1
  • 7