3

I am writing a servlet that I can't test in Eclipse, I need to run on server. I want to do memory profiling and pinpoint any leaks. So, I think I need write debug statements that can tell me current memory usage. Can someone point me to good references on how to do this and/or which classes in the JDK do this ?

Note that I can't use the "Eclipse MAT".

asked Mar 18, 2011 at 7:09
1
  • @All answerers. Really good stuff, but I'm limited in setup as I have to deploy servlet onto a Domino Setup that, although could run Apache web server, is running the proprietary Domino servlet engine. So, am sorta stuck with running internal code for performance info. Commented Apr 6, 2011 at 22:18

5 Answers 5

7

Can't you use the built-in tool in the jdk jvisualvm?

answered Mar 18, 2011 at 7:21

Comments

2

JConsole to rescue you!

answered Mar 18, 2011 at 7:24

Comments

1

Profiling application memory and hunting down leaks would be a difficult task, not to mention misleading with simple debug statements. If you can't use a tool which remotely connects to your process, using hprof would be a good bet IMO. Also, have a look at the troubleshooting documentation here.

But I still think it would be better if you tried to do the same locally (i.e. fixing leaks) if possible.

answered Mar 18, 2011 at 7:31

1 Comment

Yes, hprof helped us find memory leaks in our server app. (+1)
1

So, I think I need write debug statements that can tell me current memory usage.

That most likely won't help you much since the garbage collector makes it somewhat hard to find memory leaks by just looking at the counts (you don't really know when the gc runs and what it actually collects, it sometimes doesn't collect everything that is collectable). So you might need to make some memory snapshots and analyse them, i.e. see which objects (or which types of objects) are not collected and thus more and more instances are created.

For this, take a look at the suggested tools (JVisualVM, JConsole).

If you still want to get memory usage information from inside your program, try the classes in the java.lang.management package;

answered Mar 18, 2011 at 7:30

Comments

1

So, I think I need write debug statements that can tell me current memory usage.

I think you are like me who prefers to profile in code instead of using an external profiling tool like JVisualVM or JConsole.

You can use a tool called MemorySampler (written by me) from CoralBits that will tell you exactly in what line of your code memory is being allocated. Below an example:

ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();
MemorySampler.start();
for(int i = 0; i < strings.length; i++) queue.offer(strings[i]);
for(int i = 0; i < strings.length; i++) queue.poll();
MemorySampler.end();
if (MemorySampler.wasMemoryAllocated()) MemorySampler.printSituation();

Gives you the following output:

Memory allocated on last pass: 24576
Memory allocated total: 24576
Stack Trace:
 java.util.concurrent.ConcurrentLinkedQueue.offer(ConcurrentLinkedQueue.java:327)
 TestGC.main(TestGC2.java:25)

Now if you go to line 327 of the ConcurrentLinkedQueue source code, you will see that it allocates a Node instance there.

Disclaimer: I am one of the developers of CoralBits.

answered Jun 18, 2014 at 7:19

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.