-1

Sometimes, this code prints a negative number, like -64. The value is in KB.

What could be happenning?


long memoriaInicial = Runtime.getRuntime().freeMemory() / 1024;
sort(vector);
System.Out.Println(memoriaInicial - Runtime.getRuntime().freeMemory()/1024);

alansiqueira27
  • 8,129
  • 15
  • 67
  • 111

1 Answers1

6

It means after you've sorted, the garbage collector has probably freed more memory and you have more than you started out with.

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • hmmm..this post http://stackoverflow.com/questions/2927396/how-can-i-disable-java-garbage-collector says that the garbage collector only run when there is not space, but my command is: Xms850M -Xmx850M. And I am pretty sure the method didnt used 850mb – alansiqueira27 Apr 28 '11 at 02:08
  • 3
    Well, I don't know much about Java, but it's quite similar to .NET in a lot of ways and in .NET, the garbage collector can be run at arbitrary times. I think I do know enough about Java to assume your strange result is the result of garbage collection, though. – Ry- Apr 28 '11 at 02:10
  • I read again all my code, and I think I'm really using a lot memory. That's problably the correct answer. Thanks minitech. – alansiqueira27 Apr 28 '11 at 02:15