comparison src/share/vm/services/memTracker.cpp @ 9061:4c8bb5e4f68f

8011161: NMT: Memory leak when encountering out of memory error while initializing memory snapshot Summary: Fix memory leaks when NMT fails to initialize snapshot and worker thread Reviewed-by: dcubed, ccheung, rdurbin
author zgu
date Fri, 05 Apr 2013 12:19:19 -0400
parents 06db4c0afbf3
children 35f8765422b9
comparison
equal deleted inserted replaced
9060:cc32ccaaf47f 9061:4c8bb5e4f68f
1 /* 1 /*
2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
125 125
126 assert(_main_thread_tid == os::current_thread_id(), "wrong thread"); 126 assert(_main_thread_tid == os::current_thread_id(), "wrong thread");
127 assert(_state == NMT_bootstrapping_multi_thread, "wrong state"); 127 assert(_state == NMT_bootstrapping_multi_thread, "wrong state");
128 128
129 _snapshot = new (std::nothrow)MemSnapshot(); 129 _snapshot = new (std::nothrow)MemSnapshot();
130 if (_snapshot != NULL && !_snapshot->out_of_memory()) { 130 if (_snapshot != NULL) {
131 if (start_worker()) { 131 if (!_snapshot->out_of_memory() && start_worker()) {
132 _state = NMT_started; 132 _state = NMT_started;
133 NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack()); 133 NMT_track_callsite = (_tracking_level == NMT_detail && can_walk_stack());
134 return; 134 return;
135 } 135 }
136
137 delete _snapshot;
138 _snapshot = NULL;
136 } 139 }
137 140
138 // fail to start native memory tracking, shut it down 141 // fail to start native memory tracking, shut it down
139 shutdown(NMT_initialization); 142 shutdown(NMT_initialization);
140 } 143 }
542 */ 545 */
543 bool MemTracker::start_worker() { 546 bool MemTracker::start_worker() {
544 assert(_worker_thread == NULL, "Just Check"); 547 assert(_worker_thread == NULL, "Just Check");
545 _worker_thread = new (std::nothrow) MemTrackWorker(); 548 _worker_thread = new (std::nothrow) MemTrackWorker();
546 if (_worker_thread == NULL || _worker_thread->has_error()) { 549 if (_worker_thread == NULL || _worker_thread->has_error()) {
547 shutdown(NMT_initialization); 550 if (_worker_thread != NULL) {
551 delete _worker_thread;
552 _worker_thread = NULL;
553 }
548 return false; 554 return false;
549 } 555 }
550 _worker_thread->start(); 556 _worker_thread->start();
551 return true; 557 return true;
552 } 558 }