- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一直在从事一个项目,从一些日志文件中收集记录,并将其托管在数据库中以进行分析,我需要处理数百万行。对于 sql 插入目的,我正在尝试批量插入 1000 条或更多记录。
我创建了一个自己的函数来连接 1000 条记录的 sql 查询。
步骤
free()
处理和释放 sql 查询sleep(1)
。这样我就有时间检查任务管理器上的资源监视器并按 ctrl+c
问题是,
我的系统有 8GB 内存,在运行程序之前,内存使用量为 1.8GB。下面是循环计数和内存使用量(近似值)。
loop memory usage
10,000 2.2 GB
20,000 3.6 GB
40,000 5.0 GB
它继续......当达到 60,000+ 时,内存使用率变为 100%。
恐怕我在某处做错了什么。我想不通。如何在每次执行时释放内存并在程序结束时保持系统稳定?
下面是我的完整测试代码...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
typedef struct _device_data
{
int uid;
double stime;
double dur;
char *location;
} DeviceData;
char *buildSQLstr(const char *sql, const DeviceData *deviceData);
int main(void)
{
char *sql = "";
int loop = 0;
int sqlIsReady = 0;
for (loop = 0; loop<60000; loop++)
{
DeviceData deviceData = {};
deviceData.stime = 343434.34343;
deviceData.dur = 1.00343;
deviceData.location = malloc(50);
deviceData.location[0] = '\0';
sprintf(deviceData.location, "Location No: - %d", loop);
if (loop % 1000 == 0) {
if (sqlIsReady) {
printf("\nLoop = %d\tLength of ssql : %d\n", loop, strlen(sql));
free(sql);
sql = "";
sqlIsReady = 0;
sleep(1);
}
}
else {
sql = buildSQLstr(sql, &deviceData);
if (strlen(sql) > 0)
sqlIsReady = 1;
}
}
printf("%d\n", strlen(sql));
getchar();
return 0;
}
char *buildSQLstr(const char *sql, const DeviceData *deviceData)
{
char *insert_pattern = "INSERT INTO access(stime,dur,location,uid) VALUES (datetime(%f, 'unixepoch'), %f, '%s', %d);";
int sql_size = strlen(insert_pattern) + (sizeof(double) * 2) + strlen(deviceData->location) + sizeof(int) + 1;
char *sql_insert = malloc(sql_size);
sql_insert[0] = '\0';
sprintf(sql_insert, insert_pattern, deviceData->stime, deviceData->dur, deviceData->location, deviceData->uid);
char *ptrRetSql = (char *) malloc(strlen(sql) + strlen(sql_insert) + 1);
if (ptrRetSql != NULL) {
ptrRetSql[0] = '\0';
strncpy(ptrRetSql, sql, strlen(sql));
strncat(ptrRetSql, sql_insert, strlen(sql_insert));
}
else {
fprintf(stderr, "Malloc failed : %s\n", strerror(errno));
}
free(sql_insert); // here I am freeing the memory allocated for sql_insert pointer
return ptrRetSql;
}
.
请指教我怎样才能克服这个问题..
编辑(最终工作副本)
谢谢大家,根据下面的回复,我修改了我的代码,如下图所示。它现在工作顺利。我的内存使用量表现在对于任何数量的循环都是稳定的。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
typedef struct
{
int uid;
double stime;
double dur;
char *location;
} DeviceData_t;
char *buildSQLstr(const char *sql, const DeviceData_t *deviceData);
int main(void)
{
char *sql = NULL;
int loop = 0;
int sqlIsReady = 0;
for (loop = 0; loop<100010; loop++)
{
DeviceData_t *deviceData = (DeviceData_t *) malloc(sizeof(DeviceData_t));
deviceData->stime = 343434.34343;
deviceData->dur = 1.00343;
deviceData->location = (char *) malloc(50);
if (deviceData->location != NULL) {
deviceData->location[0] = '\0';
sprintf(deviceData->location, "Location No: - %d", loop);
}
if (loop % 1000 == 0) {
if (sqlIsReady) {
if (sql != NULL) free(sql);
// Process here the sql
sql = NULL;
sqlIsReady = 0;
}
printf("Current loop count : %d\n", loop);
}
else {
char *tmpSql = buildSQLstr(sql, deviceData);
if (sql != NULL) free(sql);
sql = malloc(strlen(tmpSql) + 1);
sql[0] = '\0';
strcpy(sql, tmpSql);
free(tmpSql);
if (strlen(sql) > 0)
sqlIsReady = 1;
}
if (deviceData != NULL) free(deviceData);
}
if (sql != NULL) {
printf("%Remaining SQL Length : %d\n%s\n", strlen(sql), sql);
free(sql);
}
return 0;
}
char *buildSQLstr(const char *sql, const DeviceData_t *deviceData)
{
char *insert_pattern = "INSERT INTO access(stime,dur,location,uid) VALUES (datetime(%f, 'unixepoch'), %f, '%s', %d);";
int sql_size = strlen(insert_pattern) + (sizeof(double) * 2) + strlen(deviceData->location) + sizeof(int) + 1;
char *sql_insert = (char *) malloc(sql_size);
sql_insert[0] = '\0';
sprintf(sql_insert, insert_pattern, deviceData->stime, deviceData->dur, deviceData->location, deviceData->uid);
int ptrRetSql_size;
if (sql != NULL ) ptrRetSql_size = strlen(sql) + strlen(sql_insert) + 1;
else ptrRetSql_size = strlen(sql_insert) + 1;
char *ptrRetSql = (char *) malloc(ptrRetSql_size);
if (ptrRetSql != NULL) {
ptrRetSql[0] = '\0';
if (sql != NULL) strcat(ptrRetSql, sql);
if (sql != NULL) strcat(ptrRetSql, sql_insert);
else strcpy(ptrRetSql, sql_insert);
}
else {
fprintf(stderr, "Malloc failed : %s\n", strerror(errno));
}
if (sql_insert != NULL) free(sql_insert);
return ptrRetSql;
}
这里出现了另一个问题。我应该单独清理 deviceData->location
分配的内存吗?
最佳答案
您的代码 malloc
是 buildSQLstr
函数的返回值(从底部开始第 13 行),然后将字符串返回给 main
.但是,main
和 buildSQLstr
都不会释放该字符串,直到迭代被 1000 整除,从而在剩余的 999 次迭代中造成内存泄漏。
由于传递给 buildSQLstr
的 sql
的旧值在 strcat
之后变得无关紧要,您可以释放
旧值字符串就在那里:
if (ptrRetSql != NULL) {
ptrRetSql[0] = '\0';
strncpy(ptrRetSql, sql, strlen(sql));
free(sql); // <<==== Here
strncat(ptrRetSql, sql_insert, strlen(sql_insert));
}
您还需要在 main
中free
sql
的最终值以避免泄漏最后的 sql
字符串。为了正确地做到这一点,您应该将声明更改为
char *sql = malloc(1);
sql[0] = '\0';
否则,当日志为空时,您可能会将指向 ""
字符串文字的指针传递给 free
,从而导致未定义的行为。当您释放它时,在 if
的顶部分支中分配 ""
也是如此。
关于c - 如何在c中有效地构建字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22506192/
我收到未知数据,我想以编程方式查看相关性,并将所有完全相关的变量组合在一起(忽略方向)。在下面的数据集中,我可以手动查看相关性并说 a, f, g, h一起去吧b, d, e .我怎样才能以编程方
这个问题在这里已经有了答案: use dplyr's summarise_each to return one row per function? (3 个答案) 关闭 4 年前。 作为探索性工作的
我想要完成的是使用数组存储未知大小的多项式。我在互联网上看到的是使用一个数组,每个单元格都包含系数,度数是单元格编号,但这不是有效的,因为如果我们有一个多项式,如:6x^14+x+5。这意味着我们将从
嘿伙计们,我一直在尝试解析 HTML 文件以从中抓取文本,但时不时地,我会得到一些非常奇怪的字符,例如 à€œ。我确定是“智能引号”或弯头标点符号导致了我的所有问题,因此我的临时修复是搜索所有这些字符
我原来的 data.table 由三列组成。 site、observation_number 和 id。 例如以下是 id = z 的所有观察结果 |site|observation_number|i
"Premature optimisation is the root of all evil (but it's nice to have once you have an ugly solutio
给定这个数组 X: [1 2 3 2 3 1 4 5 7 1] 和行长度数组R: [3 2 5] 表示转换后每行的长度。 我正在寻找一个计算效率高的函数来将 X reshape 为数组 Y: [[ 1
我有一些 data.frame s: # Sample data a <- data.frame(c(1:10), c(11:20)) names(a) <- c("A", "B") b <- dat
我有点困惑。列表擅长任意位置插入,但不善于随机访问? (怎么可能)如果你不能随机访问,你怎么知道在哪里插入? 同样,如果你可以在任何位置插入,为什么你不能从那个位置高效地读取? 最佳答案 如果您已经有
我有一个向量,我想计算它的移动平均值(使用宽度为 5 的窗口)。 例如,如果有问题的向量是[1,2,3,4,5,6,7,8],那么 结果向量的第一个条目应该是 [1,2,3,4,5] 中所有条目的总和
有一个随机整数生成器,它生成随机整数并在后台运行。需求设计一个API,调用时返回当时的簇数。 簇:簇是连续整数的字典顺序。例如,在这种情况下,10,7,1,2,8,5,9 簇是 3 (1,2--5--
我想做的是将一组 (n) 项分成大小相等的组(大小为 m 的组,并且为简单起见,假设没有剩余,即 n 可以被 m 整除)。这样做多次,我想确保同一组中的任何项目都不会出现两次。 为了使这稍微更具体一些
假设我有一些包含类型排列的模板表达式,在本例中它们来自 Abstract Syntax Tree : template
我已经在这方面工作了几天,似乎没有我需要的答案。 由于担心这个被标记为重复,我将解释为什么其他问题对我不起作用。 使用 DIFFLIB for Python 的任何答案都无助于我的需求。 (我在下面描
我正在使用 NumPy 数组。 我有一个 2N 长度向量 D,并希望将其一部分 reshape 为 N x N 数组 C. 现在这段代码可以满足我的要求,但对于较大的 N 来说是一个瓶颈: ``` i
我有一个问题: 让我们考虑这样的 pandas 数据框: Width Height Bitmap 67 56 59 71 61 73 ...
我目前正在用 C 语言编写一个解析器,设计它时我需要的东西之一是一个可变字符串“类”(一组对表示实例的不透明结构进行操作的函数),我将其称为 my_string。 string 类的实例只不过是包装
假设我在 --pandas-- 数据框中有以下列: x 1 589 2 354 3 692 4 474 5 739 6 731 7 259 8 723
我有一个成员函数,它接受另一个对象的常量引用参数。我想 const_cast 这个参数以便在成员函数中轻松使用它。为此,以下哪个代码更好?: void AClass::AMember(const BC
我们目前正在将 Guava 用于其不可变集合,但我惊讶地发现他们的 map 没有方法可以轻松创建只需稍作修改的新 map 。最重要的是,他们的构建器不允许为键分配新值或删除键。 因此,如果我只想修改一
我是一名优秀的程序员,十分优秀!