Sign in to follow this  

[java] Few Java questions

This topic is 3630 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ok first of all i need to free some rem, every time i access my data files for reading and/or writing it stores the file in ram, over and over again... is there a way to not keep building up ram?? second my game is going to be a fps, and i want to know how to keep the mouse in the center of the game screen and detect if it tries to move to change camera view and still have mouse centered, with a cross hair as mouse courser.

Share this post


Link to post
Share on other sites
Memory will be automatically freed when it is no longer referenced. As long as you don't accidentally keep references to old objects around (tricky, but not impossible to do, usually by forgetting to unregister event handlers and other listeners).

For a fps, it'll depend on what you're using to get your input. If you're using LWJGL then it'll give you mouse deltas in the Input class, so you don't need to do anything fancy. If you're using Java2D or Jogl you'll probably have to resort to using java.awt.Robot to reposition the cursor back to the center of the screen every frame (as well as hiding it and drawing your own crosshair).

Share this post


Link to post
Share on other sites
You can use System.gc() to force garbage collection, but don't use during your game for example because garbage collection is usually a slow process. You can call it when going from the title screen/GUI to the game(assign null to every object/resource of the GUI and call System.gc() and then load the game resources). Not sure if it brings any advantages because the garbage collector may run at an unpredictable time, it only frees the memory as you would do in a native application.

Please correct me if I'm wrong.

Share this post


Link to post
Share on other sites
System.gc() only suggests that now might be a good time to garbage collect, it doesn't actually have to do anything. The only time IIRC that the VM is required to do a garbage collect is before throwing an OutOfMemory exception.

The gc has come a long way - if you're running on a 1.5 or later VM then garbage collection isn't going to be a problem unless you're generating thousands of new objects per frame (and you might even be able to get away with that).

Share this post


Link to post
Share on other sites

This topic is 3630 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this