Leave it alone. The heap is programmed to run automatically and works very well if allowed to do things for itself. Don't prompt garbage collection, or make references point to null to aid garbage collection. As long as you don't create memory leaks by maintaining unwanted references, or resource leaks by failing to close resources, the automatic heap usually manages memory much better than programmers.
John Joe wrote:What is the good practice of java heap ?
While learning, run visualVM and see what it tells you about memory and performance. In real life, don't do anything like that until somebody complains about slow execution. Then use visualVM or similar tools to find what is running slowly. Don't try to guess where the performance problem is. Unless you have done something obvious, e.g. bubble sort of a 10,000,000‑element array, there is a very good chance that your guess about performance will be wrong
How does Java performance monitoring tools visualVM profiler application help ?
Campbell Ritchie wrote:there is a very good chance that your guess about performance will be wrong
For 32-bit JVM, I usually do not recommend a Java Heap size high than 2 GB (-Xms2048m, -Xmx2048m) since you need enough memory for PermGen and native Heap for your Java EE applications and threads
Actually it differs from one version of Java® to another. You would need to look at the documentation for the java tool. Here is a link to an older version. If you look for -Xmxn, you will find out a bit more, and a useful link. You usually only need -Xmxn if you run out of heap space, in which case you will have an OutOfMemoryError(←link) thrown. Even if such an exception is thrown, it is just as likely that you have maintained inappropriate references to unwanted objects as that you need the increased space.
Dave Tolls wrote:Well, the default is . . .
I think you do remember correctly for current versions.
Dave Tolls wrote:. . . if I remember correctly . . .
John Joe wrote:We need to set Java Heap Size or left it as default ???