• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

What is the good practice of java heap ?

 
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the good practice of java heap ? How does Java performance monitoring tools visualVM profiler application help ?
 
Marshal
Posts: 67444
257
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Joe wrote:What is the good practice of java heap ?

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.

How does Java performance monitoring tools visualVM profiler application help ?

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
 
Ranch Hand
Posts: 4714
9
Scala Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
great advice campbell
 
Sheriff
Posts: 14756
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:there is a very good chance that your guess about performance will be wrong


Programmers are notoriously bad at performance tuning using their gut feelings/intuition. Don't be evil; use a profiler.

"Premature optimization is the root of all evil."—Donald Knuth
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So we no need to worry about the java heap size and no need to invoke finalize() as the system will do this automatically ?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should never call finalize() yourself. This method is called by the JVM when the object is about to be garbage collected. You don't even need to ever write a finalizer method for your class, unless you have a really, really good reason to do so.

I agree with the others - the garbage collector in the JVM is a very sophisticated piece of software, and unless you really have a very good reason and you understand exactly what you are doing, it's best to not interfere with it at all. Most things you do to "optimize" garbage collection will be counter-productive, unless you really deeply understand how it works internally. You normally only will need to pay attention to garbage collection tuning if your software uses a lot of memory, or runs on a server and needs to be able to handle thousands of users at the same time. Oracle has a Garbage Collection Tuning Guide. But this is really advanced stuff.
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the default value for Java Heap Size ? I saw from this post  where the author suggest Heap = -Xms512m -Xmx1024m for small to medium Java Aplication.Do we really need to set it or left it as default ?
 
Rancher
Posts: 4450
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the default is (if I remember correctly) 1Gb, or 1/4 (something like that) of your available memory.  Whichever is the smaller.

So that heap declaration (apart from the min size) is a bit pointless.
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And from another post

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


We need to set Java Heap Size or left it as default ???
 
Dave Tolls
Rancher
Posts: 4450
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you have a functioning (or largely functioning) system then that's when you do all the performance and load testing.
That's when you figure out the sort of changes you need to tweak JVM settings, or code changes for problem areas.

Prior to that you are largely guessing.
 
Campbell Ritchie
Marshal
Posts: 67444
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:Well, the default is . . .

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.
 
Campbell Ritchie
Marshal
Posts: 67444
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:. . . if I remember correctly . . .

I think you do remember correctly for current versions.
 
Campbell Ritchie
Marshal
Posts: 67444
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see that Java8 uses different definitions with throughput targets rather than heap size recommended.

I still think my initial advice was correct: leave well alone.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Joe wrote:We need to set Java Heap Size or left it as default ???


Leave the settings alone unless you have a specific reason to change them. Don't just use certain settings because some vague recommendation.

How much memory your Java application needs, depends heavily on what the application is doing. If the application is loading a lot of data and processing it in memory, then it will need more memory. General recommendations like "-Xms512m -Xmx1024m" are meaningless.

This is good advice in general when programming: only do things / write code when you understand what you are doing, don't do things / add code just because it's "best practice" when you don't know what it means and why you are doing it. Blindly following "best practices" without understanding leads to cargo cult programming.
 
John Joe
Ranch Hand
Posts: 570
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the advice. Appreciated !
 
Randall Twede
Ranch Hand
Posts: 4714
9
Scala Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got a ran out of heap message once. the correct thing in that case was to correct my code so it didn't cause that problem
 
Well THAT's new! Comfort me, reliable tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!