Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

Memory leak and SoftReferences

Thread view: 
Joe - 01 Apr 2006 06:02 GMT
Hi,

 I am working on an application that gets large objects from a remote
 server and stores them in a Vector for a short period of time. When
 the next set of large objects come in the removeAll() method is
 called on the Vector object and the next set of large objects are
 placed one at a time into the Vector. These large objects stay in the
 Vector for a short amount of time (about 5 minutes) before the next
 large set comes in.

 However, there is a memory leak and I believe it has to do with this
 Vector and the objects. I am guessing that for some reason the GC
 isn't garbage collecting these large objects after they are removed
 from the Vector so I was thinking of using SoftReferences. Now, I
 don't know much about SoftReferences but I figure storing Soft-
 References would be better than storing the references to the acutal
 object.

 First, do you think my guess about the GC not collecting unused
 references is valid?
 Second, would softReferences help reduce these memory leaks?
 Third, are there any good tutorials on using Soft/WeakReferences?

 Any thoughts or comments would be greatly appreciated.

Thanks,
 Joe
steve - 01 Apr 2006 11:12 GMT
> Hi,
>
[quoted text clipped - 23 lines]
> Thanks,
>   Joe

check the sun news groups there is an excellent article on soft references.

But you will need to down load the  soft compiler version , for your JVM.

Steve

Signature

NewsGuy.Com 30Gb $9.95 Carry Forward and On Demand Bandwidth

Joe - 01 Apr 2006 16:39 GMT
BTW, I am using the JDK 1.4.2.

I tried to tried to implement SoftReferences just now but it didn't
seem to work. However, I don't think I did it right. I created a
SoftReference and put it into the Vector and then set the large
object that I create the SoftReference for to null. I still got an
OutOfMemoryException.

What do you mean I "will need to download the soft compiler version?

Thanks,
 Joe
Wibble - 01 Apr 2006 17:57 GMT
> BTW, I am using the JDK 1.4.2.
>
[quoted text clipped - 8 lines]
> Thanks,
>   Joe

You should get a profiler like optimizeIt or JProbe and find out
what your leak really is.  Its probably not what you think.

You can get a free 30 days of either.
Larry Barowski - 02 Apr 2006 00:30 GMT
> You should get a profiler like optimizeIt or JProbe and find out
> what your leak really is.  Its probably not what you think.
>
> You can get a free 30 days of either.

Or get Java 1.6 and use jmap and jhat.
Wibble - 01 Apr 2006 17:59 GMT
> BTW, I am using the JDK 1.4.2.
>
[quoted text clipped - 8 lines]
> Thanks,
>   Joe

Also, check your process size.  Sometimes you get OOM errors
when you exceed stack size or allocate buffers with size of
Integer.MAX_VALUE or something.  Run it in jdb and catch OOM Error.
steve - 01 Apr 2006 22:32 GMT
> BTW, I am using the JDK 1.4.2.
>
[quoted text clipped - 8 lines]
> Thanks,
>   Joe

sorry could not resist.

instead of using the removeAll(), just redefine the vector  using the same
name  or set it to null when you have finished.

It works, and so will the GC,  unless you did something really silly.

Another trick to use , is instead of sticking your objects into a vector,
write the objects out to  temp files , then store the file names in a hash
map (just do it in a background thread), I use this system for displaying
scanned document pages, and it really is quite fast.

be clear what a soft reference is, it can be just as dangerous as a "hard"
ref,  when used wrongly and will not  always guarantee that you will get  rid
of the  OutOfMemoryException.

seve

Signature

NewsGuy.Com 30Gb $9.95 Carry Forward and On Demand Bandwidth

Patricia Shanahan - 01 Apr 2006 14:57 GMT
>   First, do you think my guess about the GC not collecting unused
>   references is valid?

Are you actually running out of memory, or just seeing the memory usage
increase?

Patricia
Joe - 01 Apr 2006 16:36 GMT
I am getting an OutOfMemoryException and my application will stop
working. The application has an upper limit of 1.2 gig.

BTW, I am using JDK 1.4.2.

-- Joe
Patricia Shanahan - 01 Apr 2006 18:52 GMT
> Hi,
>
[quoted text clipped - 3 lines]
>   called on the Vector object and the next set of large objects are
>   placed one at a time into the Vector.

Are you really using removeAll(), rather than clear() or
removeAllElements()?

removeAll() takes another collection as argument, and only removes
elements in that collection. clear() and removeAllElements() are
equivalent, and each leaves you with a zero size Vector.

Patricia
Joe - 02 Apr 2006 05:21 GMT
Actually, I believe it is removeAllElements().

-- Joe
steve - 02 Apr 2006 22:35 GMT
> Actually, I believe it is removeAllElements().
>
> -- Joe

if that were true, then all my apps would be leaking memory as well.
I still extensively use  removeAllElements() mainly in my routines for
pulling data  from oracle,  and have not seen extensive memory leaks in that
area.

int ColumnCount = rsmd.getColumnCount();
           //  Loop  through  ResultSet  rows    
           int loop = 0;
           newmapping_list.removeAllElements();
           //  empty  the  supplier  list  
           while (rset.next()) {
               String[] record1 = new String[ColumnCount];
               for (int i = 0; i < record1.length; i++) {
                   record1[i] = (String)rset.getString(i + 1);
                   //  COPY  THE  DATA  TO  A  LOCAL  ARRAY  OF  THE  RIGHT  
 SIZE
               }
               loop++;
               newmapping_list.addElement(record1);
           }
       } catch (Exception e) {
           Error_stuff.handleError(e, Error_stuff.EXEPTION_ERROR, -1);
       }
       //never allow a bad array out
       finally {
        closeports(cstmt, rset);
           if (newmapping_list.size() == 0) {
               String[] record1 = new String[2];
               record1[0] = EMPTYBADTABLE;
               record1[1] = EMPTYBADTABLE;
               newmapping_list.addElement(record1);
           }
 
       }
       return newmapping_list;
   }

Signature

NewsGuy.Com 30Gb $9.95 Carry Forward and On Demand Bandwidth



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.