Reputation: 901
I have searched and read many posts on limiting memories on java apps on this site. I know that it is not recommended, but if I really need to limit the maximum RAM used on my application. How do I do so?
My current application is taken from Oracle tutorial
site. Knock Knock Server I kept the server running, kept sending message from client to server and monitored the memory usage using the Task Manager
. I noticed the memory kept increasing every time I sends a message.
I learnt about the System.gc();
and Runtime.gc();
as well and I was hoping it might reduce the memory consumption, unfortunately it didn't. As expected.
ServerSocket serverSocket = new ServerSocket(portNumber);
Socket clientSocket = serverSocket.accept();
PrintWriter out =
new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
Can any of those commands use the gc()
command? I also tried the -Xmx and -Xms hoping it would work as well.
Lastly, if creating a simple java server isn't a good idea. What should I do?
EDIT: I am using -Xms9m
and -Xmx10m
. And I wish to keep the app running at 10MB, but when it first launched. It can go from 14MB - 17MB, from there it can continue increasing beyond 20MB.
Upvotes: 3
Views: 259
Reputation: 9028
First, do not monitor VM mem usage by Task Manager. Use GC logging to get exact memory usage of the VM. If you see any increase of memory consumption in the GC log, then you have a problem.
Second, Make sure you are properly reusing the Socket/Connection. Not closing the connections could be the cause of that memory increase - if there is any.
Upvotes: 0
Reputation:
@Melvin Lai: make sure you do M not MB. This answered my question and work well.
Upvotes: 0
Reputation: 1620
If you want to limit memory for jvm (not the heap size ) ulimit -v
To get an idea of the difference between jvm and heap memory, take a look at this excellent article Taking a Closer Look at Sizing the Java Process
The NativeHeap can be increased or decreased by -XX:MaxDirectMemorySize=256M (default is 128)
Another interesting read: Java Performance Tuning
Upvotes: 1