0

I am testing a sorting algorithm, and I would like to test the average used memory by testing 1000 random vectors. The problem is when I run about the 20 random vector inside a loop, the garbage collector run and I lose the calculation. What should I do please? I don't want to test it manual one by one =X.

for(int j = 0; j < 1000; j++)
{
    int vetOriginal[] = Generate(); 
    for(int i = 0; i < 10; i++)
    {
        int vetParaTeste[] = vetOriginal.clone();
        long memoriaInicial = Runtime.getRuntime().freeMemory() / 1024;
        mergeSort(vetParaTeste);
        somaMemoriaKB += memoriaInicial - Runtime.getRuntime().freeMemory()/1024;
    }
}
System.out.println("Average memory used: " + somaMemoriaKB / (1000* 10));
Dan D.
  • 73,243
  • 15
  • 104
  • 123
alansiqueira27
  • 8,129
  • 15
  • 67
  • 111
  • Sounds like a scope issue. It could be that an object you need should be instantiated at a higher level. Got some code to post? – Richard Brightwell Apr 30 '11 at 03:01
  • Why does the average used memory even matter? The garbage collector is going to be a constant in every JVM, so when it runs it is doing exactly what it's supposed to, and what it would do in "real" runtime. Discarding the results of GC will only corrupt your results. – Joseph Ottinger Apr 30 '11 at 03:05
  • Richard, I added the code. But I think stackoverflow is a little bugged. Joseph, but my code may generate negative free memory: http://stackoverflow.com/questions/5812785/negative-free-memory – alansiqueira27 Apr 30 '11 at 03:12
  • My question still stands: I don't understand what you're trying to test, or why you're trying to factor out something that will ALWAYS be part of your runtime. – Joseph Ottinger Apr 30 '11 at 03:20
  • I'm just testing multiple cases. My main program wont run it 1000 times. I want to know the average memory used. It's a scientific test – alansiqueira27 Apr 30 '11 at 04:03
  • @Seva: I understand, but part of Java is the indeterministic nature of GC. Your test is working; it's just no deterministic because the environment isn't deterministic. – Joseph Ottinger Apr 30 '11 at 11:13
  • @Joseph, I believe it is deterministic. I want to test how much memory an application will usually waste to run a method 1x. If the garbage collector will clean or not, its not my fault. – alansiqueira27 Apr 30 '11 at 14:45
  • @Seva: I understand, but respectfully disagree. I wish you all the luck in the world. – Joseph Ottinger May 01 '11 at 02:26

1 Answers1

2

OK, so I'm a little late here and you have probably solved this by now, but just in case someone else wants to know, The easiest way to prevent the GC from cleaning up an object is to keep a reference to it in another object. For every object you create add it to some sort of container like an array/hash/vector/list or whatever your language supports. Eg:

var items as Array[0..numberOfItems]

for (var i = 0; i < numberOfItems; i++) {
    var vector = createVector()
    items[i] = vector;
}

The container would add some overhead so you would need to measure it first then subtract that amount from the final output. For example:

var baseMemory = measureMemory()
var numberOfItems = 1000

// create an array of a known size (eg 1000 bytes)
var arrayOfKnownSize as Array[0..numberOfItems]

for (var i = 0; i < numberOfItems; i++)
    arrayOfKnownSize[i] = Int32(0xFF)

// calculate the size occupied by just the data ...
var expectedMemory = sizeOf(Int32) * numberOfItems 

// ... subtract this from the total memory usage and you will get the overhead 
var arrayOverhead = measureMemory() - baseMemory - expectedMemory

// now measure the memory used by an array of vectors
baseMemory = measureMemory()
var vectors as Array[0..numberOfItems]

for (var i = 0; i < numberOfItems; i++) {
    var vector = createVector()
    vectors[i] = vector;
}

// subtract the array overhead from the memory usage
var usedMemory = measureMemory() - baseMemory - arrayOverhead
var averageMemory = usedMemory / numberOfItems

You would then do the same measurements as you did, inserting each vector into an array and subtract arrayOverhead from the memory usage to get your final result.

Luke Van In
  • 5,215
  • 2
  • 24
  • 45
  • Dude, you are a genious! thanks a lot! I didnt find a solution before you answer this! \o/. I will try that in a few days or week. – alansiqueira27 Jul 27 '11 at 13:51