mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

C++ demo memory release problem

Open junxigauss opened this issue 9 years ago • 25 comments

For bugs or installation issues, please provide the following information. The more information you provide, the more likely people will be able to help you.

Environment info

Operating System: windows

Compiler:VS2015

MXNet version:0.9.5

Steps to reproduce

or if you are running standard examples, please provide the commands you have run that lead to the error.

  1. run image-classification-predict.cc 2.MXPredFree(pred_hnd) don't release memory in GPU or CPU mode

junxigauss avatar Sep 21 '17 01:09 junxigauss

same problem, exe can't exit normally and cpu gpu memory didn't release.

Operating System: Windows 7 x64 Compiler: VS2015 MXNET version: 0.10.0 and 0.11.0

kimivital avatar Sep 21 '17 03:09 kimivital

@lx75249

piiswrong avatar Sep 21 '17 04:09 piiswrong

@piiswrong This is the raw API demo.. not the demo in c++ package.

conopt avatar Sep 21 '17 05:09 conopt

@lx75249 MXNotifyShutdown() can't resolve it yet

junxigauss avatar Sep 21 '17 05:09 junxigauss

I create a std::thread to run the demo, the memory release a big part, but left a small part before exit exe but thread end. I try the release function MXNotifyShutdown(), MXPredFree(pred_hnd) both, absolutely didn't work. I believe it was a bug. code as follow:

int f() {

std::string test_file = "G:/标识牌-9~17.jpg";
//test_file = std::string(argv[1]);

// Models path for your model, you have to modify it
std::string json_file = "VGG_VOC0712Plus_SSD_512x_16w.json";
std::string param_file = "VGG_VOC0712Plus_SSD_512x_16w.params";
std::string synset_file = "model/Inception/synset.txt";
std::string nd_file = "model/Inception/mean_224.nd";

BufferFile json_data(json_file);
BufferFile param_data(param_file);
//system("pause");
// Parameters
int dev_type = 2;  // 1: cpu, 2: gpu
int dev_id = 0;  // arbitrary.
mx_uint num_input_nodes = 1;  // 1 for feedforward
const char* input_key[1] = { "data" };
const char** input_keys = input_key;

// Image size and channels
int width = 512;
int height = 512;
int channels = 3;

const mx_uint input_shape_indptr[2] = { 0, 4 };
const mx_uint input_shape_data[4] = { 1,
	static_cast<mx_uint>(channels),
	static_cast<mx_uint>(height),
	static_cast<mx_uint>(width) };
PredictorHandle pred_hnd = 0;

if (json_data.GetLength() == 0 ||
	param_data.GetLength() == 0) {
	return -1;
}

// Create Predictor
MXPredCreate((const char*)json_data.GetBuffer(),
	(const char*)param_data.GetBuffer(),
	static_cast<size_t>(param_data.GetLength()),
	dev_type,
	dev_id,
	num_input_nodes,
	input_keys,
	input_shape_indptr,
	input_shape_data,
	&pred_hnd);
assert(pred_hnd);

int image_size = width * height * channels;

// Read Mean Data
const mx_float* nd_data = NULL;
NDListHandle nd_hnd = 0;
BufferFile nd_buf(nd_file);

if (nd_buf.GetLength() > 0) {
	mx_uint nd_index = 0;
	mx_uint nd_len;
	const mx_uint* nd_shape = 0;
	const char* nd_key = 0;
	mx_uint nd_ndim = 0;

	MXNDListCreate((const char*)nd_buf.GetBuffer(),
		nd_buf.GetLength(),
		&nd_hnd, &nd_len);

	MXNDListGet(nd_hnd, nd_index, &nd_key, &nd_data, &nd_shape, &nd_ndim);
}

// Read Image Data
std::vector<mx_float> image_data = std::vector<mx_float>(image_size);

GetImageFile(test_file, image_data.data(),
	channels, cv::Size(width, height), nd_data);

// Set Input Image
MXPredSetInput(pred_hnd, "data", image_data.data(), image_size);

// Do Predict Forward
MXPredForward(pred_hnd);

mx_uint output_index = 0;

mx_uint *shape = 0;
mx_uint shape_len;

// Get Output Result
MXPredGetOutputShape(pred_hnd, output_index, &shape, &shape_len);

size_t size = 1;
for (mx_uint i = 0; i < shape_len; ++i) size *= shape[i];

std::vector<float> data(size);

MXPredGetOutput(pred_hnd, output_index, &(data[0]), size);

// Release NDList
if (nd_hnd)
	MXNDListFree(nd_hnd);

// Release Predictor
MXPredFree(pred_hnd);
MXNotifyShutdown();

// Synset path for your model, you have to modify it
std::vector<std::string> synset = LoadSynset(synset_file);

// Print Output Data
//PrintOutputResult(data, synset);
return 0;

} #include

int main(int argc, char* argv[]) { std::thread t1(f); t1.detach(); //f(); }

junxigauss avatar Sep 21 '17 05:09 junxigauss

Win10 is ok.

kimivital avatar Sep 26 '17 01:09 kimivital

what interface you call?@kimivital

junxigauss avatar Sep 26 '17 01:09 junxigauss

I run the example file(image-classification-predict.cc) with other model.

kimivital avatar Sep 26 '17 01:09 kimivital

I run it on win10, memory leak in GPU or CPU mode.

junxigauss avatar Sep 26 '17 01:09 junxigauss

@apache/mxnet-committers: This issue has been inactive for the past 90 days. It has no label and needs triage.

For general "how-to" questions, our user forum (and Chinese version) is a good place to get help.

szha avatar Dec 26 '17 12:12 szha

@junxigauss is this still an issue on mxnet v1.2?

sojiadeshina avatar Jul 30 '18 23:07 sojiadeshina

I' got the same issue on Arch Linux in master branch, but calling MXNotifyShutdown resolve it.

Kolkir avatar Aug 05 '18 07:08 Kolkir

I solve it, by modify nnvm cpu gpu memory manager coder.

junxigauss avatar Aug 05 '18 12:08 junxigauss

@junxigauss - It would be very helpful for all users if you can do a PR for the code change? Or, Can you please provide more details on the fix you made?

sandeep-krishnamurthy avatar Aug 06 '18 15:08 sandeep-krishnamurthy

@junxigauss Can you elaborate what have you done to solve it?

zhreshold avatar Aug 06 '18 18:08 zhreshold

@mxnet-label-bot add [Pending Requester Info]

leleamol avatar Nov 09 '18 19:11 leleamol

@junxigauss Requesting an explanation on the solution, it would help all users , if they run across a similar issue.

vrakesh avatar Nov 26 '18 22:11 vrakesh

@junxigauss : Would you be able to share the solution? As mentioned above, this could be very useful for others as well.

vdantu avatar Dec 15 '18 03:12 vdantu

@leleamol : Would this still be an issue?

vdantu avatar Dec 15 '18 04:12 vdantu

This issue is not related to C++ Front End API. Removing the C++ Label.

leleamol avatar Jan 04 '19 23:01 leleamol