Solução

Boa noite amigos

Eu fiz uma prova para um processo seletivo da Irlanda e me deparei com algumas questões que eu soube como resolver e queria a ajuda de vocês.

A customer complained that the memory usage of the following component constantly increases although the numer of resources and tasks remain constant.
Fix the problem without changing the public API of Resource and Runner.

E esse é o código que eu recebi.

package br.com.algoritimos.ordenacao;
import java.util.ArrayList;
import java.util.HashMap;

public class Runner {
private HashMap<Integer, Resource> resources = new HashMap<Integer, Resource>();

public Iterable<Resource> getResources() {
    return this.resources.values();
}

public Resource acquireResource(int id) {
    Resource w = this.resources.getOrDefault(id, null);
    if (w == null) {
        w = new Resource(id);
        this.resources.put(id, w);
    }

    return w;
}

public void releaseResource(int id) {
    Resource w = this.resources.getOrDefault(id, null);
    if (w == null)
        throw new IllegalArgumentException();

    w.dispose();
}

public class Resource {
    private ArrayList<String> tasks = new ArrayList<String>();

    private int id;

    public int getId() {
        return this.id;
    }

    public Iterable<String> getTasks() {
        return this.tasks;
    }

    public Resource(int id) {
        this.id = id;
    }

    public void performTask(String task) {
        if (this.tasks == null)
            throw new IllegalStateException(this.getClass().getName());

        this.tasks.add(task);
    }

    public void dispose() {
        this.tasks = null;
    }
}

public static void main(String[] args) {
    Runner d = new Runner();

    d.acquireResource(1).performTask("Task11");
    d.acquireResource(2).performTask("Task21");
    System.out.println(String.join(", ", d.acquireResource(2).getTasks()));
    d.releaseResource(2);
    d.acquireResource(1).performTask("Task12");
    System.out.println(String.join(", ", d.acquireResource(1).getTasks()));
    d.releaseResource(1);
}

}