开发者

Java search array by brute force and doesnt spends memory?

开发者 https://www.devze.com 2023-03-20 23:01 出处:网络
I\'m doing a comparison of methods in a construction of an array without repeated elements and then get the time it took and the memory used.

I'm doing a comparison of methods in a construction of an array without repeated elements and then get the time it took and the memory used.

For the hash method and the treeset method the memory prints without problem, but for the bruteforce search it doesn't print any memory. Is it possible that the brute force doesn't use any "respectable" memory because it just compares a element one by one? this is the code I have. Is it possible that is something wrong?

public static void main(String[] args)
{
    Random r = new Random();            

        int warmup = 0;
        while(warmup<nr) {

            tempoInicial = System.nanoTime();
            memoriaInicial = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();

            while(ne<max)
            {
                valor = r.nextInt(maxRandom);
                acrescentar();
            }

            tempoFinal = System.nanoTime(); 
            memoriaFinal   = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();

   开发者_Python百科         retirar();
            System.gc();

            warmup++;
        }

and

    private static void acrescentar()
    {
    if(usaTreeSet)
    {
        if(ts.contains(valor))
            return;

        ts.add(valor);
    }

    if(usaHashSet)
    {
        if(hs.contains(valor))
            return;

        hs.add(valor);
    }

    if(usaBruteForce)
    {
        for(int i=0; i<ne; i++)
        {
            if(pilha[i]==valor)
                return;
        }
    }

    pilha[ne]=valor;
    ne++;
}


When testing small amounts of memory try turning off the TLAB and no object is too small. ;) -XX:-UseTLAB The TLAB allocates blocks of memory at a time to each thread. These blocks do not count to the free memory.

You might find this article on Getting the size of an Object useful.

0

精彩评论

暂无评论...
验证码 换一张
取 消