简体   繁体   中英

Java .jar uses too much memory

I'm making an application in Java using Eclipse Indigo. When I run it using Eclipse the Task Manager shows javaw.exe is using 50mb of memory. When I export the application as a runnable.jar and execute the.jar the Task Manager shows javaw.exe is using 500mb. Why is this? How could I fix this?

Edit: I'm using a Windows 7 64 bit, and my system says I have Java 1.7 installed. Apparently the memory problem is caused by a while loop. I'll study what's inside the while loop causing the problem.

Edit: Problem found. At one point in the while loop new BufferedImage instances where created, instead of replacing the same BufferedImage .

Without any additional details about your code, I would suggest using a profiler to analyze the problem. I know YourKit and the one that is available for NetBeans are very good.

Once you run you app from the profiler, you should initially look at the objects and listeners created by your application's packages. If the issue is not there, you can expand your search to other packages until you identify things that are growing out-of-control, and then look at the code that handles those entities.

When you run certain parts of the code multiple times and still see memory utilization after that code stopped running, then you might have a leak and may consider nulling or emptying variables/listeners on exit.

It should be a good starting point, but please report your results back, so we know how it goes. By the way, what operating system are you using and what version of java?

--Luiz

You need to profile your code to get the exact answer, but from my experience when I see similar things I often equate it to garbage collecting. For example, I ran the same job and gave one job 10 gigs and the other 2 gigs..Both ran and completed but the 10gigs one used more memory(and finished faster) while the second(2gig) one, I believe, garbage collected so it still completed but took a bit more time with less memory. I'm a bit new to java so I maybe assuming the garbage collecting but I have seen what you are talking about.

You need to profile your code(check out jconsole, which is included with java, or visualVM)..

That sounds most peculiar.

I can think of two possible explanations:

  • You looked at the wrong javaw.exe instance. Perhaps you looked at the instance that is running Eclipse... which is likely to be that big, or bigger.

  • You have (somehow) managed to configure Java to run with a large heap by default. On Linux you could do this with a wrapper script, a shell function or a shell alias. You can do at least the first of those on Windows.

I don't think it is the JAR file itself. AFAIK, you can't set JVM parameters in a JAR file. It is possible that you've somehow included a different version of something in the JAR file, but that's a bit of a stretch...

If none of these ideas help, try profiling.


Problem found. At one point in the while loop new BufferedImage instances where created, instead of replacing the same BufferedImage.

Ah yes. BufferedImage uses large amounts of out-of-heap memory and that needs to be managed carefully.

But this doesn't explain why your application used more memory when run from the JAR than when launched from Eclipse... unless you were telling the application to do different things.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM