0

I have written an app that interacts with odbc and feeds messages into an GUI. For some reason as a campaign goes on javas memory usage grows to very large amounts over 1GB ram. The app is simple and should not be running up memory like that... I think that cuz the threads are so busy and not stifled in anyway java isnt getting the chance to release the memory it is allocating so it keeps getting bigger and bigger. What I would like to do is somewhere in the thread force java to release the memory. Can anyone tell me how to do this?

thanks..

Selva
  • 839
  • 10
  • 25
  • 40

6 Answers6

2

Try a profiler like visualVM (included with JDK6) or eclipse TPTP.

As a startingpoint: Do you close all statements and resultsets? With mysql I've encountert memoryleeks with the JDBC-driver.

Christian Kuetbach
  • 15,850
  • 5
  • 43
  • 79
2

You can't force jvm to release memory. Your application simply has memory leaks. You'd have to use java profiler ( for instance jvisualvm that is in the jdk ) to find those leaks.

Mirek Pluta
  • 7,883
  • 1
  • 32
  • 23
1

You can't force java to release memory, you only can suggest it via System.gc(). But I think this isn't your main problem. I propose you should profile your application with Eclipse MAT it's a memory analyzer which is easy to understand. If you find your memory leak post it here, so we can see how we get it solved.

Christopher Klewes
  • 11,181
  • 18
  • 74
  • 102
1

Java can only release memory for objects that are no longer referenced (i.e., in use) by the program. And, once an object is no longer referenced, it will be cleaned up fairly quickly (albeit in a way that's out of your direct control).

This is likely a straightforward memory leak. You'll need to analyse the memory to see which objects are there that shouldn't be, and then determine why the garbage collector thinks they are still reachable. At some point in your code you'll be failing to release these variables that are no longer needed (for example, putting them in a Map, though there are many ways of leaking memory if you're not careful).

The first step is to analyse the heap to find out which objects are there that shouldn't be. You can use jhat, jvisualvm, various profilers and likely other tools for this.


The following questions may be of use in solving this issue:

Community
  • 1
  • 1
Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
0

You can't force java to release memory but you can request that it does so using:

System.gc()

The fact that you're seeing a gradual rise in memory usage over time however is indicative of a memory leaks and no amount of calls to System.gc will free that memory. Look at some of the profiling tools mentioned above and use them to trace your memory usage.

mmccomb
  • 13,516
  • 5
  • 39
  • 46
0

Depends upon the JVM, for example Oracle's JRockit can configure the compaction scheme on the command line:

http://download.oracle.com/docs/cd/E15289_01/doc.40/e15062/optionxx.htm#BABGFFID

Steve-o
  • 12,678
  • 2
  • 41
  • 60