code_junkie

Java and .NET heap overhead

Nov 14th, 2011
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. using System;
  2.  
  3. public class Foo
  4. {
  5. int x;
  6.  
  7. public Foo(int x)
  8. {
  9. this.x = x;
  10. }
  11. }
  12.  
  13. public class Test
  14. {
  15. static void Main(string[] args)
  16. {
  17. int length = int.Parse(args[0]);
  18.  
  19. Foo x = new Foo(0);
  20. Foo[] array = new Foo[length];
  21. // Make sure that JITting the string constructor doesn't
  22. // change things
  23. long start = GC.GetTotalMemory(true);
  24. for (int i=0; i < length; i++)
  25. {
  26. array[i] = new Foo(i);
  27. }
  28. long end = GC.GetTotalMemory(true);
  29.  
  30. GC.KeepAlive(array);
  31. GC.KeepAlive(x);
  32.  
  33. decimal totalDecimal = end-start;
  34. Console.WriteLine(totalDecimal / length);
  35. }
  36. }
Add Comment
Please, Sign In to add comment