Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Memory usage increasing in while loop - is it a memory leak

  1. #1
    Member
    Join Date
    Nov 2009
    Posts
    57
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Memory usage increasing in while loop - is it a memory leak

    Hi

    I have the following bit of code, basically keeps getting a list of available Drives on a windows computer (every 2 seconds at the moment but it will be every 30secs).

    Trouble is that when running this when watching the memory usage in Task Manager the memory keeps increasing, I have left this running for around 10 minutes and it still increasing.

    Is there any reason why this is happening as it SHOULD be just replacing the contents of 'currentDrives' variable.

    Thanks

    import java.io.File;
     
    public class Main {
     
     
        public static void main(String[] args)
        {
            File[] currentDrives = File.listRoots();
     
            while(true)
            {
                currentDrives = File.listRoots();
     
                try
                {
                    Thread.sleep(2000);
                }
                catch(Exception ex)
                {
                    System.out.println("Error: " + ex.getMessage());
                }
            }
     
        }
     
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Memory usage increasing in while loop - is it a memory leak

    Trouble is that when running this when watching the memory usage in Task Manager the memory keeps increasing, I have left this running for around 10 minutes and it still increasing
    Does it ever throw an out of memory exception? If not, then I wouldn't worry. A variable that is out of scope with no more references means it is eligible for garbage collection, but does not mean it is immediately garbage collected. If this were how the garbage collector worked, it would add massive overhead to an application.
    See http://java.sun.com/docs/books/perfo...PAppGC.fm.html

  3. #3
    Member
    Join Date
    Nov 2009
    Posts
    57
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Memory usage increasing in while loop - is it a memory leak

    Thanks very much, it has been running around 30 minutes now and it seems to have stopped increasing the memory usage, it starts off at around 7mb and has stopped increasing around 12.6mb and has been solid there for around 5 minutes now.

    Thanks again

Similar Threads

  1. Heap Memory
    By vamsi in forum Java Theory & Questions
    Replies: 3
    Last Post: November 19th, 2011, 12:48 PM
  2. Insertion Sort - memory usage
    By lovon in forum Algorithms & Recursion
    Replies: 2
    Last Post: November 6th, 2011, 04:00 PM
  3. In Memory CD Database
    By Laxman2809 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 7th, 2011, 11:13 AM
  4. Memory Handling -de allocate memory in Java
    By 19world in forum Java Theory & Questions
    Replies: 4
    Last Post: June 15th, 2010, 04:05 AM
  5. Out of Memory Error
    By wasaki in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 31st, 2010, 03:37 PM