我在 C++ dll 中有以下代码,我通过 JNI 调用它:
std::vector<double> myVector;
myVector.resize(10000000, 0);
我收到“错误分配”异常,即使 vector 的最大大小应该大于 10000000。
我应该使用什么工具来跟踪内存分配,以便定位任何内存泄漏?
如果真的没有内存泄漏,我该如何减少 vector 的占用空间以确保我有足够的空间?
最佳答案
我知道这可能是找出分配大小的最差解决方案。所以这里是:
主要.cpp:
#include "jni.h"
#include <vector>
#include <iostream>
#if (_MSC_VER == 1800) || (__cplusplus >= 201103L)
#include <thread>
#include <chrono>
#elif _MSC_VER
#include <windows.h>
#else
#include <unistd.h>
#endif
extern "C" JNIEXPORT void Java_test_Test_Alloc(JNIEnv* env, jobject obj, jint max_size, jint increment_size)
{
std::vector<double> vec;
size_t isize = max_size / 4;
size_t oldsize = isize;
while(isize <= max_size)
{
try
{
vec.resize(isize, 0);
oldsize = isize;
isize += increment_size;
std::cout<<"Allocated: "<<vec.size() * sizeof(double)<<" bytes.\n";
}
catch (std::bad_alloc &e)
{
std::cout<<"Failed to allocate: "<<isize * sizeof(double)<<" bytes.\n";
std::cout<<"Approx. max size: "<<oldsize * sizeof(double)<<" bytes.\n";
std::cout<<"Exception: "<<e.what()<< "\n";
std::cout<<"Vector.Max_Size(): "<<vec.max_size()<< "\n";
break;
}
#if (_MSC_VER == 1800) || (__cplusplus >= 201103L)
std::this_thread::sleep_for(std::chrono::seconds(1));
#elif _MSC_VER
Sleep(1);
#else
sleep(1);
#endif
}
}
#if defined _WIN32 || defined _WIN64
#include <windows.h>
extern "C" __declspec(dllexport) bool __stdcall DllMain(HINSTANCE hinstDLL, unsigned fdwReason, void* lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
}
return true;
}
#endif
在 Java 端 (Test.java):
package test;
public class Test {
static {
System.loadLibrary("JNITest");
}
public static native void Alloc(int max_size, int increment_size);
public static void main(String[] args) {
Alloc(100000000, 50000);
}
}
对我来说,它打印:
Allocated: 200000000 bytes.
Allocated: 200400000 bytes.
Allocated: 200800000 bytes.
.
.
.
Allocated: 399600000 bytes.
Allocated: 400000000 bytes.
Failed to allocate: 400400000 bytes.
Approx. max size: 400000000 bytes.
Exception: std::bad_alloc
Vector.Max_Size(): 536870911
就像我说的..“猜测”你可以分配多少是一种糟糕的方式,但在这种情况下,没有其他人发布任何东西,所以我将把它留在这里,你可以使用它,如果你希望..
关于c++ - `std::vector` 在调整大小时抛出 "bad allocation"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21885986/
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我正在学习Rails,并阅读了关于乐观锁的内容。我已将类型为integer的lock_version列添加到我的articles表中。但现在每当我第一次尝试更新记录时,我都会收到StaleObjectError异常。这是我的迁移:classAddLockVersionToArticle当我尝试通过Rails控制台更新文章时:article=Article.first=>#我这样做:article.title="newtitle"article.save我明白了:(0.3ms)begintransaction(0.3ms)UPDATE"articles"SET"title"='dwdwd
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee