私はJavaプログラムを実装しました。これは基本的に固定数のスレッドを持つマルチスレッドサービスです。各スレッドは、一度に1つのタスクを取り、ハッシュセットを作成します。ハッシュセットのサイズは、1つのハッシュセット内の10から20,000以上のアイテムまでさまざまです。各スレッドの終わりに、結果はsynchronizedを使用して共有コレクションリストに追加されます。メモリ不足:ハッシュセットを使用したマルチスレッド
問題が発生するのは、ある時点でメモリ例外から抜け出すことです。今少しの研究をした後、私はこのメモリ例外がGCがメモリをクリアしているときに発生し、その時点で全世界が何かを実行するのを止めることを発見しました。
このように大量のデータを処理する方法を教えてください。ハッシュセットは使用する正しいデータ構造ですか?どのようにメモリ例外に対処するには、私は1つの方法はSystem.GC()を使用することです、それは全体のプロセスを遅くするので、再び良くないです。または、それを共有コレクションリストに追加した後に "HashSet hsN"を処分することは可能ですか?
私があなたの考えを知って、私が間違っているところを教えてください。このサービスは、膨大なデータ処理に対処する予定です。
おかげで、このような実装では
//business object - to save the result of thread execution
public class Location{
integer taskIndex;
HashSet<Integer> hsN;
}
//task to be performed by each thread
public class MyTask implements Runnable {
MyTask(long task) {
this.task = task;
}
@Override
public void run() {
HashSet<Integer> hsN = GiveMeResult(task);//some function calling which returns a collection of integer where the size vary from 10 to 20000
synchronized (locations) {
locations.add(task,hsN);
}
}
}
public class Main {
private static final int NTHREDS = 8;
private static List<Location> locations;
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(NTHREDS);
for (int i = 0; i < 216000; i++) {
Runnable worker = new MyTask(i);
executor.execute(worker);
}
// This will make the executor accept no new threads
// and finish all existing threads in the queue
executor.shutdown();
// Wait until all threads are finish
while (!executor.isTerminated()) {
}
System.out.println("Finished all threads");
}
}
JAVAが最良の選択やC#.net4のですか?
このような実装では、JAVAは最良の選択ですか、C#.net4のために用意しますか? – user1213831