concurrency - Memory leak in Java? Dining philosophers implemented with Semaphores -
i seem have created memory leak in java, didn't realize possible. implemented 1 solution dining philosophers concurrency problem, based on figure in andrew tanenbaum's book modern operating systems.
it works fine far not deadlocking , not starving of threads. however... on short amount of time eats around 1gb of ram (based on watching windows system resources), , eclipse crashes message unhandled event loop exception java heap space.
questions:
- what causing this?
- what tools (other logical deduction, failing me) use answer question myself? java/memory profiling, etc? inexperienced such tools outside of debugger built in eclipse.
sscce:
import java.util.concurrent.semaphore; public class semaphorediningphilosophers { static enum state { thinking, hungry, eating } static int n = 5; static semaphore mutex; static semaphore[] sem_philo; static state[] states; static void philosopher(int i) throws interruptedexception { states[i] = state.thinking; system.out.println("philosopher #" + (i + 1) + " thinking."); while (true) { takeforks(i); eat(i); putforks(i); } } static void takeforks(int i) throws interruptedexception { mutex.acquire(); states[i] = state.hungry; test(i); mutex.release(); sem_philo[i].acquire(); } static void eat(int i) { system.out.println("philosopher #" + (i + 1) + " eating."); } static void putforks(int i) throws interruptedexception { mutex.acquire(); states[i] = state.thinking; system.out.println("philosopher #" + (i + 1) + " thinking."); test((i + 4) % n); test((i + 1) % n); mutex.release(); } static void test(int i) { if (states[i] == state.hungry && states[(i + 4) % n] != state.eating && states[(i + 1) % n] != state.eating) { states[i] = state.eating; sem_philo[i].release(); } } public static void main(string[] args) { mutex = new semaphore(1); sem_philo = new semaphore[n]; (int = 0; < n; i++) { sem_philo[i] = new semaphore(0); } states = new state[n]; thread[] philosophers = new thread[n]; (int = 0; < n; i++) { final int i2 = i; philosophers[i2] = new thread(new runnable() { public void run() { try { philosopher(i2); } catch (interruptedexception e) { e.printstacktrace(); } } }); philosophers[i2].start(); } } }
the problem amount of output generated program. eclipse run out of memory if tries keep stored in output console.
Comments
Post a Comment