0

I have a class, in this I initialized a frame, but I didn't extend it. When I run the program and dispose the frame by call the dispose function. other resources used in this class like String, StringBuffer , int .. etc can these be collected by JVM , or it is a memory leak ? should I use null keyword to dispose them one by one?

stelioslogothetis
  • 9,371
  • 3
  • 28
  • 53
mike
  • 1
  • Look in to this,it may help you https://www.toptal.com/java/hunting-memory-leaks-in-java – Zia Jul 13 '17 at 10:25
  • 1
    There is rarely a memory leak in Java, and certainly not if your using "basic" classes such as what you mention. The GC will collect them once there are no more references to them; you don't need to "null" them. – Steve Smith Jul 13 '17 at 10:25
  • https://stackoverflow.com/questions/1058991/how-to-monitor-java-memory-usage checkout tools which used to analyse memory utilization. You can trace from that . – Rahul Rabhadiya Jul 13 '17 at 10:26
  • Also relevant: https://stackoverflow.com/questions/13855013/understanding-java-memory-management – domsson Jul 13 '17 at 10:26
  • The "best" fit in that wide collection: Garbage collector in java - set an object null – GhostCat Jul 13 '17 at 10:28

1 Answers1

1

If the objects are not referenced by anything anymore, they will be eaten by the garbage collector when it comes around the next time. If you like, you can also kindly ask the garbage collector to do so soon by calling System.gc()

bkis
  • 2,530
  • 1
  • 17
  • 31
  • 1
    Beware: You can't depend on `System.gc()` as this call only *suggests* the VM to start the GC. It is not guaranteed to run. – QBrute Jul 13 '17 at 10:44
  • @QBrute True. That's what I meant by `...kindly ask the garbage collector to...` – bkis Jul 14 '17 at 14:02