5
public class MemoryLeakExample {
    public static void main(String[] args) {
        while (true) {
            System.gc();
        }
    }
}

How is memory leaking in this program? Memory is gradualy increasing while monitoring through the NETBEANS profiler. Guide me if am wrong, any help is appreciated. Before 5 min USED HEAP SIZE is :2257416 After: 2258360

Thank you.

4
  • 1
    Is the program eventually stopping because of an OutOfMemoryError? Commented Nov 22, 2012 at 13:43
  • What exact effects do you see and how do you determine that there really is a leak? Commented Nov 22, 2012 at 13:44
  • Are you sure you're not just leaking in the monitoring process ? And if you monitor, why don't you look where is the memory gone ? Commented Nov 22, 2012 at 13:45
  • Take look stackoverflow.com/questions/6470651/… in this link show you how do you can creating a memory leak. Commented Nov 22, 2012 at 13:51

2 Answers 2

11

I ran this code:

final Runtime rt = Runtime.getRuntime();
long before = System.currentTimeMillis();
while (true) {
  System.gc();
  long now = System.currentTimeMillis();
  if (now - before > 1000) {
    System.out.println(rt.totalMemory() + " " + rt.freeMemory());
    before = now;
  }
}

The numbers it prints are totally stable. In your case it is the profiler itself that is occupying memory with profiling data.

Sign up to request clarification or add additional context in comments.

3 Comments

please check with this you will find the difference ` System.out.println("TOTAL "+rt.totalMemory() + " FREE " + rt.freeMemory()+" USED "+(rt.totalMemory() - rt.freeMemory()));`
OP : TOTAL 62914560 FREE 62353960 USED 560600 TOTAL 62914560 FREE 62353960 USED 560600 TOTAL 62914560 FREE 62353960 USED 560600 TOTAL 62914560 FREE 62353960 USED 560600 TOTAL 62914560 FREE 62353416 USED 561144 TOTAL 62914560 FREE 62353416 USED 561144 TOTAL 62914560 FREE 62353416 USED 561144 TOTAL 62914560 FREE 62353416 USED 561144 TOTAL 62914560 FREE 62353416 USED 561144 TOTAL 62914560 FREE 62353464 USED 561096 TOTAL 62914560 FREE 62353464 USED 561096
Nope, the numbers are absolutely stable for me. Your case is not that different, either. You have observed a rise of 496 bytes in 10 seconds, which implies hundreds if not thousands of loop iterations. Note that it even subsided a little, so it isn't even steady growth, but a very minor fluctuation.
5

Memory is gradualy increasing while monitoring through the NETBEANS profiler.

The VisualVM profiler uses memory to do its profiling. If you perform memory profiling you can see that the memory used is objects sent to the profiler.

If you use a commercial profiler, like YourKit, it records profiling information in native memory so it doesn't impact the heap with what it does.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.