0

I'm new to profiling java applications and I was uncertain when I saw my small application could take up to 800MB memory twice as much as the whole operating system and around 450MB after some user interaction.

heap without heap limitation

So I've red some articles about how memory allocation and gc work and it seems the JVM takes all it can get and that most times it's not necessary to have that much memory for your application to run.

So I tested how much I can limit the heap without crashing my application and stopped testing at -Xmx32MB.

heap limited to 32MB

I was suprised that there was no recognizeable performance loss beside the fact the application uses nearly 30 times fewer memory.

So now I'm wondering...

(1) ... if there are any downsites of limiting the heap?

(2) ... if it's common the limit the heap or if it's recommandable to let the JVW decide how much memory it needs and want to use?

(3) ... what's the best way to find out what's the minimum memory size your application needs to run? (I'm just interested)

(4) ... should I experience performance losses when limiting the heap?

Thank you in advance!

trincot
  • 317,000
  • 35
  • 244
  • 286
cmtjk
  • 359
  • 2
  • 12

3 Answers3

1

Very brief answer to 1 and 4: Yes. Very brief answer to 2: No.

Your garbage collector will have to do more work which is not good for performance. 3rd question is interesting. Are you running in something embedded or limited? Is it a huge multithreaded application? Does your application require more than the RAM available? If you answer yes to any of these questions run the profiler but rather than play with different memory allocations, try to see where is your code eating up your ram? Are you using fancy objects (e.g List) where you could be using native datatypes (e.g byte[])?

Josep Valls
  • 5,483
  • 2
  • 33
  • 67
  • These questions just came to my mind after profiling my application because it used to much memory in my opinion and I had no idea how the JVM allocates memory. The high memory usage occurs because my application has to compare a lot of strings and generates ~60000 `char[]`. I was using `LinkedList`s and replaced it with `ArrayList`s because there's no removing, adding or searching and they seem to need lesser memory. But there's no need at the moment. I'm just interested. – cmtjk Sep 03 '15 at 20:35
  • From a practical perspective looks like you have already solved your issues. If you are curious read about garbage collection. This is a process that takes time so limiting your memory may be harmful to your performance if it is happening more often than necessary. – Josep Valls Sep 03 '15 at 20:38
  • Thank you very much for your answers. This was very helpful! – cmtjk Sep 03 '15 at 20:43
1

1) yes, you have less available memory,

2) it is common to set the memory size to suit the program.

3) the best way to find out the optimal size is to test you program with a realistic work load.

4) Yes, sometimes a little, some times a lot. It depends on what you are doing. If you need to save memory, you may find that it is worth an acceptable performance hit.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

Chances are you are using new a whole lot more than you really need to.

If so, it is also affecting your performance, because new is not cheap, even if you don't count gc.

There's a quick way to find out. Take several samples, as in this post. If you find more than a couple of them are doing new, that's your problem.

What people often do is have new inside a loop, when they could just as well have it outside.

A related problem is they may do a lot of string + string + string, which does a lot of new-ing underneath, when they could just as well use a string builder.

Community
  • 1
  • 1
Mike Dunlavey
  • 40,059
  • 14
  • 91
  • 135
  • I know that `new` is expensive and I avoided using it in loops. But I've never gave the string builder much attention and will read into it. Thanks. – cmtjk Sep 04 '15 at 20:35
  • I'm very suprised! I implemented the String Builder in my application and now the `char[]` instances nearly doubled but the application needs 10 times lesser memory than before. Thank you! – cmtjk Sep 04 '15 at 21:29
  • 1
    @parboiledRice: Yeah :) It's like a credit card. That little '+' between strings makes it way easy to spend more than you can afford. – Mike Dunlavey Sep 05 '15 at 13:05