我正在Pinoccio上运行一些代码,它是Arduino的一个分支。我在下面的代码中泄漏内存,并且我已经包含了我的程序运行与时间的内存占用。
我花了最后几个小时调试为什么我的记忆慢慢消失,所以我决定只给这个论坛一个镜头。
#include <SPI.h>
#include <Wire.h>
#include <Scout.h>
#include <GS.h>
#include <bitlash.h>
#include <lwm.h>
#include <js0n.h>
#include <QueueArray.h>
#include <Oyoroi.h>
#include <aJSON.h>
#include <MemoryFree.h>
#define SERVER_LOCATION "1.2.3.4"
#define SERVER_PORT 5000
int INTERVAL_QUERY_SERVER = 10000;
struct OMessage {
int scoutId;
String message;
int messageKey;
};
QueueArray<OMessage*> queue;
GSTcpClient *clientPtr;
GSModule *gsPtr;
bool isOyoroiVerbose = 0;
void setup() {
Scout.setup("Custom", "Unknown", -1);
Serial.begin(115200);
Shell.isVerbose = false;
Shell.eval("rm *"); // Clean up previous installations
Shell.eval("hq.verbose(0)");
Shell.eval("events.verbose(0)");
Shell.eval("mesh.verbose(0)");
addBitlashFunction("oyoroi.verbose", (bitlash_function) oyoroiVerbose);
Shell.eval("oyoroi.verbose(1)");
queue.setPrinter(Serial);
addBitlashFunction("oyoroi.queue", (bitlash_function) oyoroiQueue);
addBitlashFunction("oyoroi.connect", (bitlash_function) oyoroiConnect);
// Do not use oyoroi.queue(arg(1), key.print(arg(2))) here because key.print
// returns 1 when it has executed successfully.
Shell.eval( "function on.message.scout { oyoroi.queue(arg(1), arg(2)) }");
gsPtr = &pinoccio::WifiModule::instance.bp()->gs;
// if you don't use new it will be deleted out of scope
clientPtr = new GSTcpClient(*gsPtr);
}
long last = 0;
void loop() {
Scout.loop();
long now = millis();
if (now - last > INTERVAL_QUERY_SERVER) {
last = now;
oyoroiConnect();
} else if (now < last) { // millis resets
last = now;
}
}
numvar oyoroiVerbose(void) {
if (!checkArgs(1, F("usage: oyoroi.verbose(flag)"))) {
return 0;
}
isOyoroiVerbose = getarg(1);
return 1;
}
numvar oyoroiQueue(void) {
if (isOyoroiVerbose) {
Serial.print("oyoroi.queue: scoutId:");
Serial.print(getarg(1));
Serial.print(" message:");
// do not use getstringarg
Serial.print(keyGet(getarg(2)));
Serial.print(" free-memory:");
Serial.println(freeMemory());
}
OMessage *m = new OMessage();
m->scoutId = getarg(1);
m->message = keyGet(getarg(2));
m->messageKey = getarg(2);
queue.enqueue(m);
}
String getPostData(void) {
Serial.print("getPostData - 1: "); Serial.println(freeMemory());
aJsonObject *root = aJson.createObject();
aJsonObject *array = aJson.createArray();
aJson.addItemToObject(root, "data", array);
int i=queue.count();
Serial.print("getPostData - 2: "); Serial.println(freeMemory());
while (!queue.isEmpty()) {
OMessage *m = queue.dequeue();
aJsonObject *node = aJson.createObject();
aJson.addNumberToObject(node, "scoutId", m->scoutId);
char charBuf[10];
m->message.toCharArray(charBuf, 10);
aJson.addStringToObject(node, "message", charBuf);
aJson.addItemToArray(array, node);
// Don't forget to free up our used memory!
keyFree(m->messageKey);
delete(m);
}
Serial.print("getPostData - 3: "); Serial.println(freeMemory());
char *json_String = aJson.print(root);
Serial.print("getPostData - 4: "); Serial.println(freeMemory());
while (i>0) {
aJson.deleteItemFromArray(array, i--);
}
aJson.deleteItemFromObject(root, "data");
aJson.deleteItem(root);
Serial.print("getPostData - 5: "); Serial.println(freeMemory());
return json_String;
}
numvar oyoroiConnect(void) {
Serial.print("oyoroiConnect - 1: "); Serial.println(freeMemory());
if (queue.isEmpty()) {
if (isOyoroiVerbose)
Serial.println("Queue is empty.");
return 0;
}
if (!gsPtr->isAssociated()) {
if (isOyoroiVerbose)
Serial.println("GSModule not associated yet.");
return 0;
}
IPAddress ip;
char* url = SERVER_LOCATION;
if (!gsPtr->parseIpAddress(&ip, url)) {
ip = gsPtr->dnsLookup(url);
if (ip == INADDR_NONE) {
if (isOyoroiVerbose) {
Serial.print(F("Failed to resolve "));
Serial.println(url);
}
return 0;
}
}
if (!clientPtr->connect(ip, SERVER_PORT)) {
Serial.print(F("ERROR - Connection to "));
Serial.print(url);
Serial.print(F(":"));
Serial.print(SERVER_PORT);
Serial.println(F(" failed."));
return 0;
}
if (isOyoroiVerbose)
Serial.println("Client successfully connected to server");
clientPtr->println(F("POST / HTTP/1.1"));
clientPtr->println(F("User-Agent: pinoccio"));
clientPtr->print(F("Host: "));
clientPtr->print(SERVER_LOCATION);
clientPtr->print(":");
clientPtr->println(SERVER_PORT);
clientPtr->println("Connection: close");
String postData = getPostData();
Serial.print("oyoroiConnect - 2: "); Serial.println(freeMemory());
clientPtr->print("Content-Length: ");
clientPtr->println(postData.length());
clientPtr->println("Content-type: application/json");
clientPtr->println();
clientPtr->println(postData);
clientPtr->println();
clientPtr->flush();
while (clientPtr->connected()) {
if (clientPtr->available()) {
clientPtr->read();
} else {
Scout.loop();
}
}
Serial.print("oyoroiConnect - 3: "); Serial.println(freeMemory());
return 1;
}
日志:
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:15363
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:15338
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:15316
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:15288
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:15113
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:15117
oyoroiConnect - 1: 15401
Client successfully connected to server
getPostData - 1: 15332
getPostData - 2: 15294
getPostData - 3: 14974
getPostData - 4: 14716
getPostData - 5: 15211
oyoroiConnect - 2: 15063
oyoroiConnect - 3: 15098
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:14974
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:14950
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:14930
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:14904
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:14882
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:14853
oyoroiConnect - 1: 15171
Client successfully connected to server
getPostData - 1: 15102
getPostData - 2: 15064
getPostData - 3: 14753
getPostData - 4: 14495
getPostData - 5: 14988
oyoroiConnect - 2: 14840
oyoroiConnect - 3: 14840
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:14716
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:14692
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:14672
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:14646
oyoroi.queue: scoutId:4 message:ph=4.28 free-memory:14624
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:14595
oyoroiConnect - 1: 14913
Client successfully connected to server
getPostData - 1: 14844
getPostData - 2: 14806
getPostData - 3: 14495
getPostData - 4: 14237
getPostData - 5: 14730
oyoroiConnect - 2: 14582
oyoroiConnect - 3: 14582
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:14458
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:14434
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:14414
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:14388
oyoroi.queue: scoutId:4 message:ph=4.28 free-memory:14366
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:14337
oyoroiConnect - 1: 14655
Client successfully connected to server
getPostData - 1: 14586
getPostData - 2: 14548
getPostData - 3: 14237
getPostData - 4: 13979
getPostData - 5: 14472
oyoroiConnect - 2: 14324
oyoroiConnect - 3: 14324
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:14200
oyoroi.queue: scoutId:4 message:ph=4.28 free-memory:14176
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:14156
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:14130
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:14108
oyoroiConnect - 1: 14419
Client successfully connected to server
getPostData - 1: 14350
getPostData - 2: 14312
getPostData - 3: 14052
getPostData - 4: 13794
getPostData - 5: 14214
oyoroiConnect - 2: 14100
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:13753
oyoroiConnect - 3: 14078
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:13918
oyoroi.queue: scoutId:4 message:ph=4.28 free-memory:13898
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:13872
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:13850
oyoroiConnect - 1: 14161
Client successfully connected to server
getPostData - 1: 14092
getPostData - 2: 14054
getPostData - 3: 13794
getPostData - 4: 13536
getPostData - 5: 13956
oyoroiConnect - 2: 13842
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:13495
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:13471
oyoroiConnect - 3: 13798
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:13640
oyoroi.queue: scoutId:4 message:ph=4.28 free-memory:13614
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:13592
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:13563
oyoroiConnect - 1: 13881
Client successfully connected to server
getPostData - 1: 13812
getPostData - 2: 13774
getPostData - 3: 13463
getPostData - 4: 13205
getPostData - 5: 13698
oyoroiConnect - 2: 13550
oyoroiConnect - 3: 13550
oyoroi.queue: scoutId:4 message:ph=4.28 free-memory:13426
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:13402
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:13382
oyoroi.queue: scoutId:4 message:ph=4.28 free-memory:13356
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:13334
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:13305
oyoroiConnect - 1: 13623
Client successfully connected to server
getPostData - 1: 13554
getPostData - 2: 13516
getPostData - 3: 13205
getPostData - 4: 12947
getPostData - 5: 13440
oyoroiConnect - 2: 13292
oyoroiConnect - 3: 13292
oyoroi.queue: scoutId:4 message:ph=4.28 free-memory:13168
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:13144
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:13124
oyoroi.queue: scoutId:4 message:ph=4.28 free-memory:13098
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:13076
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:13047
oyoroiConnect - 1: 13365
Client successfully connected to server
getPostData - 1: 13296
getPostData - 2: 13258
getPostData - 3: 12947
getPostData - 4: 12689
getPostData - 5: 13182
oyoroiConnect - 2: 13034
oyoroiConnect - 3: 13034
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:12910
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:12886
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:12866
oyoroi.queue: scoutId:4 message:ph=4.28 free-memory:12840
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:12818
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:12789
oyoroiConnect - 1: 13107
Client successfully connected to server
getPostData - 1: 13038
getPostData - 2: 13000
getPostData - 3: 12689
getPostData - 4: 12431
getPostData - 5: 12924
oyoroiConnect - 2: 12776
oyoroiConnect - 3: 12776
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:12652
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:12628
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:12608
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:12582
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:12560
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:12531
oyoroiConnect - 1: 12849
Client successfully connected to server
getPostData - 1: 12780
getPostData - 2: 12742
getPostData - 3: 12431
getPostData - 4: 12173
getPostData - 5: 12666
oyoroiConnect - 2: 12518
oyoroiConnect - 3: 12518
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:12394
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:12370
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:12350
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:12324
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:12302
oyoroiConnect - 1: 12613
Client successfully connected to server
getPostData - 1: 12544
getPostData - 2: 12506
getPostData - 3: 12246
getPostData - 4: 11988
getPostData - 5: 12408
oyoroiConnect - 2: 12294
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:11947
oyoroiConnect - 3: 12272
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:12112
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:12092
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:12066
oyoroi.queue: scoutId:4 message:ph=4.28 free-memory:11939
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:11910
oyoroiConnect - 1: 12228
Client successfully connected to server
getPostData - 1: 12159
getPostData - 2: 12121
getPostData - 3: 11800
getPostData - 4: 11542
getPostData - 5: 12037
oyoroiConnect - 2: 11889
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:11542
oyoroiConnect - 3: 11867
oyoroi.queue: scoutId:4 message:ph=4.28 free-memory:11741
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:11721
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:11695
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:11673
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:11643
oyoroiConnect - 1: 11959
Client successfully connected to server
getPostData - 1: 11890
getPostData - 2: 11852
getPostData - 3: 11536
getPostData - 4: 11278
getPostData - 5: 11779
oyoroiConnect - 2: 11631
oyoroiConnect - 3: 11631
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:11507
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:11483
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:11463
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:11437
oyoroi.queue: scoutId:4 message:ph=4.28 free-memory:11415
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:11385
oyoroiConnect - 1: 11701
Client successfully connected to server
getPostData - 1: 11632
getPostData - 2: 11594
getPostData - 3: 11278
getPostData - 4: 11020
getPostData - 5: 11521
oyoroiConnect - 2: 11373
oyoroiConnect - 3: 11373
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:11249
oyoroi.queue: scoutId:4 message:ph=4.28 free-memory:11225
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:11205
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:11179
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:11157
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:11127
oyoroiConnect - 1: 11443
Client successfully connected to server
getPostData - 1: 11374
getPostData - 2: 11336
getPostData - 3: 11020
getPostData - 4: 10762
getPostData - 5: 11263
oyoroiConnect - 2: 11115
oyoroiConnect - 3: 11115
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:10991
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:10967
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:10947
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:10921
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:10899
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:10869
oyoroiConnect - 1: 11185
Client successfully connected to server
getPostData - 1: 11116
getPostData - 2: 11078
getPostData - 3: 10762
getPostData - 4: 10504
getPostData - 5: 11005
oyoroiConnect - 2: 10857
oyoroiConnect - 3: 10857
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:10733
oyoroi.queue: scoutId:4 message:ph=4.28 free-memory:10709
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:10689
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:10663
oyoroi.queue: scoutId:4 message:ph=4.28 free-memory:10641
oyoroiConnect - 1: 10952
Client successfully connected to server
getPostData - 1: 10883
getPostData - 2: 10845
getPostData - 3: 10577
getPostData - 4: 10319
getPostData - 5: 10747
oyoroiConnect - 2: 10633
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:10286
oyoroiConnect - 3: 10611
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:10451
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:10431
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:10405
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:10383
oyoroiConnect - 1: 10694
Client successfully connected to server
getPostData - 1: 10625
getPostData - 2: 10587
getPostData - 3: 10319
getPostData - 4: 10061
getPostData - 5: 10489
oyoroiConnect - 2: 10375
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:10028
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:10004
oyoroiConnect - 3: 10331
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:10173
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:10147
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:10125
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:10095
oyoroiConnect - 1: 10411
Client successfully connected to server
getPostData - 1: 10342
getPostData - 2: 10304
getPostData - 3: 9988
getPostData - 4: 9730
getPostData - 5: 10231
oyoroiConnect - 2: 10083
oyoroi.queue: scoutId:4 message:ph=4.28 free-memory:9736
oyoroiConnect - 3: 10061
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:9935
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:9915
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:9889
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:9867
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:9837
oyoroiConnect - 1: 10153
Client successfully connected to server
getPostData - 1: 10084
getPostData - 2: 10046
getPostData - 3: 9730
getPostData - 4: 9472
getPostData - 5: 9973
oyoroiConnect - 2: 9825
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:9478
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:9454
oyoroiConnect - 3: 9781
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:9657
oyoroi.queue: scoutId:4 message:ph=4.28 free-memory:9631
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:9609
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:9579
oyoroiConnect - 1: 9895
Client successfully connected to server
getPostData - 1: 9826
getPostData - 2: 9788
getPostData - 3: 9472
getPostData - 4: 9214
getPostData - 5: 9715
oyoroiConnect - 2: 9567
oyoroiConnect - 3: 9567
oyoroi.queue: scoutId:4 message:ph=4.28 free-memory:9443
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:9419
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:9399
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:9373
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:9351
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:9321
oyoroiConnect - 1: 9637
Client successfully connected to server
getPostData - 1: 9568
getPostData - 2: 9530
getPostData - 3: 9214
getPostData - 4: 8956
getPostData - 5: 9457
oyoroiConnect - 2: 9309
oyoroiConnect - 3: 9309
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:9185
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:9161
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:9141
oyoroi.queue: scoutId:4 message:ph=4.28 free-memory:9115
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:9093
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:9063
oyoroiConnect - 1: 9379
Client successfully connected to server
getPostData - 1: 9310
getPostData - 2: 9272
getPostData - 3: 8956
getPostData - 4: 8698
getPostData - 5: 9199
oyoroiConnect - 2: 9051
oyoroiConnect - 3: 9051
oyoroi.queue: scoutId:4 message:ph=4.28 free-memory:8927
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:8903
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:8883
oyoroi.queue: scoutId:4 message:ph=4.28 free-memory:8857
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:8835
oyoroiConnect - 1: 9146
Client successfully connected to server
getPostData - 1: 9077
getPostData - 2: 9039
getPostData - 3: 8771
getPostData - 4: 8513
getPostData - 5: 8941
oyoroiConnect - 2: 8827
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:8480
oyoroiConnect - 3: 8805
oyoroi.queue: scoutId:4 message:ph=4.28 free-memory:8645
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:8625
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:8599
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:8577
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:8547
oyoroiConnect - 1: 8863
Client successfully connected to server
getPostData - 1: 8794
getPostData - 2: 8756
getPostData - 3: 8440
getPostData - 4: 8182
getPostData - 5: 8683
oyoroiConnect - 2: 8535
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:8188
oyoroiConnect - 3: 8513
oyoroi.queue: scoutId:4 message:ph=4.28 free-memory:8387
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:8367
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:8341
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:8319
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:8289
oyoroiConnect - 1: 8605
Client successfully connected to server
getPostData - 1: 8536
getPostData - 2: 8498
getPostData - 3: 8182
getPostData - 4: 7924
getPostData - 5: 8425
oyoroiConnect - 2: 8277
oyoroiConnect - 3: 8277
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:8153
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:8129
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:8109
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:8083
oyoroi.queue: scoutId:4 message:ph=4.28 free-memory:8061
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:8031
oyoroiConnect - 1: 8347
Client successfully connected to server
getPostData - 1: 8278
getPostData - 2: 8240
getPostData - 3: 7924
getPostData - 4: 7666
getPostData - 5: 8167
oyoroiConnect - 2: 8019
oyoroiConnect - 3: 8019
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:7895
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:7871
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:7851
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:7825
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:7803
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:7773
oyoroiConnect - 1: 8089
Client successfully connected to server
getPostData - 1: 8020
getPostData - 2: 7982
getPostData - 3: 7666
getPostData - 4: 7408
getPostData - 5: 7909
oyoroiConnect - 2: 7761
oyoroiConnect - 3: 7761
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:7637
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:7613
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:7593
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:7567
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:7545
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:7515
oyoroiConnect - 1: 7831
Client successfully connected to server
getPostData - 1: 7762
getPostData - 2: 7724
getPostData - 3: 7408
getPostData - 4: 7150
getPostData - 5: 7651
oyoroiConnect - 2: 7503
oyoroiConnect - 3: 7503
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:7379
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:7355
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:7335
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:7309
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:7272
oyoroiConnect - 1: 7583
Client successfully connected to server
getPostData - 1: 7514
getPostData - 2: 7476
getPostData - 3: 7211
getPostData - 4: 6953
getPostData - 5: 7378
oyoroiConnect - 2: 7264
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:6915
oyoroiConnect - 3: 7254
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:7094
oyoroi.queue: scoutId:4 message:ph=4.28 free-memory:7072
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:7046
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:7024
oyoroiConnect - 1: 7337
Client successfully connected to server
getPostData - 1: 7268
getPostData - 2: 7231
getPostData - 3: 6971
getPostData - 4: 6713
getPostData - 5: 7135
oyoroiConnect - 2: 7021
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:6674
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:6650
oyoroiConnect - 3: 6977
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:6819
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:6793
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:6771
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:6741
oyoroiConnect - 1: 7057
Client successfully connected to server
getPostData - 1: 6988
getPostData - 2: 6950
getPostData - 3: 6634
getPostData - 4: 6376
getPostData - 5: 6877
oyoroiConnect - 2: 6729
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:6382
oyoroiConnect - 3: 6707
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:6581
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:6561
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:6535
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:6513
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:6483
oyoroiConnect - 1: 6799
Client successfully connected to server
getPostData - 1: 6730
getPostData - 2: 6692
getPostData - 3: 6376
getPostData - 4: 6118
getPostData - 5: 6619
oyoroiConnect - 2: 6471
oyoroiConnect - 3: 6471
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:6347
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:6323
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:6303
oyoroi.queue: scoutId:4 message:ph=4.26 free-memory:6277
oyoroi.queue: scoutId:3 message:ph=4.28 free-memory:6255
oyoroi.queue: scoutId:2 message:ph=5.59 free-memory:6225
oyoroiConnect - 1: 6541
Client successfully connected to server
getPostData - 1: 6472
getPostData - 2: 6434
getPostData - 3: 6118
getPostData - 4: 5860
getPostData - 5: 6361
oyoroiConnect - 2: 6213
oyoroiConnect - 3: 6213
...
我真的不知道造成这种泄漏的原因是什么,如果有人能提出一些建议我真的很感谢你的帮助
答案 0 :(得分:2)
有人给我资源有限的嵌入式系统的最佳建议是:
远离堆。
你可能没有泄漏记忆,你可能只是将它破碎了。您显示的程序是在FIFO队列上分配和取消分配结构。这种模式的运行机会很差。当第二个对象被添加到堆并放入队列时,它将第一个对象隐藏在堆上。当处理Q的头部时,代码会删除()第一个对象分配,但这会在内存中留下一个漏洞。如果第三个对象无法放入孔中,那么第三个对象将移动到未使用的空间。
对于您打算运行固定任务并希望永久运行的任何应用程序,请进行一次内存分配和重用。换句话说,没有操作系统或庞大的运行时库来支持您的应用程序:您必须管理内存。
分配N个对象的固定池
struct OMessage {
int scoutId;
char message[50];
int messageKey;
};
const int sizePool = 10;
OMessage pool[10];
int ixNext = 0;
int ixHead = -1;
此固定分配现在也是您的队列。你管理头部和尾部。当你&#34;添加&#34;并且对象到队列,您填写结构并调整索引:
// detect full queue if ixNext = ixHead and reject addition
pool[ixNext].scoutId = ...
pool[ixNext].messageKey = ...
ixNext += 1;
ixNext = ixNext % sizePool;
当你&#34;拉&#34;和队列中的项目,您只需移动索引:
if(ixHead >= 0) {
do something with pool[ixHead]
ixHead += 1;
ixHead = ixHead % sizePool;
// detect if head = tail and mark q empty
}
使用固定内存分配,只要程序启动,您就可以确定它永远不会耗尽内存。