Multi Threading and GC question

I have a question about multithreading and GC...I have a java application that read s2 files and creates a Frame with 2 JTreeTables with the datas read in this file. I use a recursive method to create the Jtree, and that takes much memory...my questions are: could I improve the performance of my application forcing the garbage collection in any way, to free the memory? Could the multithreading help me about improvements (create 2 thread to read the file and build the trees..). Can anybody help me with this this problem?

Thanks a lot in advance...

[565 byte] By [pobox76] at [2007-9-30 20:06:05]
# 1

> could I improve the performance of my application forcing

> the garbage collection in any way, to free the memory?

[url=http://www-106.ibm.com/developerworks/library/j-jtp01274.html]No.[/url]

> Could the multithreading help me about improvements

> (create 2 thread to read the file and build the trees..).

Likely not, but you could try it.

If you have a multi-CPU system, it could help.

Or if the files are large, and the amount of processing in reading them is small. Then one thread would be waiting for the disk to rotate to the next data block, while the other thread processes its chunk of data. This will result in measurable savings only if the files are in the multi-megabyte range. If the reading is CPU-bound (significant amount of processing), threads won't help, because you are already maxing out the CPU.

Do you have a speed issue? Profile it first (java -Xrunhprof:help). Measure how much time the reading takes - simply two calls to System.currentTimeMillis() will do it.

sjasja at 2007-7-7 0:52:09 > top of Java-index,Java Essentials,Java Programming...
# 2

> I use a recursive method to create the Jtree,

> and that takes much memory...my questions are: could

> I improve the performance of my application forcing

> the garbage collection in any way, to free the memory?

The recursive method in itself just uses temporary (stack) memory. When it returns it gives everything back. What's left are the objects you've created (on the heap). The GC just controls the heap. But you don't want it to remove the object the recursive method just created do you?

UlrikaJ at 2007-7-7 0:52:10 > top of Java-index,Java Essentials,Java Programming...
# 3

> Could the multithreading help me about improvements

> (create 2 thread to read the file and build the

> trees..).

If the files are being read from the same device then multi-threading probably won't increase performance. The bottleneck is probably the file I/O. Adding a thread won't make that any faster.

If recursion uses too much memory the first thing to try is to replace recursion with iteration.

ehodges at 2007-7-7 0:52:10 > top of Java-index,Java Essentials,Java Programming...
# 4

The recursion code I'm using is

public static void readStructure(String str,TreeTableNode top)

{

Structure current=(Structure )elementsStruct.get(str);

System.out.println("Current "+current.getPadre());

Vector vect=current.getchildren();

for(int i=0;i<vect.size();i++)

{

String related=(String)vect.get(i);

System.out.println("Found "+related);

TreeTableNode node = new TreeTableNode(related);

top.add(node);

readStructure(related,node);

}

}

where elementsStruct. is an HashTable that holds the ID of the father and a vector with all the children.Could iteration improve the performance? Can you give an example of iteration insted of recursion?

>

pobox76 at 2007-7-7 0:52:10 > top of Java-index,Java Essentials,Java Programming...
# 5

> Could iteration improve the performance?

It can reduce the memory used and the number of method calls. That may or may not improve performance.

> Can

> you give an example of iteration insted of recursion?

Not off the top of my head. You can keep your own stack of next siblings and do a depth-first traversal. Here's a link that should get you started with more info about iterative traversals: http://encyclopedia.thefreedictionary.com/Iterative%20deepening%20depth-first%20search

ehodges at 2007-7-7 0:52:10 > top of Java-index,Java Essentials,Java Programming...
# 6
When I run the application I get the error java.lang.ArrayStoreException. What is it due to? Does it concern memory use?
pobox76 at 2007-7-7 0:52:10 > top of Java-index,Java Essentials,Java Programming...
# 7
Read the description in the API: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ArrayStoreException.html
DrLaszloJamf at 2007-7-7 0:52:10 > top of Java-index,Java Essentials,Java Programming...
# 8

Hi,

I have one doubt.

in theory even java supports multithreading, no two processes execute at the same time. right?

I am having two CPU in my machine, if my application starts two threds do they executed parellely by two CPUs. if it is right, if one thread is reading, other one is writing to a file..DEADLOCK occurs?

please clarify..

Thanks in advance..

Thanks

Vijay kumar.

vijaykumar_balla at 2007-7-7 0:52:10 > top of Java-index,Java Essentials,Java Programming...