Re: Memory Leak

Robert Seward <[email protected]> Fri, 26 Feb 2010 09:42:41 -0500
Newsgroups gmane.comp.db.mysql.java
Message-ID <[email protected]>
Hello William,

My experience with Sun's JVM is the JVM never gives memory back to the
operating system. Once Java increases the heap size (specified by
parameters on the command line) Java retains that memory until the JVM
is shutdown. 

If it is important to give the memory back to the OS after your batch
operation, I would recommend ending the process. You can use a cron or
similar mechanism to restart your program for additional work.

Alternatively you could use a pattern for doing batch work to reduce the
amount of data you hold in memory concurrently.

Good luck.

Rob 

On Fri, 2010-02-26 at 00:40 -0800, William Taylor wrote:
> On Feb 26, 2010, at 12:16 AM, Christopher G. Stach II wrote:
> 
> > 
> > ----- "William Taylor" <[email protected]> wrote:
> > 
> >> On Feb 25, 2010, at 11:44 PM, Christopher G. Stach II wrote:
> >>>> Working on some code where I select 4 million rows and store the
> >>>> results in a short array.
> >>> 
> 
> 
> Here is the test case. This is with the newest connector J 5.1.12
> 
> table name is test with a single smallint column.  4 million rows.
> 
> first console output shows 0mb used by the problem.
> each output after that shows ~255MB of ram used.
> regardless of how long you let the program run the GC never reclaims the memory.
> top says java is using ~289MB ram.
> mysqld shows ~7.4mb ram
> 
> public class Test
> {
> 
>     public static void main(String[] args) 
>     {
>         new Test();
>     }
>     
>     public Test()
>     {
>         System.out.println((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())/(1024*1024)+" MB");
>         
>         try 
>         {
>             Class.forName("com.mysql.jdbc.Driver").newInstance();            
>         } 
>         catch (InstantiationException e) 
>         {
>             e.printStackTrace();                        
>         } 
>         catch (IllegalAccessException e) 
>         {
>             e.printStackTrace();            
>         } 
>         catch (ClassNotFoundException e) 
>         {
>             e.printStackTrace();
>             System.exit(1);            
>         }
>         
>         try
>         {            
>             String url = "jdbc:mysql://localhost/yourDataBase";//?dontTrackOpenResources=true";
>             String userName = "username";
>             String password = "password";
>             
>             Connection connection = DriverManager.getConnection (url, userName, password);
>             PreparedStatement statement = connection.prepareStatement("select id from test");
>             ResultSet resultSet = statement.executeQuery();        
>             
>             resultSet.close();            
>             statement.close();
>             connection.close();
>         }
>         catch (SQLException e)
>         {
>             e.printStackTrace();
>         }    
>         
>         while(true)
>         {
>             try
>             {
>                 Thread.sleep(1000);
>             }
>             catch (InterruptedException e)
>             {
>                 e.printStackTrace();
>             }
>             
>             System.out.println((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())/(1024*1024)+" MB");
>         }
>     }
> }
-- 
Rob Seward
Bluestone Consulting Group, LLC

web:     http://www.bluestone-consulting.com/
e-mail:  [email protected]
office:  734.274.5168
mobile:  734.604.3780



-- 
MySQL Java Mailing List
For list archives: http://lists.mysql.com/java
To unsubscribe:    http://lists.mysql.com/[email protected]