垃圾收集器未运行

时间:2015-07-04 14:00:10

标签: java garbage-collection

据我所知,我们无法保证在java中调用垃圾收集器。

public class finalizeDemo {
    protected void finalize() {
        System.out.println("Wow!! I am called");
    }
}

class testFinalizeDemo {
    public static void main(String[] a) {
        finalizeDemo obnj = new finalizeDemo();

        System.gc();
        /**
         *  Forcefully call garbage collector.
         */
    }
}

但我读到System.gc()会强行调用gc。但我的声明并没有印在我当地的日食中。

有谁知道为什么声明没有被打印?

2 个答案:

答案 0 :(得分:0)

当您调用System.gc时,obnj引用的对象是一个活动对象。它仍然被obnj引用。因此,垃圾收集器的正确行为是不完成de object。

尝试在调用System.gc()

之前添加obnj = null

仍然无法保证对象将最终确定,但至少现在是可能的。

答案 1 :(得分:0)

代码将打印

  

哇!!我被称为

因为有一个需要最终确定的对象的参考。

$ cat t835.cu
#include <thrust/host_vector.h>
#include <thrust/copy.h>
#include <thrust/device_vector.h>
#include <stdint.h>
#include <iostream>
#include <thrust/transform.h>
#include <thrust/iterator/zip_iterator.h>
#include <thrust/iterator/permutation_iterator.h>
#include <thrust/sequence.h>
#include <thrust/scan.h>


using namespace thrust::placeholders;

struct my_semarker_func
{
template <typename T>
  __host__ __device__
  uint32_t operator()(const T &d1, const T &d2){
    if (thrust::get<0>(d1) != thrust::get<0>(d2)) return 1;
    if (thrust::get<1>(d1) != thrust::get<1>(d2)) return 1;
    return 0;}
};


#define PRINTER(name) print(#name, (name))
template <template <typename...> class V, typename T, typename ...Args>
void print(const char* name, const V<T,Args...> & v)
{
    std::cout << name << ":\t";
    thrust::copy(v.begin(), v.end(), std::ostream_iterator<T>(std::cout, "\t"));
    std::cout << std::endl;
}

int main()
{
    uint32_t e[] = {0,0,0,0,0,0,1,1,1};
    uint32_t t[] = {1,1,4,4,4,5,1,6,7};

    int size = sizeof(t)/sizeof(t[0]);
    typedef thrust::host_vector<uint32_t> HVec;
    typedef thrust::device_vector<uint32_t> DVec;
    HVec h_e(e,e+size);
    HVec h_t(t,t+size);
    DVec d_i(size+1);
    DVec d_e = h_e;
    DVec d_t = h_t;
    thrust::sequence(d_i.begin(), d_i.end());
    PRINTER(d_e);
    PRINTER(d_t);
    PRINTER(d_i);

// create segment end markers
    DVec d_s(size,1);
    thrust::transform(thrust::make_zip_iterator(thrust::make_tuple(d_e.begin(), d_t.begin())), thrust::make_zip_iterator(thrust::make_tuple(d_e.end()-1, d_t.end()-1)), thrust::make_zip_iterator(thrust::make_tuple(d_e.begin()+1, d_t.begin()+1)), d_s.begin(), my_semarker_func());
// create segment f values
    DVec d_g(size);
    thrust::copy_if(d_i.begin()+1, d_i.end(), d_s.begin(), d_g.begin(), _1 == 1);
// create segment indices
    DVec d_h(size);
    thrust::exclusive_scan(d_s.begin(), d_s.end(), d_h.begin());
// create f
    DVec d_f(size);
    thrust::copy_n(thrust::make_permutation_iterator(d_g.begin(), d_h.begin()), size, d_f.begin());
    PRINTER(d_f);

    return 0;
}
$ nvcc -std=c++11 -o t835 t835.cu
$ ./t835
d_e:    0       0       0       0       0       0       1       1       1
d_t:    1       1       4       4       4       5       1       6       7
d_i:    0       1       2       3       4       5       6       7       8       9
d_f:    2       2       5       5       5       6       7       8       9
$