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 / March 2008

Tip: Looking for answers? Try searching our database.

Out of memory with file streams

Thread view: 
Hendrik Maryns - 17 Mar 2008 12:46 GMT
Hi all,

I have little proggie that queries large linguistic corpora.  To make
the data searchable, I do some preprocessing on the corpus file.  I now
start getting into trouble when those files are big.  Big means over 40
MB, which isn’t even that big, come to think of it.

So I am on the lookout for a memory leak, however, I can’t find it.  The
preprocessing method basically does the following (suppose the inFile
and the treeFile are given Files):

final BufferedReader corpus = new BufferedReader(new FileReader(inFile));
final ObjectOutputStream treeOut = new ObjectOutputStream(new
BufferedOutputStream(new FileOutputStream(treeFile)));
final int nbTrees = TreebankConverter.parseNegraTrees(corpus, treeOut);
try {
    treeOut.close();
} catch (final IOException e) {
    // if it cannot be closed, it wasn’t open
}
try {
    corpus.close();
} catch (final IOException e) {
    // if it cannot be closed, it wasn’t open
}

parseNegraTrees then does the following: it scans through the input
file, constructs trees that are described in it in some text format
(NEGRA), converts those trees to a binary format, and writes them as
Java objects to the treeFile.  Each of those trees consists of nodes
with a left daughter, a right daughter and a list of strings of length
at most 5.  And those are short strings: words or abbreviations.  So
this shouldn’t take too much memory, I would think.

This is also done one by one:

TreebankConverter.skipHeader(corpus);
String bosLine;
while ((bosLine = corpus.readLine()) != null) {
 final StringTokenizer tokens = new StringTokenizer(bosLine);
 final String treeIdLine = tokens.nextToken();
 if (!treeIdLine.equals("%%")) {
  final String treeId = tokens.nextToken();
  final NodeSet forest = parseSentenceNodes(corpus);
  final Node root = forest.toTree();
  final BinaryNode binRoot = root.toBinaryTree(new ArrayList<Node>(), 0);
  final BinaryTree binTree = new BinaryTree(binRoot, treeId);
  treeOut.writeObject(binTree);
 }
}

I see no reason in the above code why the GC wouldn’t discard the trees
that have been constructed before.

So the only place for memory problems I see here is the file access.
However, as I grasp from the Javadocs, both FileReader and
FileOutputStream are, indeed streams, that do not have to remember what
came before.  Is the buffering the problem, maybe?

TIA, H.
Signature

Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Lew - 17 Mar 2008 14:03 GMT
...
> So I am on the lookout for a memory leak, however, I can’t find it.  The
> preprocessing method basically does the following (suppose the inFile
[quoted text clipped - 39 lines]
> FileOutputStream are, indeed streams, that do not have to remember what
> came before.  Is the buffering the problem, maybe?

When incomplete code is posted with a question, the answer is pretty much
always in the code not posted.  Check through the code you left out of your
post for packratted references.

<http://mindprod.com/jgloss/sscce.html>
<http://mindprod.com/jgloss/packratting.html>

Signature

Lew

Mark Space - 17 Mar 2008 16:50 GMT
> So I am on the lookout for a memory leak, however, I can’t find it.  The
> preprocessing method basically does the following (suppose the inFile
> and the treeFile are given Files):

More likely you have an error in the code and the tree is growing to the
size of the entire file.

Do you have access to a profiler?  Most profilers also annalyze garbage
collection too.  Each object that survives garbage collection gets
marked as one "generation" older by the gc, so the trick is to look for
objects which survive many generations.

If you don't have a good debugger/profiler, get one.  It's basically
required for any serious development work.  The profiler that comes with
NetBeans 6 is excellent, and it's trivial to import an existing project
into NetBeans.  Give it a shot.
Hendrik Maryns - 17 Mar 2008 19:14 GMT
Mark Space schreef:

>> So I am on the lookout for a memory leak, however, I can’t find it.  
>> The preprocessing method basically does the following (suppose the
[quoted text clipped - 12 lines]
> NetBeans 6 is excellent, and it's trivial to import an existing project
> into NetBeans.  Give it a shot.

I use Eclipse, and installed TPTP, but it’s a PITA, I didn’t get it
running.  Maybe I really should give NetBeans another try.  However, the
answer of Zig was right on the spot, so no more need for now.

Thanks anyway, H.
Signature

Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Mark Space - 17 Mar 2008 19:52 GMT
>   However, the
> answer of Zig was right on the spot, so no more need for now.

Yeah that was news to me also.  Good job, Zig.
Zig - 17 Mar 2008 17:50 GMT
> Hi all,
>
[quoted text clipped - 55 lines]
> FileOutputStream are, indeed streams, that do not have to remember what
> came before.  Is the buffering the problem, maybe?

You are right, FileOutputStream & FileReader are pretty primitive.  
ObjectOutputStream, OTOH is a different matter. ObjectOutputStream will  
keep references to objects written to the stream, which enables it to  
handle cyclic object graphs, and repeating references of the same object  
are handled predictably.

You can force ObjectOutputStream to clean up by using:

treeOut.writeObject(binTree);
treeOut.reset();

This should notify ObjectOutputStream that you will not be re-referencing  
any previously written objects, and allow the stream to release it's  
internal references.

HTH,

-Zig
Hendrik Maryns - 17 Mar 2008 19:15 GMT
Zig schreef:

>> Hi all,
>>
[quoted text clipped - 70 lines]
> re-referencing any previously written objects, and allow the stream to
> release it's internal references.

That’s exactly what I needed.  The API could have been more informing
over the memory implications of this backreferencing mechanism.  The
memory footprint is not even mentioned in the Javadoc of the reset() method.

Thank you very much!
H.
Signature

Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Mark Space - 17 Mar 2008 20:13 GMT
> The API could have been more informing
> over the memory implications of this backreferencing mechanism.  The
> memory footprint is not even mentioned in the Javadoc of the reset()
> method.

"Reset" is also a terrible name for a method of an output stream.
"Reset" normally means something totally different when talking about IO
streams.

From collections, "clear" probably would have been better.  Maybe
"cleanUpMemory" would have been even better.  And maybe using weak
references would have been even better still.  Don't make the user deal
with the internal memory of an object.
Mark Thornton - 17 Mar 2008 20:42 GMT
>> The API could have been more informing over the memory implications of
>> this backreferencing mechanism.  The memory footprint is not even
[quoted text clipped - 8 lines]
> references would have been even better still.  Don't make the user deal
> with the internal memory of an object.

WeakReference's would probably be a bad idea --- the overhead is
significant.

Mark Thornton
Mark Space - 17 Mar 2008 20:52 GMT
> WeakReference's would probably be a bad idea --- the overhead is
> significant.

I've seen an article or white paper on this, I think from Sun.  They
said to put all references in a regular old Map (HashMap, etc.), then
use one weak or soft reference to the map itself.

SoftReference cache = new SoftReference( new HashMap() );

Then, when the GC needs to collect this, it sees one soft reference,
frees that, and then can automatically free everything in the hash map
without any more checking or involved processing.  It's much much
lighter on the GC than using a weak hash map, which makes the GC check
every single weak reference.

I'll try to find that article, I don't see it right now....
Zig - 17 Mar 2008 22:29 GMT
>> The API could have been more informing over the memory implications of  
>> this backreferencing mechanism.  The memory footprint is not even  
[quoted text clipped - 8 lines]
> references would have been even better still.  Don't make the user deal  
> with the internal memory of an object.

Well, "reset" does reset the ObjectOutputStream back to the state it was  
initialized to: all objects are new, no class descriptors have been  
written, etc. Using reset() will decrease your memory overhead, but at the  
cost of increasing your data size (since the class descriptors have to be  
re-serialized).

Weak / Soft references would be slick, but there is an extra "quirk" that  
would have to be addressed. reset() will put a RESET marker in the stream  
in order for ObjectInputStream to recognize that the stream has been reset  
(at which point it will dump it's references). Even while  
ObjectOutputStream could use soft references to determine that an object  
is no longer referenced, you also have to notify ObjectInputStream whent  
it's safe to clear it's references to objects that will not be  
subsequently re-referenced later in the stream.

For simple objects you can get around all of this by using

ObjectOutputStream.writeUnshared() / ObjectInputStream.readUnshared(), but  
those will still graph references for the nested object you'll get in a  
tree structure :/

Anyway, hope that was interesting food for thought,

-Zig
Hendrik Maryns - 18 Mar 2008 12:49 GMT
Mark Space schreef:
>> The API could have been more informing over the memory implications of
>> this backreferencing mechanism.  The memory footprint is not even
[quoted text clipped - 8 lines]
> references would have been even better still.  Don't make the user deal
> with the internal memory of an object.

That’s what I think as well, so I created a bug report: 1209747 (not yet
visible).

H.
Signature

Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Zig - 17 Mar 2008 22:38 GMT
> Zig schreef:
>>
[quoted text clipped - 79 lines]
> memory footprint is not even mentioned in the Javadoc of the reset()  
> method.

Glad to help!

> Thank you very much!
> H.
Roedy Green - 20 Mar 2008 06:43 GMT
On Mon, 17 Mar 2008 12:46:06 +0100, Hendrik Maryns
<gtw37bn02@sneakemail.com> wrote, quoted or indirectly quoted someone
who said :

>I see no reason in the above code why the GC wouldn’t discard the trees
>that have been constructed before.

Places to start:
http://mindprod.com/jgloss/packratting.html
http://mindprod.com/jgloss/profiler.html

to find out what is chewing up all your RAM.
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



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.