i have developed chatting application using threads. but when i start my application system acts very slow and sometime exception occur that heap is full. i want to increase heap size of Java Virtual Machine. how can i do it?
Just increase the heap size of the JVM. All Java applications, even simple ones, consume a lot of memory. Take a look at this article explaining in detail how to increase the amount of memory available for your application; basically you'll need to pass a couple of extra parameters to the JVM when you invoke the java command, like this:
java -Xms64m -Xmx256m HelloWorld
In the above command, I'm saying that the HelloWorld program should have an initial heap size of 64MB and a maximum of 256MB. Try with these values and fiddle a bit with them until you find a combination of values that works for your application.
You can increase heap size, but your larger issue is "Why did I get that exception?" Increasing the heap size will only delay the inevitable if your application is not cleaning up after itself properly.
You need to instrument your application with Visual VM and see what's going on. That will give you more of a path forward than simply increasing the heap size.
Add -Xmx100m to the command when you start your app. This will give you 100 MB heap (you can change the number).
It sounds strange that a chat app would required more than the standard heap size...
Blockquote
Large server applications often experience two problems with these
defaults. One is slow startup, because the initial heap is small and
must be resized over many major collections. A more pressing problem
is that the default maximum heap size is unreasonably small for most
server applications.
Blockquote
You could start your program via command prompt with these parameters
java -Xms64m -Xmx256m chat_program.
Here Xms64m = 64mb initial heap size
and Xmx256m = 256mb maximum heap size
Related
I can set the max memory as 1000 and not more than that, if I set the memory more than that, it throws the following error.
Error occurred during initialization of VM
Could not reserve enough space for object heap
Could not create the Java virtual machine.
My question is, why jvm looks for the max memory at startup?
Thanks in advance.
The Sun JVM needs a contiguous area of memory for its heap. Using the tool vmmap from the Sysinternals suite you can examine the memory layout of the Java process exactly. To do that, write a simple Java program like this:
public class MemoryLayout {
public static void main(String[] args) throws java.io.IOException {
System.in.read();
}
}
Compile that program and run it using the large heap settings
javac MemoryLayout.java
java -Xmx1000m -Xms1000m MemoryLayout
Then, start vmmap, select the java process and look for the yellow memory region whose size is larger than 1000000k. This is the JVM heap. Look further below, and you will eventually find a purple row indicating that there is a DLL file mapped. This DLL file prevents your JVM heap from growing bigger.
If you know what you are doing, you can then rebase that DLL, so it will be loaded at a different address. Microsoft provides a tool called rebase.exe as part of the Microsoft Platform SDK (I have version 5.2.3790.1830).
There are two command line parameters that directly control the size of the (normal) heap:
-Xmx<nnn> sets the maximum heap size
-Xms<nnn> sets the initial heap size
In both cases <nnn> is a number of bytes, with a k or m on the end to indicate kilobytes and megabytes respectively. The initial size gives the heap size allocated when the JVM starts, and the maximum size puts a limit on how big it can grow. (But the JVM also allocates memory for buffers, the "permgen" heap, stacks and other things ... in addition to the normal heap.)
It is not clear what options you are actually giving. (A value of 1000 doesn't make any sense. The -Xmx size has to be more than 2 megabytes and the -Xms size has to be more than 1 megabytes; see this page.)
There are advantages and disadvantages in making the initial heap size smaller than the maximum heap size; e.g. -Xms100m -Xmx1000m. But there is no point making the maximum heap size larger than the amount of virtual memory your machine can allocate to the JVM.
why jvm looks for the max memory at startup.
It wants to make sure that it can eventually allocate the maximum amount which you said it could have.
Why do you need to set a higher maximum then your machine actually supports?
Answer: It would make JVM configuration easier, if you could just set it to basically unlimited, especially if you deployed to different machines. This was possible in earlier versions, but for the current Sun JVM, you have to figure out a "proper" value for every machine. Hopefully, there will be more clever/automatic memory settings in the future.
As elaborated here for implementation reasons (basically it makes performance faster and that is their priority) the JVM requires contiguous memory addressing, so it has to establish that it has that at startup, otherwise it might to be available later.
The fact of the matter is that the JVM in many ways is a server-side oriented technology. That is where Java is popular so that is what gets the development attention.
If you use a 64-bit JVM on a 64-bit OS you won't have this problem. This is only a problem on 32-bit OSes.
I have a Java program that is launched by a batch file with a line like this:
javaw -Xms64m -Xmx1024m com.acme.MyProgram
However, on some computers the program will not launch and displays the following message:
Could not reserve enough space for object heap. Could not create the Java virtual machine.
The problem seems to be the the maximum size of the memory allocation pool is larger than the computer can handle. Reducing the maximum size of the memory allocation pool from 1024m to 512m seems to resolve the problem.
Is there a way I can determine how much memory is available on the computer ahead of time (from within the batch file) and determine whether to use -Xmx1024m or -Xmx512m in the batch file invocation? Note that this batch file only needs to work on Windows.
Actually the Java VM already does something similar. If you do not specify -Xms or -Xmx, then these values are inferred from the amount of physical memory on the machine. Or at least so says this page.
You could set -Xms to the minimum heap size which makes your application useful, and let Java determine a proper value for -Xmx.
You could take a look at this page for some answers: Get JVM to grow memory demand as needed up to size of VM limit?
If your program functions correctly with a max heap of 512m, I would use that value.
That said I will also check to see if there is a way to do what you're asking as that is an interesting question.
You could execute from your batch file, check the error level on exit and restart at a lower memory if it failed. I'm not sure the error level would work--if it doesn't you could also check how long it took the program to execute... any thing less than 10sec would be a giveaway.
Just a couple comments though--
If you know it doesn't NEED more than 512, you should run a test to ensure that 1024 actually helps. Larger heaps can often make your GC pauses longer and do little else.
If you're pretty sure you'll use a certain amount of ram (say, the heap will easily fill the 512 you are allocating), you should probably set the min to that number. Setting both the min and max to 512 is good if your program allocates a bunch of stuff but is not situational (always uses about the same amount of ram)
I'm using ASANT to run a xml file which points to a NARS.jar file. (i do not have the project file of the NARS.jar)
I'm getting "java.lang.OutOfMemoryError: Java heap space.
I used VisualVM to look at the heap while running the NARS.jar, and it says that it max uses 50 MB of the heapspace.
I've set the initial and max size of heapspace to 512 MB.
Does anyone have an ide of what could be wrong?
I got 1 GB physical Memory and created a 5 GB pagefile (for test purpose).
Thanks in advance.
Your app may be trying to allocate memory that exceeds your 512m limit, thus you see an outofmemory error even though only 50m is being used. To test this, I would set:
-Xms512m -Xmx1024m
And see what happens. I would also try a smaller test file, say 1g. Keep reducing the file size until you stop seeing the error. If you succeed, then the trouble is that what you're trying to do and the way you're trying to do it takes too much memory. Time to look for an alternate approach.
Are you forking the process when running the NARS.jar file? Setting ANT_OPTS will only have effect on the VM running the ant system. If you use the java task to start/fork an additional VM process, the ANT_OPTS settings will not be inherited.
If this is the case, set either fork="false" in the java task (if you are not using any other options, which require fork to be enabled), or set maxmemory="512m".
XML files are notorious memory hogs since the DOM representation can often require ten times their size on disk.
My guess is that this is where you hit the limit. Is there a stack trace with the out of memory exception?
I'm simulating a overload of a server and I'm getting this error:
java.lang.OutOfMemoryError: unable to create new native thread
I've read in this page http://activemq.apache.org/javalangoutofmemory.html, that I can increase the memory size. But how do I do that? Which file I need to modify,? I tried to pass the arguments by the bin/activemq script but no luck.
Your case corresponds to massive number of threads.
There are 3 ways to solve it:
reduce number of threads (i.e., -Dorg.apache.activemq.UseDedicatedTaskRunner=false in the document)
reduce per-thread stack size by -Xss option (default values: 320 KiB for 32-bit Java on Win/Linux, 1024 KiB for 64-bit Java on Win/Linux, see doc)
reduce (not extend) heap size -Xmx option to make a room for per-thread stacks (512 MiB by default in ActiveMQ script)
Note: If stack or heap is too small, it must cause another OutOfMemoryError.
You can specify them using ACTIVEMQ_OPTS shell variable (in UNIX).
For example, run ActiveMQ as
ACTIVEMQ_OPTS=-Xss160k bin/activemq
Check here
Specify the -Xmx argument to the VM that is running the ActiveMQ - Tomcat, for example.
You could assign the Java virtual machine more memory using the -Xmx command argument.
Eg. java -Xmx512M MyClass
We were running into this issue on a Linux (RedHat Enterprise 5) system and discovered that on this build the nprocs ulimit in /etc/security/limits.conf actually controls the number of threads a user can spawn.
You can view this limit using the ulimit -a command.
Out of the box this was set to a soft limit of 100 and a hard limit of 150, which is woefully short of the number of threads necessary to run a modern App Server.
We removed this limit altogether and it solved this issue for us.
This doesn't look like you are running out of heap space, so don't increase that (the -Xmx option). Instead, your application is running out of process memory and decreasing the heap space will free up process memory for native use. The question is, why you are using so much process memory? If you don't use JNI, you probably have created too many threads, and habe's post has explained how to do fix that.
I have one main class that contains 5 buttons each link to a program/package. Each package runs a jmf program that capture images from a webcam and it also loads about 15 images from file.
The 1st program to load(regardless of which button i press) always runs correctly. But When i run a program after the 1st program ends, java.lang.OutOfMemoryError: java heap space occurs.
Im not sure if java can't handle all of our images or if it has something to do with jmf image capture.
Maybe you should give more memory to your JVM (-Xmx512m on the command line could be a good start),
then, if it solves the problem, investigate why your programs consumes so much memory.
The use of sun diagnostic tools like jvisualvm could be helpful.
Increase the Java maximum memory and re-rerun. If you still see OOM's, you may have a leak. To increase the max memory, append -Xmx<new heap size>m to your command line.
Example:
java -Xmx1024m Foo
How much memory are you giving to your JVM? You can give it more using the following: -Xmx1024m (for 1GB, adjust as necessary)
This assumes that you don't have some memory leak in your program. I don't know anything about JMF, this is just general advice for Out of Memory errors.
JVMs run with a limited amount of maximum memory available to them. This is a little counterintuitive and trips a lot of people up (I can't think of many similar environments).
You can increase the max memory the JVM takes by specifying
java -Xmx128m ...
or similar. If you know in advance that you're going to consume that amount of memory, use
java -Xms128m ...
to specify the memory that the JVM will allocate at startup. Note the -Xms vs -Xmx !
Try to check, if you still have some references around which prevent the first package/program to be garbage-collected.
When the launcher has detected that the first program has ended, set all references to the first program and maybe objects retrieved from it to NULL to allow the JVM to reclaim the memory again and have it ready for the second launch.
Java uses 64 MByte heap space by default. An alternative to the other suggestions (increasing heap space to 512M or 1024M) is to start separate JVMs for the controller and the 5 applications. Then if one of your JMF applications crashes (due to insufficient memory), the controller and the other apps are still running.
(this will only work if the applications and the controller are completely decoupled - otherwise, just increase the heap size and dispose all media as soon as you don't need it anymore to prevent from memory leaks)