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 8 of 8

Thread: Read Textfile from server, memory issue

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Read Textfile from server, memory issue

    Hello, I use this code to read a textfile from my server:

    String nextLine;
           URL url = null;
           URLConnection urlConn = null;
           InputStreamReader  inStream = null;
           BufferedReader buff = null;
           try{
              url  = new URL("mytextfile.txt" );
              urlConn = url.openConnection();
              inStream = new InputStreamReader( 
              urlConn.getInputStream());
               buff= new BufferedReader(inStream);
     
     
            while (true){
                nextLine =buff.readLine();  
                if (nextLine !=null){
                    System.out.println(nextLine); 
                }
                else{
                   break;
                } 
            }
         } catch(MalformedURLException e){
           System.out.println("Please check the URL:" + 
                                               e.toString() );
         } catch(IOException  e1){
          System.out.println("Can't read  from the Internet: "+ 
                                              e1.toString() ); 
      }
     }

    I call this every 10 seconds to read the text file. The used memory is increased then every minute, and after 2/3 hours the service gets killed. I'am not sure why the memory is increased every minute. I'am not storing the string or appending it to something.
    I need to read every 10 seconds my text file and it would be very nice if there is a solution.
    Thanks


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Read Textfile from server, memory issue

    Check what is going on with garbage collection? Are you creating and deleting lots of objects?

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Read Textfile from server, memory issue

    I'am only creating URL, URLConnection, InputStreamReader and the BufferedReader each time. Tried to declare at the beginning and only using it without creating it again, but same result.

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Read Textfile from server, memory issue

    Ensure you close all of the connections, it's hard to see what is going on without knowing the full code. Due to the nature of the problem, I am guessing threads are also involved

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Read Textfile from server, memory issue

    First I started a service, this service starts a this thread, which reads my text file.
    public int onStartCommand(Intent intent, int flags, int startId) {
    		executethread = new Thread(new Runnable() { public void run() { try {
    			Reader();
    		} catch (Exception e) {
    			 //TODO Auto-generated catch block
    			e.printStackTrace();
    		} } });
     
    		executethread.start(); 
     
    		return START_STICKY;
     
    	}


    public void Reader() throws Exception{
    			Thread.sleep(10000);
     
     
     
     
     
    			try{
    		          url  = new URL("mytext.txt" );
    		          urlConn = url.openConnection();
    		         inStream = new InputStreamReader( 
    		                           urlConn.getInputStream());
    		           buff= new BufferedReader(inStream);
    		           nextLine = buff.readLine();  
    		        while (true){
    		            nextLine = buff.readLine();  
    		            if (nextLine !=null){
    		                System.out.println(nextLine); 
    		            }
    		            else{
    		               break;
    		            } 
    		        }
     
     
    		     } catch(MalformedURLException e){
    		       System.out.println("Please check the URL:" + 
    		                                           e.toString() );
    		     } catch(IOException  e1){
    		      System.out.println("Can't read  from the Internet: "+ 
    		                                          e1.toString() ); 
    		  }
     
    			buff.close();
    			Reader();
     
     
    		}
     
    	}

    So now the thread calls itself again, but in future i want to add the text to the screen, but while this problem isn't solved i can't proceed...

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Read Textfile from server, memory issue

    Can you make a small, complete program that compiles, executes and shows the problem for testing?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    May 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Read Textfile from server, memory issue

    I'am compiling the last posted code in Eclipse for Android. I look up the memory consumption in settings and running services. You want the .apk?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Read Textfile from server, memory issue

    Android???
    Are you saying this is not a Java SE application?

    EDIT: Moved to Android section
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: April 3rd, 2013, 09:17 PM
  2. Tomcat server Issue
    By Srinivasa in forum Web Frameworks
    Replies: 1
    Last Post: November 8th, 2012, 08:10 AM
  3. Properly releasing memory to avoid memory pileup/crash
    By fickletrick in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 22nd, 2012, 10:09 AM
  4. [SOLVED] Memory usage increasing in while loop - is it a memory leak
    By mds1256 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 18th, 2012, 10:06 AM
  5. Replies: 1
    Last Post: November 21st, 2011, 01:11 AM

Tags for this Thread