• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java.exe memory leaks and Profiling/Performance tools  RSS feed

 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

In the application that I am writing I am now having the error (but not exception) that windows alerts saying "java.exe has generated errors and will be closed by windows."

I started looking at the code to see if there was a single line that was the problem. After lots of research I think that I have a memory problem, with java using up too much memory.

So I installed JProfiler to help me track the classes and objects. The problem now is that I cannot replicate the error when running the application with the profiler or when running the app from the cmd line with the profiling options on, the error only replicates when the application is run on it's own.

Is there anything I can do to isolate the problem?

Thank you kindly!
Rachel
 
sander hautvast
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in my humble opinion i'd say that you don't have a memory problem. Because that would give an OutOfMemoryError. Try evaluating the heap programmatically by using Runtime.freeMemory() once every interval
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Sander - a windows alert means that something has gone seriously wrong inside the JVM. Running out of memory should cause a normal Java Error output to stderr followed by the JVM closing down gracefully.
Are you using any JNI methods? Which Java version is this, and which Windows?
Bill
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess you are right about the OutOfMemoryError - I'm definitely not getting that.

I'm using j2sdk1.4.0 on Windows 2000 Pro. And I'm not using any JNI methods. It's just a plain GUI interface that does file handling for me. I have started going through my code to make sure that I'm doing it in the most efficient way possible, but so far no major changes made.

Rachel
 
slade cox
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, the OutOfMemoryError only occurs when Java's allotted memory is completely used up within the virtual machine. This is entirely independent from Windows running out of memory. In fact, if java tries to take up more memory within windows than is available you will get strange behavior, not the OutOfMemoryError. Before you run java, how much free memory is available on your windows machine? By default java will try to take up 64Mb and if you have less than 64Mb available then you will have problems.

Having said all that, it's more likely that you have plenty of memory and that something else is causing the crash. I've seen similar crashes triggered by the AWT-Event-Thread. Does it die most often at some event? When you mouse over a particular component? Click a button? Or does it just happen after a while of running it? The best way to track down the problem is to compile it with debugging options and use jdb.
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the only thing I have yet to try to to compile with the debugging options.

I find that the error occurs in two places only in the system.

The first is when the code
1) Closes a JFrame
2) Updates a table on another panel
3) Writes to a file
4) Writes to a second file

The second is when the code
1) Closes the same frame
2) Deletes a file

The only similarity is the closing of the frame. But closing the frame under a condition that is not included in the above does not give the error.

I'll try the debug and see what gives.

Cheers,
Rachel
 
Rajat Nayer
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would it help if the code checked for null before doing any operations that lead it to failure just to make sure that the object refernces are retained and are not lost.

regds
Rajat
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rajat

I'm quite sure that the jvm would through a NullPointerException if that were the case.

Btw, now I'm running the jdb debug. It seems that I can replicate the problem while running the debug, but if I step through the code line by line, the problem does not replicate. So what I am doing is establishing breakpoints from the back to the front to try find the line of code where it messes up.

Continued thanks!

Rachel
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I think that the problem is fixed.

Using jdb and breaking from the back to the front I found a line of code that was not executing. And commenting it out seems to have done the trick! But why?

The commented out code was setting an object to null (because I didn't need it anymore.) So why did it throw me out and not just give an exception?

Wierd! Even if I never know why, at least this has taught me about profiling tools, jdb debugger and that commenting your code in good english is actually SO worth it in the long run!

Cheers for your help mates!
Rachel
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rachel Swailes:
Using jdb and breaking from the back to the front I found a line of code that was not executing. And commenting it out seems to have done the trick! But why?


Well, I'd guess that it produced some byte code that caused some weired bug in the VM to show up.

The commented out code was setting an object to null (because I didn't need it anymore.) So why did it throw me out and not just give an exception?


Why should setting a reference to null ever throw an exception?

that commenting your code in good english is actually SO worth it in the long run!


That's interesting - could you please elaborate on how the comments helped?
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By comments helping I just mean that I've now realised how good it is to make little notes to yourself in the code. Like "Changed the filenames here so that I could identify all files moved in this session" or "Used a [] instead of a Vector because...". At least then when you're rushed for time you can look at the comments and not have to figure out what the code was supposed to do.

Cheers,
Rachel
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rachel Swailes:
By comments helping I just mean that I've now realised how good it is to make little notes to yourself in the code. Like "Changed the filenames here so that I could identify all files moved in this session" or "Used a [] instead of a Vector because...". At least then when you're rushed for time you can look at the comments and not have to figure out what the code was supposed to do.


I just asked because I typically prefer to use "self-speaking" code instead of comments. It's not always possible, of course. Not sure about the above examples.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!