You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Sean Schofield <se...@gmail.com> on 2006/04/19 05:03:34 UTC

Re: TreeModelBase/HtmlTree : Java heap space

You are probably trying to load way too much data into the tree.  How
much data are you talking about?  I would suggest looking at the
archives for discussions on dynamic trees.  You can customize tree2
model to use a database.

Sean

On 4/17/06, Sunil Kulkarni <su...@yahoo.com> wrote:
> Hi,
>
>  I am using MyFaces 1.1.1/Tomcat 5.5.12. I am using sample TreeBacker.java
> file to render a tree.
>
>  If I don't get data from Database, then everything works fine. But, if I am
> trying to get Tree information from database and then construct 'TreeNode'
> then I get the following exception.
>
>  There is similar issue reported in users list with subject "Java heap
> space: Error with Tree2", but I did not find any answer to it.
>
>  Please let me know, if you know the solution.
>
>  Thanks.
>
>
>  javax.faces.FacesException: Caused by: javax.servlet.ServletException:
> ServletException in '/common/login_menu.jsp': Java heap space
>      at
> org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:848)
>      at
> org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
>      at
> org.apache.jsp.template.logintemplate_jsp._jspService(logintemplate_jsp.java:78)
>      at
> org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
>      at
> javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
>      at
> org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
>      at
> org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
>      at
> org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
>      at
> javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
>      at
> org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
>      at
> org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
>      at
> org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:672)
>      at
> org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:463)
>      at
> org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:398)
>      at
> org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:301)
>      at
> org.apache.myfaces.context.servlet.ServletExternalContextImpl.dispatch(ServletExternalContextImpl.java:415)
>

Re: TreeModelBase/HtmlTree : Java heap space

Posted by Andrew Robinson <an...@gmail.com>.
That last class is supposed to be CustomTreeNode.

Then, just have a TreeModel as a member variable of you backing-bean and
return that (or create a new one each time, up to you). If you keep it
around as a variable, it will keep the expanded state across calls.


On 4/19/06, Andrew Robinson <an...@gmail.com> wrote:
>
> I have a static tree model with one root node. Then, I override the node
> interface. Something like this:
>
>   public class CustomTreeRootNode
>     implements TreeNode, Serializable {
> ...
>     public List getChildren() {
>       MyObject parent = rootObject;
>       List<MyObject> myChildren = parent.getChildren();
>       List<TreeNode> nodes = new ArrayList<TreeNode>();
>       for (MyObject child : myChildren)
>       {
>          nodes.add(new CustomTreeNode(child.getId()));
>       }
>     }
> ...
>   }
>
>   public class CustomRootNode
>     implements TreeNode, Serializable {
>     private String identifier;
>     public CustomRootNode(String id) { this.identifier = id; }
> ...
>     public List getChildren() {
>       MyObject parent = myHashMap.get(this.identifier);
>       List<MyObject> myChildren = parent.getChildren();
>       List<TreeNode> nodes = new ArrayList<TreeNode>();
>       for (MyObject child : myChildren)
>       {
>          nodes.add(new CustomTreeNode(child.getId()));
>       }
>     }
> ...
>
>   }
>
>
>
>
>
> On 4/19/06, sunilskulkarni <su...@yahoo.com> wrote:
> >
> >
> > Thanks Sean, Andrew.
> >
> > I am trying to construct tree2 from following DB table. KaMenu object
> > represents the following table.
> > +-------+--------+------+------+--------+
> > | child | parent | name | link | locale |
> > +-------+--------+------+------+--------+
> > |     1 |      1 | n1   | l1   |      0 |
> > |     2 |      1 | n2   | l2   |      0 |
> > |     3 |      1 | n3   | l3   |      0 |
> > +-------+--------+------+------+--------+
> >
> > Here is my algorithm to construct the TreeNode for tree2. I tried server
> > side tree2 without any help. It is recursive. nodeLeafRef is a hashmap
> > that
> > returns TreeNode for rootNode 1. When the function is called, it is
> > filled
> > with information about the root node. In this example nodeLeafRef.size()
> > is
> > 3, but I still get the Java heap space exception.
> >
> > Any help is really appreciated. Thank you.
> >
> > public void buildTreeData(KaMenu kaMenu) {
> >
> >                 //List list = kaMenuModel.findChildren(kaMenu.getChild
> > ());
> >                 List list = (ArrayList)parentChild.get(kaMenu.getChild
> > ());
> >                 // If the list is empty, then its a leaf else it is a
> > node
> >
> >                 if(list != null)
> >                 {
> >                         int n = list.size();
> >                         if (n > 0) {
> >                                 // Add the parent to the Hashtable
> >                                 TreeNode nodeRef = new
> > TreeNodeBase("foo-folder",
> > kaMenu.getName(),false);
> >
> >                                 if (!nodeLeafRef.containsKey(
> > kaMenu.getChild())) {
> >                                          nodeLeafRef.put(kaMenu.getChild(),
> > nodeRef);
> >                                 } else {
> >                                         log.severe("Navigation has
> > duplicate node ids");
> >                                 }
> >
> >                                 // Check, if each child has its own
> > children
> >                                 Iterator itr = list.iterator();
> >                                 for (int i = 0; i < n; i++) {
> >                                         Integer child =
> > (Integer)itr.next();
> >                                         KaMenu kaChildMenu = (KaMenu)
> > nodeInfo.get(child);
> >                                         buildTreeData(kaChildMenu);
> >                                 }
> >
> >                                 // Add the node to its parent
> >                                 // if(parent != 1){
> >                                 TreeNode treeNode = (TreeNode)
> > nodeLeafRef.get(kaMenu.getParent());
> >                                 if (treeNode != null) {
> >                                         nodeRef.getChildren
> > ().add(nodeRef);
> >                                         nodeLeafRef.remove (
> > kaMenu.getParent());
> >                                         nodeLeafRef.put(kaMenu.getParent(),
> > treeNode);
> >                                 }
> >                                 treeNode = null;
> >                                 nodeRef = null;
> >                                 // }
> >                         }
> >                 } else {
> >                         TreeNodeBase leaf = new TreeNodeBase("document",
> > kaMenu.getName(),
> >                                          kaMenu.getLink(), true);
> >
> >                         // Add the leaf to its node
> >                         TreeNode treeNode = null;
> >                         TreeNodeBase treeNodeBase = null;
> >
> >                         // If the parent is root node(1), then add the
> > TreeNodeBase
> >                         if (kaMenu.getParent().intValue() != 1) {
> >                                 treeNode = (TreeNode) nodeLeafRef.get(
> > kaMenu.getParent());
> >                                 if (treeNode != null) {
> >                                         treeNode.getChildren
> > ().add(leaf);
> >                                         nodeLeafRef.remove(
> > kaMenu.getParent());
> >                                         nodeLeafRef.put(kaMenu.getParent(), treeNode);
> >                                         treeNode = null;
> >                                 }
> >                         } else {
> >                                 treeNodeBase = (TreeNodeBase)
> > nodeLeafRef.get (kaMenu.getParent());
> >                                 if (treeNodeBase != null) {
> >                                         treeNodeBase.getChildren
> > ().add(leaf);
> >                                         nodeLeafRef.remove (
> > kaMenu.getParent());
> >                                         nodeLeafRef.put(kaMenu.getParent(),
> > treeNodeBase);
> >                                         treeNodeBase = null;
> >                                 }
> >                         }
> >
> >                         /*
> >                          * // Add leaf reference to node if
> >                          * (!nodeLeafRef.containsKey(parent))
> > nodeLeafRef.put(parent, leaf);
> >                          * else { log.severe("Navigation has duplicate
> > node ids"); }
> >                          */
> >                         leaf = null;
> >                 }
> >         }
> > --
> > View this message in context:
> > http://www.nabble.com/TreeModelBase-HtmlTree-%3A-Java-heap-space-t1461744.html#a3991308
> > Sent from the MyFaces - Users forum at Nabble.com.
> >
> >
>

Re: TreeModelBase/HtmlTree : Java heap space

Posted by Andrew Robinson <an...@gmail.com>.
I have a static tree model with one root node. Then, I override the node
interface. Something like this:

  public class CustomTreeRootNode
    implements TreeNode, Serializable {
...
    public List getChildren() {
      MyObject parent = rootObject;
      List<MyObject> myChildren = parent.getChildren();
      List<TreeNode> nodes = new ArrayList<TreeNode>();
      for (MyObject child : myChildren)
      {
         nodes.add(new CustomTreeNode(child.getId()));
      }
    }
...
  }

  public class CustomRootNode
    implements TreeNode, Serializable {
    private String identifier;
    public CustomRootNode(String id) { this.identifier = id; }
...
    public List getChildren() {
      MyObject parent = myHashMap.get(this.identifier);
      List<MyObject> myChildren = parent.getChildren();
      List<TreeNode> nodes = new ArrayList<TreeNode>();
      for (MyObject child : myChildren)
      {
         nodes.add(new CustomTreeNode(child.getId()));
      }
    }
...
  }





On 4/19/06, sunilskulkarni <su...@yahoo.com> wrote:
>
>
> Thanks Sean, Andrew.
>
> I am trying to construct tree2 from following DB table. KaMenu object
> represents the following table.
> +-------+--------+------+------+--------+
> | child | parent | name | link | locale |
> +-------+--------+------+------+--------+
> |     1 |      1 | n1   | l1   |      0 |
> |     2 |      1 | n2   | l2   |      0 |
> |     3 |      1 | n3   | l3   |      0 |
> +-------+--------+------+------+--------+
>
> Here is my algorithm to construct the TreeNode for tree2. I tried server
> side tree2 without any help. It is recursive. nodeLeafRef is a hashmap
> that
> returns TreeNode for rootNode 1. When the function is called, it is filled
> with information about the root node. In this example nodeLeafRef.size()
> is
> 3, but I still get the Java heap space exception.
>
> Any help is really appreciated. Thank you.
>
> public void buildTreeData(KaMenu kaMenu) {
>
>                 //List list = kaMenuModel.findChildren(kaMenu.getChild());
>                 List list = (ArrayList)parentChild.get(kaMenu.getChild());
>                 // If the list is empty, then its a leaf else it is a node
>
>                 if(list != null)
>                 {
>                         int n = list.size();
>                         if (n > 0) {
>                                 // Add the parent to the Hashtable
>                                 TreeNode nodeRef = new
> TreeNodeBase("foo-folder",
> kaMenu.getName(),false);
>
>                                 if (!nodeLeafRef.containsKey(
> kaMenu.getChild())) {
>                                         nodeLeafRef.put(kaMenu.getChild(),
> nodeRef);
>                                 } else {
>                                         log.severe("Navigation has
> duplicate node ids");
>                                 }
>
>                                 // Check, if each child has its own
> children
>                                 Iterator itr = list.iterator();
>                                 for (int i = 0; i < n; i++) {
>                                         Integer child =
> (Integer)itr.next();
>                                         KaMenu kaChildMenu = (KaMenu)
> nodeInfo.get(child);
>                                         buildTreeData(kaChildMenu);
>                                 }
>
>                                 // Add the node to its parent
>                                 // if(parent != 1){
>                                 TreeNode treeNode = (TreeNode)
> nodeLeafRef.get(kaMenu.getParent());
>                                 if (treeNode != null) {
>                                         nodeRef.getChildren
> ().add(nodeRef);
>                                         nodeLeafRef.remove(
> kaMenu.getParent());
>                                         nodeLeafRef.put(kaMenu.getParent(),
> treeNode);
>                                 }
>                                 treeNode = null;
>                                 nodeRef = null;
>                                 // }
>                         }
>                 } else {
>                         TreeNodeBase leaf = new TreeNodeBase("document",
> kaMenu.getName(),
>                                         kaMenu.getLink(), true);
>
>                         // Add the leaf to its node
>                         TreeNode treeNode = null;
>                         TreeNodeBase treeNodeBase = null;
>
>                         // If the parent is root node(1), then add the
> TreeNodeBase
>                         if (kaMenu.getParent().intValue() != 1) {
>                                 treeNode = (TreeNode) nodeLeafRef.get(
> kaMenu.getParent());
>                                 if (treeNode != null) {
>                                         treeNode.getChildren().add(leaf);
>                                         nodeLeafRef.remove(
> kaMenu.getParent());
>                                         nodeLeafRef.put(kaMenu.getParent(),
> treeNode);
>                                         treeNode = null;
>                                 }
>                         } else {
>                                 treeNodeBase = (TreeNodeBase)
> nodeLeafRef.get(kaMenu.getParent());
>                                 if (treeNodeBase != null) {
>                                         treeNodeBase.getChildren
> ().add(leaf);
>                                         nodeLeafRef.remove(
> kaMenu.getParent());
>                                         nodeLeafRef.put(kaMenu.getParent(),
> treeNodeBase);
>                                         treeNodeBase = null;
>                                 }
>                         }
>
>                         /*
>                          * // Add leaf reference to node if
>                          * (!nodeLeafRef.containsKey(parent))
> nodeLeafRef.put(parent, leaf);
>                          * else { log.severe("Navigation has duplicate
> node ids"); }
>                          */
>                         leaf = null;
>                 }
>         }
> --
> View this message in context:
> http://www.nabble.com/TreeModelBase-HtmlTree-%3A-Java-heap-space-t1461744.html#a3991308
> Sent from the MyFaces - Users forum at Nabble.com.
>
>

Re: TreeModelBase/HtmlTree : Java heap space

Posted by sunilskulkarni <su...@yahoo.com>.
Thanks Sean, Andrew.

I am trying to construct tree2 from following DB table. KaMenu object
represents the following table.
+-------+--------+------+------+--------+
| child | parent | name | link | locale |
+-------+--------+------+------+--------+
|     1 |      1 | n1   | l1   |      0 |
|     2 |      1 | n2   | l2   |      0 |
|     3 |      1 | n3   | l3   |      0 |
+-------+--------+------+------+--------+

Here is my algorithm to construct the TreeNode for tree2. I tried server
side tree2 without any help. It is recursive. nodeLeafRef is a hashmap that
returns TreeNode for rootNode 1. When the function is called, it is filled
with information about the root node. In this example nodeLeafRef.size() is
3, but I still get the Java heap space exception.

Any help is really appreciated. Thank you.

public void buildTreeData(KaMenu kaMenu) {
		
		//List list = kaMenuModel.findChildren(kaMenu.getChild());
		List list = (ArrayList)parentChild.get(kaMenu.getChild());
		// If the list is empty, then its a leaf else it is a node

		if(list != null)
		{
			int n = list.size();
			if (n > 0) {
				// Add the parent to the Hashtable
				TreeNode nodeRef = new TreeNodeBase("foo-folder",
kaMenu.getName(),false);
				
				if (!nodeLeafRef.containsKey(kaMenu.getChild())) {
					nodeLeafRef.put(kaMenu.getChild(), nodeRef);
				} else {
					log.severe("Navigation has duplicate node ids");
				}
	
				// Check, if each child has its own children
				Iterator itr = list.iterator();
				for (int i = 0; i < n; i++) {
					Integer child = (Integer)itr.next();
					KaMenu kaChildMenu = (KaMenu) nodeInfo.get(child);
					buildTreeData(kaChildMenu);
				}
	
				// Add the node to its parent
				// if(parent != 1){
				TreeNode treeNode = (TreeNode) nodeLeafRef.get(kaMenu.getParent());
				if (treeNode != null) {
					nodeRef.getChildren().add(nodeRef);
					nodeLeafRef.remove(kaMenu.getParent());
					nodeLeafRef.put(kaMenu.getParent(), treeNode);
				}
				treeNode = null;
				nodeRef = null;
				// }
			}
		} else {
			TreeNodeBase leaf = new TreeNodeBase("document", kaMenu.getName(),
					kaMenu.getLink(), true);

			// Add the leaf to its node
			TreeNode treeNode = null;
			TreeNodeBase treeNodeBase = null;

			// If the parent is root node(1), then add the TreeNodeBase
			if (kaMenu.getParent().intValue() != 1) {
				treeNode = (TreeNode) nodeLeafRef.get(kaMenu.getParent());
				if (treeNode != null) {
					treeNode.getChildren().add(leaf);
					nodeLeafRef.remove(kaMenu.getParent());
					nodeLeafRef.put(kaMenu.getParent(), treeNode);
					treeNode = null;
				}
			} else {
				treeNodeBase = (TreeNodeBase) nodeLeafRef.get(kaMenu.getParent());
				if (treeNodeBase != null) {
					treeNodeBase.getChildren().add(leaf);
					nodeLeafRef.remove(kaMenu.getParent());
					nodeLeafRef.put(kaMenu.getParent(), treeNodeBase);
					treeNodeBase = null;
				}
			}

			/*
			 * // Add leaf reference to node if
			 * (!nodeLeafRef.containsKey(parent)) nodeLeafRef.put(parent, leaf);
			 * else { log.severe("Navigation has duplicate node ids"); }
			 */
			leaf = null;
		}
	}
--
View this message in context: http://www.nabble.com/TreeModelBase-HtmlTree-%3A-Java-heap-space-t1461744.html#a3991308
Sent from the MyFaces - Users forum at Nabble.com.


Re: TreeModelBase/HtmlTree : Java heap space

Posted by Andrew Robinson <an...@gmail.com>.
I had a similar problem with a tree with ~100,000 nodes. I have moved to
tree2. The great thing about tree2, is it operates 100% by node IDs, not
nodes. Therefore, I was able to implement the flyweight pattern.

I have a tree node as a root that contains no children, but instead a
reference to my root object. When asked for the children, I dynamically
build children nodes for each child object, but do not store the nodes. For
each of the nodes, I also do not build the children at construction, but
instead when getChildren() is called, and once again do not store the
children in a variable. All this is done by my object IDs. I keep a hash map
of my objects (HashMap<String,MyObject> where the string is the ID), so I
can easily access them.

In this way, I only have current node + number of children nodes created in
the Java heap at one time.

If you go with this, just make sure you don't save the children, and make
sure you implement server side node expansion.

-Andrew

On 4/18/06, Sean Schofield <se...@gmail.com> wrote:
>
> You are probably trying to load way too much data into the tree.  How
> much data are you talking about?  I would suggest looking at the
> archives for discussions on dynamic trees.  You can customize tree2
> model to use a database.
>
> Sean
>
> On 4/17/06, Sunil Kulkarni <su...@yahoo.com> wrote:
> > Hi,
> >
> >  I am using MyFaces 1.1.1/Tomcat 5.5.12. I am using sample
> TreeBacker.java
> > file to render a tree.
> >
> >  If I don't get data from Database, then everything works fine. But, if
> I am
> > trying to get Tree information from database and then construct
> 'TreeNode'
> > then I get the following exception.
> >
> >  There is similar issue reported in users list with subject "Java heap
> > space: Error with Tree2", but I did not find any answer to it.
> >
> >  Please let me know, if you know the solution.
> >
> >  Thanks.
> >
> >
> >  javax.faces.FacesException: Caused by: javax.servlet.ServletException:
> > ServletException in '/common/login_menu.jsp': Java heap space
> >      at
> > org.apache.jasper.runtime.PageContextImpl.doHandlePageException(
> PageContextImpl.java:848)
> >      at
> > org.apache.jasper.runtime.PageContextImpl.handlePageException(
> PageContextImpl.java:781)
> >      at
> > org.apache.jsp.template.logintemplate_jsp._jspService
> (logintemplate_jsp.java:78)
> >      at
> > org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
> >      at
> > javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
> >      at
> > org.apache.jasper.servlet.JspServletWrapper.service(
> JspServletWrapper.java:322)
> >      at
> > org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
> >      at
> > org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
> >      at
> > javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
> >      at
> > org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
> ApplicationFilterChain.java:252)
> >      at
> > org.apache.catalina.core.ApplicationFilterChain.doFilter(
> ApplicationFilterChain.java:173)
> >      at
> > org.apache.catalina.core.ApplicationDispatcher.invoke(
> ApplicationDispatcher.java:672)
> >      at
> > org.apache.catalina.core.ApplicationDispatcher.processRequest(
> ApplicationDispatcher.java:463)
> >      at
> > org.apache.catalina.core.ApplicationDispatcher.doForward(
> ApplicationDispatcher.java:398)
> >      at
> > org.apache.catalina.core.ApplicationDispatcher.forward(
> ApplicationDispatcher.java:301)
> >      at
> > org.apache.myfaces.context.servlet.ServletExternalContextImpl.dispatch(
> ServletExternalContextImpl.java:415)
> >
>