grizz
Just dropped in
Offline
Posts: 7
|
 |
« on: 18. January 2011, 03:40:41 pm » |
|
I have a simple program that draws a couple boxes and uses PickingLibrary to display what box is clicked. But if I click fairly rapidly for a couple seconds I run out of heap space. Is there a better way to handle this than implementing a timer and only allowing users to click every so often?
|
|
|
|
|
Logged
|
|
|
|
Marvin Fröhlich
Xith Lord
Administrator
Guru
   
Online
Posts: 4381
May the 4th, be with you...
|
 |
« Reply #1 on: 18. January 2011, 05:46:14 pm » |
|
Are you drawing in 2D or 3D?
|
|
|
|
|
Logged
|
|
|
|
grizz
Just dropped in
Offline
Posts: 7
|
 |
« Reply #2 on: 18. January 2011, 05:47:11 pm » |
|
3D
|
|
|
|
|
Logged
|
|
|
|
Marvin Fröhlich
Xith Lord
Administrator
Guru
   
Online
Posts: 4381
May the 4th, be with you...
|
 |
« Reply #3 on: 18. January 2011, 06:04:41 pm » |
|
Are you using the PickingLibrary from within the rendering thread? You can do this by overriding e.g. the prepareNextFrame() method of RenderLoop or by implementing an Updatable or ScheduledOperation and adding it to the Updater.
Marvin
|
|
|
|
|
Logged
|
|
|
|
grizz
Just dropped in
Offline
Posts: 7
|
 |
« Reply #4 on: 18. January 2011, 09:10:11 pm » |
|
I believe so, I'm extending InputAdapterRenderLoop and unless I'm missing something one of the super classes adds the input adapters to the updater. Here's the code if that would help. import org.jagatoo.input.InputSystem; import org.jagatoo.input.InputSystemException; import org.jagatoo.input.devices.Mouse; import org.jagatoo.input.devices.components.Key; import org.jagatoo.input.devices.components.MouseButton; import org.jagatoo.input.devices.components.MouseButtons; import org.jagatoo.input.events.KeyReleasedEvent; import org.jagatoo.input.events.MouseButtonPressedEvent; import org.jagatoo.input.events.MouseButtonReleasedEvent; import org.jagatoo.input.events.MouseMovedEvent; import org.jagatoo.input.events.MouseWheelEvent; import org.openmali.vecmath2.Point3f; import org.openmali.vecmath2.Tuple3f; import org.openmali.vecmath2.Vector3f; import org.xith3d.base.Xith3DEnvironment; import org.xith3d.loaders.texture.TextureLoader; import org.xith3d.loop.CanvasFPSListener; import org.xith3d.loop.InputAdapterRenderLoop; import org.xith3d.loop.Updater; import org.xith3d.loop.UpdatingThread.TimingMode; import org.xith3d.loop.opscheduler.Animator; import org.xith3d.loop.opscheduler.OperationScheduler; import org.xith3d.picking.NearestPickListener; import org.xith3d.picking.PickResult; import org.xith3d.picking.PickingLibrary; import org.xith3d.render.Canvas3D; import org.xith3d.render.Canvas3DFactory; import org.xith3d.scenegraph.BranchGroup; import org.xith3d.scenegraph.GroupNode; import org.xith3d.scenegraph.Texture; import org.xith3d.scenegraph.primitives.Box; import org.xith3d.scenegraph.primitives.Cube;
public class Boxes extends InputAdapterRenderLoop implements NearestPickListener{ private GroupNode pickGroup; private Canvas3D canvas; Xith3DEnvironment env; Texture tex; boolean leftButtonPressed = false; boolean rightButtonPressed = false; private BranchGroup createScene(){
BranchGroup bg = new BranchGroup(); for(int i=0;i<100;i++){ for(int j=0;j<100;j++){ int k = (i+1)*(j+1); String name = "Box #" + Integer.toString(k); Box c = new Box((i-50)*0.5f,(j-50)*0.5f,0.0f,0.5f,0.5f,0.5f, tex); c.setName(name); bg.addChild(c); } } try { InputSystem.getInstance().getMouse().setAbsolute(false); } catch (InputSystemException e) { // TODO Auto-generated catch block e.printStackTrace(); } return bg; }
public Boxes() { super(); tex = TextureLoader.getInstance().getTexture("brick.png"); env = new Xith3DEnvironment(this); canvas = Canvas3DFactory.createWindowed(800, 600, "My empty scene"); env.addCanvas(canvas);
try { InputSystem.getInstance().registerNewKeyboardAndMouse( canvas.getPeer()); } catch (InputSystemException e) { // TODO Auto-generated catch block e.printStackTrace(); } BranchGroup bg = createScene(); env.addPerspectiveBranch(bg); this.pickGroup = bg; this.addFPSListener(new CanvasFPSListener(canvas)); this.begin(); } public void onKeyReleased(KeyReleasedEvent e,Key key){ switch(key.getKeyID()){ case ESCAPE: this.end(); break; } } public static void main(String...args){ new Boxes(); }
public void onMouseButtonPressed(MouseButtonPressedEvent e,MouseButton button){ if(button == MouseButtons.LEFT_BUTTON){ leftButtonPressed = true; PickingLibrary.pickNearest(pickGroup,canvas,button,e.getX(),e.getY(),this); } else if(button == MouseButtons.RIGHT_BUTTON){ rightButtonPressed = true; float z = env.getView().getPosition().getZ(); Tuple3f eye = new Tuple3f(env.getView().getPosition().getX(),env.getView().getPosition().getY(),z+0.5f); env.getView().setPosition(eye); } } public void onMouseButtonReleased(MouseButtonReleasedEvent e, MouseButton button){ if(button == MouseButtons.LEFT_BUTTON){ leftButtonPressed = false; } else if(button == MouseButtons.RIGHT_BUTTON){ rightButtonPressed = false; } } public void onMouseMoved( MouseMovedEvent e, int x, int y, int dx, int dy ) { float angleX = env.getView().getFacingDirection().getX(); float angleY = env.getView().getFacingDirection().getY(); Vector3f rot = new Vector3f(angleX + (dx * .004f), angleY + (dy * -.004f),env.getView().getFacingDirection().getZ()); env.getView().setFacingDirection(rot); } @Override public boolean testIntersectionsInWorldSpaceForPicking() { // TODO Auto-generated method stub return false; }
@Override public void onPickingMissed(Object userObject, long pickTime) { System.out.println("none"); }
@Override public void onObjectPicked(PickResult nearest, Object userObject, long pickTime) { System.out.println("You picked "+nearest.getNode().getName()); }
public void onMouseWheelMoved( MouseWheelEvent e, int wheelDelta){ float z = env.getView().getPosition().getZ(); Tuple3f eye = new Tuple3f(env.getView().getPosition().getX(),env.getView().getPosition().getY(),z+0.5f*-wheelDelta); env.getView().setPosition(eye); } }
|
|
|
|
|
Logged
|
|
|
|
Marvin Fröhlich
Xith Lord
Administrator
Guru
   
Online
Posts: 4381
May the 4th, be with you...
|
 |
« Reply #5 on: 18. January 2011, 09:34:36 pm » |
|
Looks good so far.
The PickingLibrary itself doesn't allocate any objects. I don't see, how that could create an out of memory exception. Must be some side effect. Could you please post one of those exceptions? Maybe I can see something in it. But please note, that I cannot currently put too much time into it.
Marvin
|
|
|
|
|
Logged
|
|
|
|
grizz
Just dropped in
Offline
Posts: 7
|
 |
« Reply #6 on: 18. January 2011, 09:44:34 pm » |
|
java.lang.OutOfMemoryError: Java heap space at org.xith3d.picking.PickResult.<init>(PickResult.java:68) at org.xith3d.picking.PickPool.allocatePickResult(PickPool.java:259) at org.xith3d.picking.PickingLibrary.getPickCandidates(PickingLibrary.java:154) at org.xith3d.picking.PickingLibrary.getPickCandidates(PickingLibrary.java:191) at org.xith3d.picking.PickingLibrary.pickNearest(PickingLibrary.java:465) at org.xith3d.picking.PickingLibrary.pickNearest(PickingLibrary.java:528) at org.xith3d.picking.PickingLibrary.pickNearest(PickingLibrary.java:590) at org.xith3d.picking.PickingLibrary.pickNearest(PickingLibrary.java:628) at org.xith3d.picking.PickingLibrary.pickNearest(PickingLibrary.java:647) at Boxes.onMouseButtonPressed(Boxes.java:118) at org.jagatoo.input.devices.Mouse.fireOnMouseButtonPressed(Mouse.java:435) at org.jagatoo.input.impl.lwjgl.LWJGLMouse.collectOrFireEvents(LWJGLMouse.java:199) at org.jagatoo.input.impl.lwjgl.LWJGLMouse.updateMouse(LWJGLMouse.java:277) at org.jagatoo.input.devices.Mouse.update(Mouse.java:787) at org.jagatoo.input.InputSystem.updateMouses(InputSystem.java:1135) at org.jagatoo.input.InputSystem.update(InputSystem.java:1178) at org.xith3d.base.Xith3DEnvironment.updateInputSystem(Xith3DEnvironment.java:211) at org.xith3d.loop.RenderLoop.prepareNextFrame(RenderLoop.java:593) at org.xith3d.loop.RenderLoop.loopIteration(RenderLoop.java:638) at org.xith3d.loop.RenderLoop.update(RenderLoop.java:698) at org.xith3d.loop.UpdatingThread.nextIteration(UpdatingThread.java:487) at org.xith3d.loop.RenderLoop.nextIteration(RenderLoop.java:709) at org.xith3d.loop.RenderLoop.loop(RenderLoop.java:762) at org.xith3d.loop.UpdatingThread.run(UpdatingThread.java:540) at org.xith3d.loop.RenderLoop.run(RenderLoop.java:785) at org.xith3d.loop.RenderLoop.begin(RenderLoop.java:841) at org.xith3d.loop.RenderLoop.begin(RenderLoop.java:871) at Boxes.<init>(Boxes.java:94) at Boxes.main(Boxes.java:109)
and thanks for the help  edit: sorry wrong one.
|
|
|
|
« Last Edit: 18. January 2011, 09:46:17 pm by grizz »
|
Logged
|
|
|
|
Marvin Fröhlich
Xith Lord
Administrator
Guru
   
Online
Posts: 4381
May the 4th, be with you...
|
 |
« Reply #7 on: 19. January 2011, 08:52:23 pm » |
|
I had a look at it. And as long as there's no threading issue, I don't see, how this could happen.
Marvin
|
|
|
|
|
Logged
|
|
|
|
|
ChrisE
|
 |
« Reply #8 on: 19. January 2011, 09:13:53 pm » |
|
Grizz, I noticed that in your onMouseMove() method you are calling new() on a Vector3f. public void onMouseMoved( MouseMovedEvent e, int x, int y, int dx, int dy ) { float angleX = env.getView().getFacingDirection().getX(); float angleY = env.getView().getFacingDirection().getY(); Vector3f rot = new Vector3f(angleX + (dx * .004f), angleY + (dy * -.004f),env.getView().getFacingDirection().getZ()); env.getView().setFacingDirection(rot); } Could you replace that with something like this: public void onMouseMoved( MouseMovedEvent e, int x, int y, int dx, int dy ) { float angleX = env.getView().getFacingDirection().getX(); float angleY = env.getView().getFacingDirection().getY(); Vector3f rot = Vector3f.fromPool(angleX + (dx * .004f),angleY + (dy * -.004f),env.getView().getFacingDirection().getZ()); env.getView().setFacingDirection(rot); Vector3f.toPool(rot); } Does that help? -Chris Edit: ...my reasoning here being that possibly a *lot* of those are being allocated and blowing the heap before the GC runs.
|
|
|
|
|
Logged
|
|
|
|
Marvin Fröhlich
Xith Lord
Administrator
Guru
   
Online
Posts: 4381
May the 4th, be with you...
|
 |
« Reply #9 on: 19. January 2011, 10:03:20 pm » |
|
That shouldn't hurt too much. But it should still be replaced like that.
|
|
|
|
|
Logged
|
|
|
|
grizz
Just dropped in
Offline
Posts: 7
|
 |
« Reply #10 on: 20. January 2011, 02:20:17 am » |
|
Changing the onMouseMoved to ChrisE's code changes the error to java.lang.OutOfMemoryError: Java heap space at org.openmali.vecmath2.TupleNf.<init>(TupleNf.java:950) at org.openmali.vecmath2.Tuple3f.<init>(Tuple3f.java:528) at org.openmali.vecmath2.Vector3f.<init>(Vector3f.java:213) at org.openmali.vecmath2.Vector3f.<init>(Vector3f.java:247) at org.openmali.vecmath2.Vector3f.<init>(Vector3f.java:287) at org.xith3d.scenegraph.Transform3D.<init>(Transform3D.java:86) at org.xith3d.scenegraph.Transform3D.<init>(Transform3D.java:1258) at org.xith3d.picking.PickResult.<init>(PickResult.java:68) at org.xith3d.picking.PickPool.allocatePickResult(PickPool.java:259) at org.xith3d.picking.PickingLibrary.getPickCandidates(PickingLibrary.java:154) at org.xith3d.picking.PickingLibrary.getPickCandidates(PickingLibrary.java:191) at org.xith3d.picking.PickingLibrary.pickNearest(PickingLibrary.java:465) at org.xith3d.picking.PickingLibrary.pickNearest(PickingLibrary.java:528) at org.xith3d.picking.PickingLibrary.pickNearest(PickingLibrary.java:590) at org.xith3d.picking.PickingLibrary.pickNearest(PickingLibrary.java:628) at org.xith3d.picking.PickingLibrary.pickNearest(PickingLibrary.java:647) at Boxes.onMouseButtonPressed(Boxes.java:123) at org.jagatoo.input.devices.Mouse.fireOnMouseButtonPressed(Mouse.java:435) at org.jagatoo.input.impl.lwjgl.LWJGLMouse.collectOrFireEvents(LWJGLMouse.java:199) at org.jagatoo.input.impl.lwjgl.LWJGLMouse.updateMouse(LWJGLMouse.java:277) at org.jagatoo.input.devices.Mouse.update(Mouse.java:787) at org.jagatoo.input.InputSystem.updateMouses(InputSystem.java:1135) at org.jagatoo.input.InputSystem.update(InputSystem.java:1178) at org.xith3d.base.Xith3DEnvironment.updateInputSystem(Xith3DEnvironment.java:211) at org.xith3d.loop.RenderLoop.prepareNextFrame(RenderLoop.java:593) at org.xith3d.loop.RenderLoop.loopIteration(RenderLoop.java:638) at org.xith3d.loop.RenderLoop.update(RenderLoop.java:698) at org.xith3d.loop.UpdatingThread.nextIteration(UpdatingThread.java:487) at org.xith3d.loop.RenderLoop.nextIteration(RenderLoop.java:709) at org.xith3d.loop.RenderLoop.loop(RenderLoop.java:762) at org.xith3d.loop.UpdatingThread.run(UpdatingThread.java:540) at org.xith3d.loop.RenderLoop.run(RenderLoop.java:785)
Edit: Still getting the picking error while holding the mouse perfectly still.
|
|
|
|
« Last Edit: 20. January 2011, 03:10:48 am by grizz »
|
Logged
|
|
|
|
Marvin Fröhlich
Xith Lord
Administrator
Guru
   
Online
Posts: 4381
May the 4th, be with you...
|
 |
« Reply #11 on: 20. January 2011, 09:15:54 am » |
|
It's basically the same exception.
Can you please comment out the contents of the onMouseMoved() method adn try it again? If that works, please adda syso to it. I'm curious, if this method gets called for you, even if you don't move the mouse.
Marvin
|
|
|
|
|
Logged
|
|
|
|
grizz
Just dropped in
Offline
Posts: 7
|
 |
« Reply #12 on: 20. January 2011, 02:12:23 pm » |
|
Commenting out the onMouseMoved code still ends up getting the first error, it seems to be getting called only when its supposed to (when the mouse moves).
|
|
|
|
|
Logged
|
|
|
|
|
ChrisE
|
 |
« Reply #13 on: 20. January 2011, 04:03:31 pm » |
|
Grizz, another thought...
Could you change the max limit for i and j to something more on the order of 20? I'm curious if that might also be it.
(100*100 = 10,000 boxes, which depending on how things are done in the back end might be up to 60000 Shape3Ds as well)
Even better, don't try picking, but double the bounds for both box counts--let's see if we can provoke that heap problem before we hit the picking code.
Do you happen to know what heap size you are passing your JVM for the app?
-Chris
|
|
|
|
« Last Edit: 20. January 2011, 04:05:05 pm by ChrisE »
|
Logged
|
|
|
|
grizz
Just dropped in
Offline
Posts: 7
|
 |
« Reply #14 on: 20. January 2011, 04:37:55 pm » |
|
I'm running the JVM at 2GB. Lowering the number of boxes to even 20x20 does help but with enough picking it does still run out of memory. It can get to ~330x330 boxes before running out of memory with no picking.
|
|
|
|
|
Logged
|
|
|
|
|