views:

106

answers:

4

I am currently running a small java class for scientific calculations on graphs (which internally creates lots of huge collections) from within Eclipse on MacOSX Snow Leopard. I have a Macbook with 2GB of RAM and to successfully run the app without OutOfMemory Error I need to run it from eclipse with -Xmx1200m (I know this is very much).

It takes about 500seconds to do all the computations I need to run and it also uses up all the memory I have given it (I monitored it with macosx progress monitor and it goes to 1,2GB for Real Mem)

After this application is done my MacOSX is almost unusable. Even if I close Eclipse everything becomes sluggish and slow. I also notice that whatever I do afterwards (even after closing Eclipse) that e.g. Finder navigation is very slow. It takes ages to render some folders or to change directories. Sometimes I restart the system to be able to work again efficiently.

I would like to understand what is happening in the background when this happens? I was under the impression, that when a java class / app has finished running, that the Garbage Collector at least at the end will cleanup everything and I would have expected that other programs are free to use this memory again. But it "feels" like a memory leak which affects the whole system, even if the Java app is not running anymore.

I would appreciate any hints or readings in this direction.

+3  A: 

Make sure that java process ends properly after you exit your application. I had similar issue with JBoss AS

denis_k
That is the cases. verified via Activity Monitor / top.
Christoph
+3  A: 

Your Mac is running slow because it has 2GB, you have Eclipse running AND you have a 1.2GB app. Its probably swapping like mad.

Your Java program probably has a memory leak. You are adding to collections and not removing from them. You could use a profiler such as TPTP, NetBeans or VisualVM to search for it.

Java programs can leak as much as any other language if you do the wrong thing.

If you genuinely need that much memory and its not a leak then you may need to buy more if you can't split the program up or save intermediate results to file or a database.

Fortyrunner
The OP said that the slowdown continues even after Eclipse is closed; does closing Eclipse not also close any Java environments it started? (Honest question, I know little about how Eclipse behaves behind the scenes, especially for Macs.)
Lord Torgamus
Good point. But that aside, the questioner is going to struggle to run an app that big on a low spec machine. Its good practice to code review and look for memory issues I find
Fortyrunner
@Forty, indeed, I was just trying to clarify about Eclipse.
Lord Torgamus
Hi,the Java process has finished already and in addition to that I close Eclipse too. I can see that in the MacOSX Progress Monitor (or top on console). The java programm definitely could be coded better, but that is a different topic :) I wanted to know why everything is slow after all java related processes have stopped. Thanks for the hints regarding page-swapping.
Christoph
+2  A: 

What you're experiencing is called page-swapping. The OS has evicted (or paged-out) a bunch of virtual memory pages to disk in order to accommodate the requirements of your Java code. When your program is done, and you try to switch back to some other program you have running, the OS has to page-in from disk before it can resume.

You can see all of this happening by using tools such as top or Activity Monitor.

Kaelin Colclasure
... or `vm_stat` (aka `vmstat` on Linux / UNIX / Solaris)
Stephen C
Ok that makes sense. I will have a look now on disk activity after I the java processes have finished. I expect lots of it after reading your answers.
Christoph
+1  A: 

Do yourself a favour and buy another 2Gb RAM. I'm running the 32 bit kernel (i.e. the smaller one) right now on my MBP and the kernel itself has 800Mb RAM wired. For you that would leave 1.2Gb in total for everything else. That's not enough. Believe me, if you buy the extra RAM, not only will your Java process be better, but everything will seem much snappier.

JeremyP
If the problem is a memory leak, adding more RAM will only postpone the appearance of symptoms.
Lord Torgamus
Indeed it will, but this particular issue looks more like his system is under specified. In any case, having 4Gb will improve his experience of OS X all round, not just with this one application. I can say that from experience.
JeremyP