[antlr-interest] Java code generator memory optimization

Scott Stanchfield scott at javadude.com
Sat Sep 24 07:56:30 PDT 2005


Be careful with that...

If your program is short-lived, it's generally fine.

If your program lives a long time (like on a server), it's very very bad, as
strings that are interned are never GC'd. Over time, you can essentially
leak rarely or singularly-used strings. (The more unique strings you have,
the more memory use with no way to reclaim...)

Be careful...
-- Scott 

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org 
> [mailto:antlr-interest-bounces at antlr.org] On Behalf Of 
> Akhilesh Mritunjai
> Sent: Saturday, September 24, 2005 10:02 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Java code generator memory optimization
> 
> Hi everybody
> 
> Sorry have been totally out of touch lately. (Deepest 
> apologies to you, Terence).
> 
> I have been able to reduce memory requirement for my AST tree 
> generated by parser by upto 25% by using
> intern() function of String class to get reference of an 
> string object from internal JVM string pool.
> 
> Changes required are:
> 
> Function CommonAST.setText(String text_)
> 
> - text = text_
> + text = text_.intern()
> 
> Obviously it was a hack. Proper way to do it is to modify 
> lexer code and use "new String(char[]).intern()" where we do 
> just "new String(...)".
> 
> String.intern() has been present since JDK1.2 so it shouldn't 
> pose compatibility problems.
> 
> Secondly, I still push forward inclusion of an option 
> "useDupTree" in tree parser. This will call dupTree() rather 
> than dup() on current node when option "buildAST" is true. 
> This allows me to make stages that do pure tree-to-tree 
> transformations with no side-effects, with just wring grammar 
> for nodes of interest rather than entire tree.
> 
> Thanks
> - Akhilesh
> 
> 
> 		
> __________________________________
> Yahoo! Mail - PC Magazine Editors' Choice 2005 http://mail.yahoo.com
> 




More information about the antlr-interest mailing list