0

I'm using IntelliJ as an editor. These are my vmoptions:

-Xms1024m

-Xmx4096m

-XX:MaxPermSize=700m

-XX:ReservedCodeCacheSize=480m

-XX:SoftRefLRUPolicyMSPerMB=50

Is there anything else I can change in the settings to make it work for me?

My algorithm tries to calculate the Matrix Chain Multiplication Problem via Branch&Bound and in this part(code below) I'm performing deapth-search/creating successors etc. I assume this recursion triggers the heap problem.

 public  static  SimpleMCPNode createTree(SimpleMCPNode currentNode) {
//other statements 
.
.
.
.
for (int i = 0; i < currentNode.matrices.size() - 1; i++) {
        List<MatrixInfo> adaptedList = new ArrayList(currentNode.matrices);
        currentNode.successors.add(createTree(currentNode.createSuccessor(adaptedList, i)));
    }
//other statements
.
.

Depending on the input it can grow exponentially...

trincot
  • 317,000
  • 35
  • 244
  • 286
  • What is the error that you are seeing ? – Geek Mar 04 '20 at 07:05
  • *"it can grow exponentially"* And you're confused it might run out of memory? – Andreas Mar 04 '20 at 07:06
  • 1
    "Depending on the input it can grow exponentially...", if it does grow exponentially you could be simply running out of memory. There's not more than you can do then besides get a better algorithm or get more memory. – PiRocks Mar 04 '20 at 07:07
  • you are creating a new ArrayList for each iteration. you should create the arraylist outside the loop – Shubham Mar 04 '20 at 07:08
  • thank you guys, I will try to initialize it outside the loop. – Amokachi1903 Mar 04 '20 at 07:40

1 Answers1

0

You do not have to initialize a new arraylist each time.

Please use :

ArrayList<MatrixInfo> adaptedList = new ArrayList<MatrixInfo>();
for(int i=0; i< 10; i++){
  currentNode.successors.add(createTree(currentNode.createSuccessor(adaptedList,i)));
  adaptedList.clear() 
}
  • 3
    [Calling `gc()` is not a very good idea](https://stackoverflow.com/questions/2414105/why-is-it-bad-practice-to-call-system-gc), and it does not guarantee that the garbage collector is invoked. – Titulum Mar 04 '20 at 07:22
  • the adaptedList must have the type and I need for each node an own list of the matrices. Therefore, I don't know how to initiliaze it outside the loop – Amokachi1903 Mar 04 '20 at 08:17
  • Then use adaptedList.clear() at the end of the each iteration – kantarovski Mar 04 '20 at 12:53
  • @kantarovski as you did above? I'm not sure If it helps, but I will try. Thanks mate – Amokachi1903 Mar 08 '20 at 10:31