Guest User

Untitled

a guest
Jun 25th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. package fr.brouillard.jvm;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.LinkedList;
  7. import java.util.List;
  8.  
  9. public class MemoryFree {
  10. private BufferedReader reader = new BufferedReader(new
  11. InputStreamReader(System.in));
  12. private List<byte[]> usedMemory = new LinkedList<byte[]>();
  13. private int totalMB = 0;
  14. private int gcTimes = 0;
  15.  
  16. public void allocate(int howManyMB) {
  17. usedMemory.add(new byte[howManyMB * 1024 * 1024]);
  18. totalMB += howManyMB;
  19. System.out.println(howManyMB + "MB allocated, total allocated: " +
  20. totalMB + "MB");
  21. }
  22.  
  23. public void free() {
  24. usedMemory.clear();
  25. }
  26.  
  27. public void gc() {
  28. System.gc();
  29. System.out.println("GC " + (++gcTimes) + " times" );
  30. }
  31.  
  32. public void waitAnswer(String msg) {
  33. System.out.println("Press [enter]" + ((msg==null)?"":msg));
  34. try {
  35. reader.readLine();
  36. } catch (IOException e) {
  37. }
  38. }
  39.  
  40. public static void main(String[] args) {
  41. MemoryFree mf = new MemoryFree();
  42. mf.waitAnswer(" to allocate memory");
  43. mf.allocate(20);
  44. mf.allocate(10);
  45. mf.allocate(15);
  46. mf.waitAnswer(" to free memory");
  47. mf.free();
  48. mf.waitAnswer(" to GC");
  49. mf.gc();
  50. mf.waitAnswer(" to GC");
  51. mf.gc();
  52. mf.waitAnswer(" to GC");
  53. mf.gc();
  54. mf.waitAnswer(" to GC");
  55. mf.gc();
  56. mf.waitAnswer(" to exit the program");
  57.  
  58. try {
  59. mf.reader.close();
  60. } catch (IOException e) {}
  61. }
  62. }
  63.  
  64. -verbosegc -XX:+PrintGCDetails
Add Comment
Please, Sign In to add comment