Author Topic: Java Thread Memory  (Read 1292 times)

Greg Pringle

  • Jr. Member
  • **
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 74
    • View Profile
Java Thread Memory
« on: July 21, 2016, 07:24:27 pm »
Has anyone else noticed that every time a thread under java 1.6 for os/2 is run and ends the memory goes down by 4 k ?

I use Theseus4 to check memory usage by process and after a new thread ends there is 4 k more memory
used by the java vm. This is true when the thread is started directly or as a runnable class or an inner class.
It seems to be true for both low and high memory. Garbage collection seems to have no effect.

When using java 1.3 for os/2 the memory stabilizes after a few starts and stops.



Joop

  • Sr. Member
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 30
  • Posts: 405
    • View Profile
Re: Java Thread Memory
« Reply #1 on: July 21, 2016, 09:24:15 pm »
Yes, but it is very dependent on the java program. I have programs which do not add used memory and I have java programs which do. This might be an incompatibility between the different Java systems available.  I do see this between Sun and OpenJDK. It looks like Sun compiled programs do add. But it might as much as you run the program or you don't run the program because the developer probably can't help you here. And how bad is it that it eats some memory in the order of 4K or less against the 1Gb or more?

Greg Pringle

  • Jr. Member
  • **
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 74
    • View Profile
Re: Java Thread Memory
« Reply #2 on: July 21, 2016, 10:22:58 pm »
The most basic program that simply calls a thread will make the problem happen.

package aaa;

/**
 * @author: greg
 */
public class PrcTst {
   private class T implements Runnable {
      public T() {
      }
      public void run() {
         System.out.println("T");
      }
   }
/**
 * PrcTst constructor comment.
 */
public PrcTst() {
   super();
   try {
      for (int i = 0; i < 20000; i++) {
         T t = new T();
         Thread ms = new Thread((Runnable) t);
         ms.start();
         Thread.sleep(300);
         System.gc();
         ms.stop();
      }
   } catch (Exception e) {
      System.out.println(e);
   }
}
/**
 * @param args java.lang.String[]
 */
public static void main(String[] args) {
   PrcTst p = new PrcTst();
}
}

Greg Pringle

  • Jr. Member
  • **
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 74
    • View Profile
Re: Java Thread Memory
« Reply #3 on: July 21, 2016, 10:32:30 pm »
I should have mentioned that I like to run os/2 server applications and use java to do it.
I do have java applications that run for months or years between reboots using os/2 java 1.3

The problem of the leak in java 1.6 seems to be greater memory depending on the thread started and
in any case the applications fail quickly if many threads are started. Within days.

I have read about problems with java on other platforms but this particular issue has not been mentioned.

Greg Pringle

  • Jr. Member
  • **
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 74
    • View Profile
Re: Java Thread Memory
« Reply #4 on: August 03, 2016, 08:15:07 pm »
After spending time looking at the leak I decided to try and work around it.
I wrote a thread pool that uses reflection to start "threads" and otherwise the stared classes act like regular Threads.
Existing code needed only small modifications to use the thread pool since the code was working and
properly starting and stopping Threads.
What I also found was the garbage collector is not very smart. To make it work well the scope should not be expanded
too far and certainly not intertwined.
This made the running code stabilize after some time and stop claiming more memory. The IBM Java 1.3 does not need
the time to stabilize or the special thread pool.
The new code added will work with all the other platforms java so if they have the same problem they should also be fixed.
Now to update a large code base.

Greg Pringle

  • Jr. Member
  • **
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 74
    • View Profile
Re: Java Thread Memory
« Reply #5 on: October 01, 2016, 05:28:04 pm »
I found that Windows Virtual Machines also have the Thread issue with memory not being returned to the OS. The code I wrote to fix the problem for OS/2 Java 1.6 also works to fix Windows. Two stub methods are added to Classes to be able to use the new thread pool. Also this thread pool can be passed to dynamically added classes and methods. The other change is that the thread pool needs to be initialized at the beginning and ended at the end of the program. Otherwise your application will never stop. The threads that are put into the thread pool are added with a few lines that replace the way Runnable classes are started.

This is actually working and in production on OS/2, Linux, Widows, AIX and HP.