Java Game Server taking up too much memory!
I have a开发者_StackOverflow中文版 Java-based command-line server running that hosts the highscores for a game I made on my website. It works efficiently and fast. However, it takes up about 200MB of RAM! I have tried everything from manually nulling out everything to calling System.gc()
I am starting to suspect that this might have something to do with the input stream and output stream objects that I use from the Socket connection. I have noticed that when I first run the program, it takes up a normal amount of RAM. Then once it gets a connection, it jumps to 100MB and keeps getting higher for each connection.
EDIT: In one of my classes, I hold all the names, scores, and timestamp in 3 different ArrayLists. However, a thorough examination using jhat and jmap showed that combined they only use about 5MB of RAM.
If this is too vague for anyone to answer, ask and I will gladly give the source code.
You may have a leak caused by retaining references to objects beyond their useful life.
I suggest you get a profiler and use it to investigate. A good and free place start is the VisualVM program that distributes with Java 6.
A profiler is a separate program that either attaches to the JVM or hosts a JVM for your program and monitors the execution of your program. It can track object allocations and code execution, either statistically or by "instrumenting" the executing code. It will show you if objects are being allocated and not released, and can show what objects they are and where they were allocated (among many other useful things).
I use jProfiler, which is commercial (but well worth it for a professional). Last time I searched there were several good quality profilers available for free (at least for personal use). VisualVM has basic, but useful, profiling capabilities, and I would begin there (on Windows you can find it in the JDK bin directory; I presume the same is true for Linux and Mac).
Try using something like jmap followed by jhat to see how your memory is actually being used.
Sun's (Oracle's) JVM never releases memory back to the OS, so it's not surprising you see monotonic increase in memory usage.
精彩评论